Class: MonkeyWrench::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- MonkeyWrench::Base
- Includes:
- HTTParty
- Defined in:
- lib/monkey_wrench/base.rb
Constant Summary collapse
- @@apikey =
nil
- @@datacenter =
nil
- @@dryrun =
false
Class Method Summary collapse
- .apikey ⇒ Object
- .base_uri ⇒ Object
- .datacenter ⇒ Object
- .default_query_params ⇒ Object
- .default_retry_limit ⇒ Object
- .get(params, http_options = {}) ⇒ Object
- .handle_errors(objects) ⇒ Object
- .post(params, http_options = {}) ⇒ Object
Class Method Details
.apikey ⇒ Object
64 65 66 |
# File 'lib/monkey_wrench/base.rb', line 64 def apikey @@apikey end |
.base_uri ⇒ Object
15 16 17 |
# File 'lib/monkey_wrench/base.rb', line 15 def base_uri "http://#{datacenter}.api.mailchimp.com/1.2/" end |
.datacenter ⇒ Object
68 69 70 |
# File 'lib/monkey_wrench/base.rb', line 68 def datacenter @@datacenter end |
.default_query_params ⇒ Object
11 12 13 |
# File 'lib/monkey_wrench/base.rb', line 11 def default_query_params { :output => "json", :apikey=> @@apikey} end |
.default_retry_limit ⇒ Object
19 20 21 |
# File 'lib/monkey_wrench/base.rb', line 19 def default_retry_limit 3 end |
.get(params, http_options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/monkey_wrench/base.rb', line 23 def get(params, = {}) if @@dryrun puts "GET #{base_uri} #{params.merge(default_query_params).inspect}" return {} else robustly() do response = super(base_uri, .merge(:query => params.merge(default_query_params))) handle_errors(response.parsed_response) end end end |
.handle_errors(objects) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/monkey_wrench/base.rb', line 49 def handle_errors(objects) return objects unless objects.respond_to?(:has_key?) if objects.has_key?("error") objects.replace({ "error" => MonkeyWrench::Error.new(objects['error'], objects['code']) }) elsif objects.has_key?("errors") objects["errors"] = objects["errors"].map do |err| = err.delete('message') code = err.delete('code') MonkeyWrench::Error.new(, code, err) end end objects end |
.post(params, http_options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/monkey_wrench/base.rb', line 35 def post(params, = {}) if @@dryrun puts "POST #{base_uri} #{params.merge(default_query_params).inspect}" return {} else robustly() do post_params = params.dup get_params = default_query_params.merge(:method => post_params.delete(:method)) response = super(base_uri, .merge(:body => post_params, :query => get_params)) handle_errors(response.parsed_response) end end end |