Class: FreeAgent::Collection
- Inherits:
-
Object
- Object
- FreeAgent::Collection
show all
- Includes:
- Enumerable
- Defined in:
- lib/free_agent.rb
Instance Method Summary
collapse
Constructor Details
#initialize(resource, options = {}) ⇒ Collection
Returns a new instance of Collection.
60
61
62
63
64
|
# File 'lib/free_agent.rb', line 60
def initialize(resource, options={})
@resource = resource
@entity = options.delete(:entity)
@entity_klass = "FreeAgent::#{@entity.to_s.classify}".constantize
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Treat the collection as if it’s an array otherwise
126
127
128
|
# File 'lib/free_agent.rb', line 126
def method_missing(method, *args, &block)
all.send(method, *args, &block)
end
|
Instance Method Details
#all(params = {}) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/free_agent.rb', line 82
def all(params={})
if params.any?
resource = @resource['?'+params.map{|k,v|"#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')]
else
resource = @resource
end
case (response = resource.get).code
when 200
if entities = Crack::XML.parse(response)[@entity.to_s.pluralize]
entities.map do |attributes|
entity_for_id(attributes['id'], attributes)
end
else
[]
end
end
end
|
#create(attributes) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/free_agent.rb', line 101
def create(attributes)
payload = attributes.to_xml(:root => @entity.to_s )
case (response = @resource.post(payload,
:content_type => 'application/xml', :accept => 'application/xml')).code
when 201
resource_path = URI.parse(response.[:location]).path
@entity_klass.new(@resource.root[resource_path]).reload
end
end
|
#destroy(id) ⇒ Object
115
116
117
|
# File 'lib/free_agent.rb', line 115
def destroy(id)
entity_for_id(id).destroy
end
|
#each(&block) ⇒ Object
78
79
80
|
# File 'lib/free_agent.rb', line 78
def each(&block)
all.each(&block)
end
|
#find(id = nil) ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/free_agent.rb', line 70
def find(id=nil)
if id
entity_for_id(id).reload
else
super
end
end
|
#update(id, attributes) ⇒ Object
111
112
113
|
# File 'lib/free_agent.rb', line 111
def update(id, attributes)
entity_for_id(id).update(attributes, )
end
|
#url ⇒ Object
66
67
68
|
# File 'lib/free_agent.rb', line 66
def url
@resource.url
end
|