Module: MagnumPI::API::Consumer
- Defined in:
- lib/magnum-pi/api/consumer.rb
Defined Under Namespace
Classes: Error
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
46
47
48
|
# File 'lib/magnum-pi/api/consumer.rb', line 46
def method_missing(name, *args)
resources[name.to_sym] ? resource(name, *args) : super
end
|
Instance Method Details
#download(target, method, *args) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/magnum-pi/api/consumer.rb', line 16
def download(target, method, *args)
url, params = parse_args *args
File.delete target if File.exists? target
FileUtils.mkdir_p File.dirname(target)
file = File.open target, "w"
begin
options = {:method => method, :headers => {"User-Agent" => agent.user_agent}.merge((method, url, params))}
if method.to_s.downcase == "get"
options[:params] = params
else
options[:body] = params
end
request = Typhoeus::Request.new url, options
request. {|response| raise "Failed to download #{target}" unless (response.response_code / 200) == 1}
request.on_body {|chunk| file.write chunk.force_encoding("UTF-8")}
request.run
ensure
file.close
end
true
end
|
#get(*args) ⇒ Object
6
7
8
9
|
# File 'lib/magnum-pi/api/consumer.rb', line 6
def get(*args)
url, params = parse_args *args
parse_content request(:get, url, params).content, :get, *args
end
|
#post(*args) ⇒ Object
11
12
13
14
|
# File 'lib/magnum-pi/api/consumer.rb', line 11
def post(*args)
url, params = parse_args *args
parse_content request(:post, url, params).content, :post, *args
end
|
#resource(name, variables = {}, save_as = nil) ⇒ Object
38
39
40
41
42
|
# File 'lib/magnum-pi/api/consumer.rb', line 38
def resource(name, variables = {}, save_as = nil)
args = parse_resource_variables(resources[name], variables)
args = [:download, save_as].concat args if save_as
send *args
end
|