Class: Sink::Link

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (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_jsonObject



63
# File 'lib/sink/link.rb', line 63

def as_json(*) = to_h

#cloaking?Boolean

Returns:

  • (Boolean)


47
# File 'lib/sink/link.rb', line 47

def cloaking? = !!@attributes[:cloaking]

#created?Boolean

Returns:

  • (Boolean)


55
# File 'lib/sink/link.rb', line 55

def created? = status == "created"

#created_atObject



41
# File 'lib/sink/link.rb', line 41

def created_at = time(:created_at)

#existing?Boolean

Returns:

  • (Boolean)


57
# File 'lib/sink/link.rb', line 57

def existing? = status == "existing"

#expirationObject



39
# File 'lib/sink/link.rb', line 39

def expiration = @attributes[:expiration]

#expired?Boolean

Returns:

  • (Boolean)


53
# File 'lib/sink/link.rb', line 53

def expired? = !expiration.nil? && expiration <= Time.now.to_i

#expires_atObject



45
# File 'lib/sink/link.rb', line 45

def expires_at = time(:expiration)

#geoObject



37
# File 'lib/sink/link.rb', line 37

def geo = @attributes[:geo] || {}

#hashObject



68
# File 'lib/sink/link.rb', line 68

def hash = @attributes.hash

#inspectObject



70
# File 'lib/sink/link.rb', line 70

def inspect = "#<#{self.class} slug=#{slug.inspect} url=#{url.inspect}>"

#redirect_with_query?Boolean

Returns:

  • (Boolean)


51
# File 'lib/sink/link.rb', line 51

def redirect_with_query? = !!@attributes[:redirect_with_query]

#tagsObject



35
# File 'lib/sink/link.rb', line 35

def tags = @attributes[:tags] || []

#to_hObject



61
# File 'lib/sink/link.rb', line 61

def to_h = @attributes.dup

#unsafe?Boolean

Returns:

  • (Boolean)


49
# File 'lib/sink/link.rb', line 49

def unsafe? = !!@attributes[:unsafe]

#updated_atObject



43
# File 'lib/sink/link.rb', line 43

def updated_at = time(:updated_at)