Class: Haipa::Resource
- Inherits:
-
Object
- Object
- Haipa::Resource
- Defined in:
- lib/haipa/resource.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#data ⇒ Object
(also: #to_hash)
readonly
Returns the value of attribute data.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete ⇒ Object
- #embedded ⇒ Object (also: #resources)
- #get ⇒ Object
- #href ⇒ Object
-
#initialize(api, uri, data = nil) ⇒ Resource
constructor
A new instance of Resource.
- #links ⇒ Object
- #post(body) ⇒ Object
- #put(body) ⇒ Object
Constructor Details
#initialize(api, uri, data = nil) ⇒ Resource
Returns a new instance of Resource.
9 10 11 12 13 |
# File 'lib/haipa/resource.rb', line 9 def initialize(api, uri, data=nil) @api = api @uri = uri @data = data end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
6 7 8 |
# File 'lib/haipa/resource.rb', line 6 def api @api end |
#data ⇒ Object (readonly) Also known as: to_hash
Returns the value of attribute data.
6 7 8 |
# File 'lib/haipa/resource.rb', line 6 def data @data end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/haipa/resource.rb', line 6 def uri @uri end |
Instance Method Details
#clear ⇒ Object
15 16 17 18 |
# File 'lib/haipa/resource.rb', line 15 def clear @data = nil self end |
#delete ⇒ Object
29 30 31 |
# File 'lib/haipa/resource.rb', line 29 def delete api.delete(uri) end |
#embedded ⇒ Object Also known as: resources
47 48 49 |
# File 'lib/haipa/resource.rb', line 47 def Embedded.new(self, get.fetch('_embedded', {})) end |
#get ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/haipa/resource.rb', line 20 def get return @data if @data return {} unless uri response = api.get(uri) raise FailureResponseError unless response.success? raise EmptyResponseError if !response.body || response.body.empty? @data = JSON.parse(response.body) end |
#href ⇒ Object
56 57 58 |
# File 'lib/haipa/resource.rb', line 56 def href links.to_hash.fetch('self', {}).fetch('href', nil) end |
#links ⇒ Object
52 53 54 |
# File 'lib/haipa/resource.rb', line 52 def links Links.new(self, get.fetch('_links', {})) end |
#post(body) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/haipa/resource.rb', line 33 def post(body) api.post do |conn| conn.url uri conn.body = body end end |
#put(body) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/haipa/resource.rb', line 40 def put(body) api.put do |conn| conn.url uri conn.body = body end end |