Class: Excon::HyperMedia::Resource
- Inherits:
-
Object
- Object
- Excon::HyperMedia::Resource
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(body) ⇒ Resource
constructor
A new instance of Resource.
- #link(link_name) ⇒ Object
- #links ⇒ Object
- #type?(name) ⇒ Boolean
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
#data ⇒ Object (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
#attributes ⇒ Object
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 |
#link(link_name) ⇒ Object
23 24 25 |
# File 'lib/excon/hypermedia/resource.rb', line 23 def link(link_name) Link.new(name: link_name, hash: data) end |
#links ⇒ Object
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 |