Class: Pige::Client::Resource
- Inherits:
-
Object
- Object
- Pige::Client::Resource
- Includes:
- HTTParty
- Defined in:
- lib/pige/client/resource.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#id ⇒ Object
Returns the value of attribute id.
-
#source_id ⇒ Object
Returns the value of attribute source_id.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
- .all ⇒ Object
- .base_url ⇒ Object
- .create(attributes = {}) ⇒ Object
- .destroy(resource) ⇒ Object
- .destroy_all(conditions = {}) ⇒ Object
- .older_than(date) ⇒ Object
- .resource_name ⇒ Object
- .resources_name ⇒ Object
- .time_attribute(*names) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
- #older_than?(date) ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Resource
8 9 10 |
# File 'lib/pige/client/resource.rb', line 8 def initialize(attributes = {}) attributes.each { |k,v| send "#{k}=", v } end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
21 22 23 |
# File 'lib/pige/client/resource.rb', line 21 def created_at @created_at end |
#errors ⇒ Object
Returns the value of attribute errors.
24 25 26 |
# File 'lib/pige/client/resource.rb', line 24 def errors @errors end |
#id ⇒ Object
Returns the value of attribute id.
29 30 31 |
# File 'lib/pige/client/resource.rb', line 29 def id @id end |
#source_id ⇒ Object
Returns the value of attribute source_id.
29 30 31 |
# File 'lib/pige/client/resource.rb', line 29 def source_id @source_id end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
21 22 23 |
# File 'lib/pige/client/resource.rb', line 21 def updated_at @updated_at end |
Class Method Details
.all ⇒ Object
56 57 58 59 60 |
# File 'lib/pige/client/resource.rb', line 56 def self.all get("#{Pige::Client.box_url}/sources/1/#{resources_name}.json").map do |attributes| new attributes[resource_name] end end |
.base_url ⇒ Object
47 48 49 |
# File 'lib/pige/client/resource.rb', line 47 def self.base_url "#{Pige::Client.box_url}/sources/1/#{resource_name}s" end |
.create(attributes = {}) ⇒ Object
51 52 53 54 |
# File 'lib/pige/client/resource.rb', line 51 def self.create(attributes = {}) response = post("#{base_url}.json", :query => { resource_name => attributes }) new response end |
.destroy(resource) ⇒ Object
79 80 81 |
# File 'lib/pige/client/resource.rb', line 79 def self.destroy(resource) delete "#{base_url}/#{resource.id}.json" end |
.destroy_all(conditions = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pige/client/resource.rb', line 68 def self.destroy_all(conditions = {}) resources = if older_than = conditions[:older_than] older_than(older_than) else all end resources.each(&:destroy) end |
.older_than(date) ⇒ Object
62 63 64 65 66 |
# File 'lib/pige/client/resource.rb', line 62 def self.older_than(date) all.select do |resource| resource.older_than?(date) end end |
.resource_name ⇒ Object
39 40 41 |
# File 'lib/pige/client/resource.rb', line 39 def self.resource_name name.downcase.split("::").last end |
.resources_name ⇒ Object
43 44 45 |
# File 'lib/pige/client/resource.rb', line 43 def self.resources_name "#{resource_name}s" end |
.time_attribute(*names) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/pige/client/resource.rb', line 12 def self.time_attribute(*names) names.each do |time_attribute| define_method("#{time_attribute}=") do |value| value = Time.parse(value) if String === value instance_variable_set "@#{time_attribute}", value end end end |
Instance Method Details
#destroy ⇒ Object
35 36 37 |
# File 'lib/pige/client/resource.rb', line 35 def destroy self.class.destroy(self) end |
#older_than?(date) ⇒ Boolean
31 32 33 |
# File 'lib/pige/client/resource.rb', line 31 def older_than?(date) self.created_at < date end |