Class: Excon::HyperMedia::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/excon/hypermedia/resource.rb

Overview

Resource

This HyperMedia::Resource object encapsulates the returned JSON and makes it easy to access the links and attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Resource



15
16
17
# File 'lib/excon/hypermedia/resource.rb', line 15

def initialize(body)
  @body = body
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/excon/hypermedia/resource.rb', line 13

def data
  @data
end

Instance Method Details

#attributesObject



27
28
29
30
31
32
33
# File 'lib/excon/hypermedia/resource.rb', line 27

def attributes
  attributes = data.reject do |k, _|
    k == '_links'
  end

  Struct.new(*attributes.keys.map(&:to_sym)).new(*attributes.values)
end


23
24
25
# File 'lib/excon/hypermedia/resource.rb', line 23

def link(link_name)
  Link.new(name: link_name, hash: data)
end


19
20
21
# File 'lib/excon/hypermedia/resource.rb', line 19

def links
  data.fetch('_links', {}).keys.map { |name| link(name) }
end

#type?(name) ⇒ Boolean



35
36
37
38
39
40
# File 'lib/excon/hypermedia/resource.rb', line 35

def type?(name)
  return :link if link(name).valid?
  return :attribute if attributes.respond_to?(name.to_s)

  :unknown
end