Class: Zipmark::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Resource

Returns a new instance of Resource.



5
6
7
# File 'lib/zipmark/resource.rb', line 5

def initialize(options={})
  @options = Util.stringify_keys(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/zipmark/resource.rb', line 3

def options
  @options
end

Instance Method Details

#allObject



9
10
11
# File 'lib/zipmark/resource.rb', line 9

def all
  Zipmark::Collection.new(self, Zipmark::Iterator)
end

#build(options) ⇒ Object



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

def build(options)
  Zipmark::Entity.new(options.merge(:client => client, :resource_type => resource_name))
end

#clientObject



45
46
47
# File 'lib/zipmark/resource.rb', line 45

def client
  options["client"] || raise(Zipmark::ClientError, "You must pass :client as an option")
end

#create(options) ⇒ Object



27
28
29
30
# File 'lib/zipmark/resource.rb', line 27

def create(options)
  entity = build(options)
  entity.save
end

#find(id) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/zipmark/resource.rb', line 13

def find(id)
  response = client.get("/" + rel + "/" + id)
  object = JSON.parse(response.body)
  if client.successful?(response)
    Zipmark::Entity.new(object[resource_name].merge(:client => client, :resource_type => resource_name))
  else
    raise Zipmark::Error.new(!!object ? object.merge(:status => response.status) : response)
  end
end

#hrefObject



32
33
34
# File 'lib/zipmark/resource.rb', line 32

def href
  options["href"] || raise(Zipmark::ResourceError, "Resource did not specify href")
end

#relObject



36
37
38
# File 'lib/zipmark/resource.rb', line 36

def rel
  options["rel"] || raise(Zipmark::ResourceError, "Resource did not specify rel")
end

#resource_nameObject



40
41
42
43
# File 'lib/zipmark/resource.rb', line 40

def resource_name
  #TODO: This is a hack
  rel.gsub(/s$/, '')
end