Class: Zipmark::Resource
- Inherits:
-
Object
- Object
- Zipmark::Resource
- Defined in:
- lib/zipmark/resource.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #all ⇒ Object
- #build(options) ⇒ Object
- #client ⇒ Object
- #create(options) ⇒ Object
- #find(id) ⇒ Object
- #href ⇒ Object
-
#initialize(options = {}) ⇒ Resource
constructor
A new instance of Resource.
- #rel ⇒ Object
- #resource_name ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Resource
Returns a new instance of Resource.
5 6 7 |
# File 'lib/zipmark/resource.rb', line 5 def initialize(={}) @options = Util.stringify_keys() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/zipmark/resource.rb', line 3 def @options end |
Instance Method Details
#all ⇒ Object
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() Zipmark::Entity.new(.merge(:client => client, :resource_type => resource_name)) end |
#client ⇒ Object
45 46 47 |
# File 'lib/zipmark/resource.rb', line 45 def client ["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() entity = build() 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 |
#href ⇒ Object
32 33 34 |
# File 'lib/zipmark/resource.rb', line 32 def href ["href"] || raise(Zipmark::ResourceError, "Resource did not specify href") end |
#rel ⇒ Object
36 37 38 |
# File 'lib/zipmark/resource.rb', line 36 def rel ["rel"] || raise(Zipmark::ResourceError, "Resource did not specify rel") end |
#resource_name ⇒ Object
40 41 42 43 |
# File 'lib/zipmark/resource.rb', line 40 def resource_name #TODO: This is a hack rel.gsub(/s$/, '') end |