Class: Restify::Resource
- Inherits:
-
Object
- Object
- Restify::Resource
- Includes:
- Relations
- Defined in:
- lib/restify/resource.rb
Instance Attribute Summary collapse
-
#media_type ⇒ MediaType
readonly
Return content Media-Type.
Class Method Summary collapse
-
.create(client, data, response) ⇒ Object
Build a resource from given response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve value for given key.
-
#[]=(key, value) ⇒ Object
Set value for given key.
-
#attributes ⇒ HashWithIndifferentAccess
Return parsed resource attributes as hash.
-
#code ⇒ Fixnum
Return response status code if available.
-
#each ⇒ Object
Iterate over keys and values or return enumerator.
-
#follow ⇒ Obligation<Resource>
Follow the Location header from the response of this resource if available.
-
#initialize(client, relations = {}, attributes = {}, response = nil) ⇒ Resource
constructor
A new instance of Resource.
-
#key?(name) ⇒ Boolean
Check if resource has given key.
-
#status ⇒ Symbol
Return response status if available.
Methods included from Relations
Constructor Details
#initialize(client, relations = {}, attributes = {}, response = nil) ⇒ Resource
Returns a new instance of Resource.
91 92 93 94 95 96 |
# File 'lib/restify/resource.rb', line 91 def initialize(client, relations = {}, attributes = {}, response = nil) @client = client @response = response @relations = HashWithIndifferentAccess.new relations @attributes = HashWithIndifferentAccess.new attributes end |
Instance Attribute Details
#media_type ⇒ MediaType (readonly)
Return content Media-Type.
10 11 12 |
# File 'lib/restify/resource.rb', line 10 def media_type @media_type end |
Class Method Details
.create(client, data, response) ⇒ Object
Build a resource from given response.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/restify/resource.rb', line 102 def create(client, data, response) relations = {} relations = response.relations(client) if response hash = {} if data data.each do |key, value| hash[key] = case value when Array Collection.create(client, value, nil) when Hash Resource.create(client, value, nil) else value end end data.keys.each do |key| name = nil if (m = /\A(\w+)_url\z/.match(key)) name = m[1] elsif key == 'url' name = :self else next end unless relations.key?(name) || data[key].to_s.blank? relations[name] = Relation.new(client, data[key].to_s) end end end new client, relations, hash, response end |
Instance Method Details
#[](key) ⇒ Object
Retrieve value for given key.
18 |
# File 'lib/restify/resource.rb', line 18 delegate :[], to: :attributes |
#[]=(key, value) ⇒ Object
Set value for given key.
26 |
# File 'lib/restify/resource.rb', line 26 delegate :[]=, to: :attributes |
#attributes ⇒ HashWithIndifferentAccess
Return parsed resource attributes as hash.
58 59 60 |
# File 'lib/restify/resource.rb', line 58 def attributes @attributes ||= HashWithIndifferentAccess.new end |
#code ⇒ Fixnum
Return response status code if available.
76 |
# File 'lib/restify/resource.rb', line 76 delegate :code, to: :@response, allow_nil: true |
#each {|key, value| ... } ⇒ Object #each ⇒ Enumerator
Iterate over keys and values or return enumerator.
52 |
# File 'lib/restify/resource.rb', line 52 delegate :each, to: :attributes |
#follow ⇒ Obligation<Resource>
Follow the Location header from the response of this resource if available.
83 84 85 86 87 88 89 |
# File 'lib/restify/resource.rb', line 83 def follow if @response && @response.headers['LOCATION'] @client.request :get, @response.headers['LOCATION'] else raise RuntimeError.new 'Nothing to follow.' end end |
#key?(name) ⇒ Boolean
Check if resource has given key.
34 |
# File 'lib/restify/resource.rb', line 34 delegate :key?, :has_key?, to: :attributes |
#status ⇒ Symbol
Return response status if available.
68 |
# File 'lib/restify/resource.rb', line 68 delegate :status, to: :@response, allow_nil: true |