Class: Haipa::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#apiObject (readonly)

Returns the value of attribute api.



6
7
8
# File 'lib/haipa/resource.rb', line 6

def api
  @api
end

#dataObject (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

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/haipa/resource.rb', line 6

def uri
  @uri
end

Instance Method Details

#clearObject



15
16
17
18
# File 'lib/haipa/resource.rb', line 15

def clear
  @data = nil
  self
end

#deleteObject



29
30
31
# File 'lib/haipa/resource.rb', line 29

def delete
  api.delete(uri)
end

#embeddedObject Also known as: resources



47
48
49
# File 'lib/haipa/resource.rb', line 47

def embedded
  Embedded.new(self, get.fetch('_embedded', {}))
end

#getObject



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

#hrefObject



56
57
58
# File 'lib/haipa/resource.rb', line 56

def href
  links.to_hash.fetch('self', {}).fetch('href', nil)
end


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