Class: MailchimpTransactional::ApiClient
- Inherits:
-
Object
- Object
- MailchimpTransactional::ApiClient
- Defined in:
- lib/MailchimpTransactional/api_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call_api(http_method, path, body = {}) ⇒ Object
-
#initialize(api_key = '') ⇒ ApiClient
constructor
A new instance of ApiClient.
- #set_api_key(api_key = '') ⇒ Object
- #set_default_output_format(output_format) ⇒ Object
Constructor Details
#initialize(api_key = '') ⇒ ApiClient
Returns a new instance of ApiClient.
18 19 20 21 22 23 24 |
# File 'lib/MailchimpTransactional/api_client.rb', line 18 def initialize(api_key = '') @host = "https://mandrillapp.com/api/1.0" @format_list = ['json', 'xml', 'php', 'yaml'] @default_output_format = 'json' @accepts = ['application/json', 'application/xml', 'application/x-php', 'application/x-yaml; charset=utf-8'] set_api_key(api_key) end |
Class Method Details
.default ⇒ Object
26 27 28 |
# File 'lib/MailchimpTransactional/api_client.rb', line 26 def self.default @@default ||= ApiClient.new end |
Instance Method Details
#call_api(http_method, path, body = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/MailchimpTransactional/api_client.rb', line 40 def call_api(http_method, path, body = {}) use_default_output_format = true active_output_format = @default_output_format url = @host + path # format body if body[:outputFormat] format = body[:outputFormat].downcase if @format_list.include? format url += ".#{format}" body.delete(:outputFormat) use_default_output_format = false active_output_format = format end end # apply output format if use_default_output_format && @format_list.include?(@default_output_format) url += '.%s' % @default_output_format active_output_format = @default_output_format end # apply api key body[:key] = @api_key # send request conn = Excon.new(url, :headers => {'Content-Type' => 'application/json'}, :read_timeout => 300, :write_timeout => 300) res = conn.post(:body => body.to_json) # handle response data = nil if res.status == 200 if active_output_format == 'json' data = JSON.parse(res.body) else data = res.body end end if (!data) fail ApiError.new(:status => res.status, :response_body => res.body) end return data end |
#set_api_key(api_key = '') ⇒ Object
30 31 32 |
# File 'lib/MailchimpTransactional/api_client.rb', line 30 def set_api_key(api_key = '') @api_key = api_key end |
#set_default_output_format(output_format) ⇒ Object
34 35 36 37 38 |
# File 'lib/MailchimpTransactional/api_client.rb', line 34 def set_default_output_format(output_format) if @format_list.include? output_format @default_output_format = output_format end end |