Class: Sink::Link
- Inherits:
-
Object
- Object
- Sink::Link
- Defined in:
- lib/sink/link.rb
Constant Summary collapse
- ATTRIBUTES =
%i[ id url slug comment title description image apple google password cloaking redirect_with_query unsafe geo tags expiration created_at updated_at short_link status ].freeze
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.from_response(body, base_url: nil) ⇒ Object
The API only builds
shortLinkfor write endpoints, from the protocol and host of the request, sobase_urlreproduces it for the read endpoints.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
- #as_json ⇒ Object
- #cloaking? ⇒ Boolean
- #created? ⇒ Boolean
- #created_at ⇒ Object
- #existing? ⇒ Boolean
- #expiration ⇒ Object
- #expired? ⇒ Boolean
- #expires_at ⇒ Object
- #geo ⇒ Object
- #hash ⇒ Object
-
#initialize(attributes) ⇒ Link
constructor
A new instance of Link.
- #inspect ⇒ Object
- #redirect_with_query? ⇒ Boolean
- #tags ⇒ Object
- #to_h ⇒ Object
- #unsafe? ⇒ Boolean
- #updated_at ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Link
Returns a new instance of Link.
27 28 29 |
# File 'lib/sink/link.rb', line 27 def initialize(attributes) @attributes = attributes.freeze end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/sink/link.rb', line 11 def attributes @attributes end |
Class Method Details
.from_response(body, base_url: nil) ⇒ Object
The API only builds shortLink for write endpoints, from the protocol and
host of the request, so base_url reproduces it for the read endpoints.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sink/link.rb', line 15 def self.from_response(body, base_url: nil) return nil unless body.is_a?(Hash) link = body["link"].is_a?(Hash) ? body["link"] : body attributes = Keys.underscore_keys(link) attributes[:status] = body["status"] if body.key?("status") attributes[:short_link] = body["shortLink"] || (base_url && attributes[:slug] && "#{base_url}/#{attributes[:slug]}") new(attributes.compact) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
65 |
# File 'lib/sink/link.rb', line 65 def ==(other) = other.is_a?(Link) && other.attributes == @attributes |
#[](key) ⇒ Object
59 |
# File 'lib/sink/link.rb', line 59 def [](key) = @attributes[key.to_sym] |
#as_json ⇒ Object
63 |
# File 'lib/sink/link.rb', line 63 def as_json(*) = to_h |
#cloaking? ⇒ Boolean
47 |
# File 'lib/sink/link.rb', line 47 def cloaking? = !!@attributes[:cloaking] |
#created? ⇒ Boolean
55 |
# File 'lib/sink/link.rb', line 55 def created? = status == "created" |
#created_at ⇒ Object
41 |
# File 'lib/sink/link.rb', line 41 def created_at = time(:created_at) |
#existing? ⇒ Boolean
57 |
# File 'lib/sink/link.rb', line 57 def existing? = status == "existing" |
#expiration ⇒ Object
39 |
# File 'lib/sink/link.rb', line 39 def expiration = @attributes[:expiration] |
#expired? ⇒ Boolean
53 |
# File 'lib/sink/link.rb', line 53 def expired? = !expiration.nil? && expiration <= Time.now.to_i |
#expires_at ⇒ Object
45 |
# File 'lib/sink/link.rb', line 45 def expires_at = time(:expiration) |
#geo ⇒ Object
37 |
# File 'lib/sink/link.rb', line 37 def geo = @attributes[:geo] || {} |
#hash ⇒ Object
68 |
# File 'lib/sink/link.rb', line 68 def hash = @attributes.hash |
#inspect ⇒ Object
70 |
# File 'lib/sink/link.rb', line 70 def inspect = "#<#{self.class} slug=#{slug.inspect} url=#{url.inspect}>" |
#redirect_with_query? ⇒ Boolean
51 |
# File 'lib/sink/link.rb', line 51 def redirect_with_query? = !!@attributes[:redirect_with_query] |
#tags ⇒ Object
35 |
# File 'lib/sink/link.rb', line 35 def = @attributes[:tags] || [] |
#to_h ⇒ Object
61 |
# File 'lib/sink/link.rb', line 61 def to_h = @attributes.dup |
#unsafe? ⇒ Boolean
49 |
# File 'lib/sink/link.rb', line 49 def unsafe? = !!@attributes[:unsafe] |
#updated_at ⇒ Object
43 |
# File 'lib/sink/link.rb', line 43 def updated_at = time(:updated_at) |