Class: Mandrill::API

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill/api.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

API_VERSION =

Mandrill API Documentation: mandrillapp.com/api/docs

"1.0"
API_URL =
"https://mandrillapp.com/api"
AUTH_URL =
"https://mandrillapp.com/api-auth/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config = {}) ⇒ API

Initialize



35
36
37
38
39
40
41
42
# File 'lib/mandrill/api.rb', line 35

def initialize(api_key, config = {})
  defaults = {
    :api_version => API_VERSION,
    :format => 'json'
  }
  @config = defaults.merge(config).freeze
  @api_key = api_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(api_method, *args) ⇒ Object

Dynamically find API methods



45
46
47
48
49
50
51
52
53
# File 'lib/mandrill/api.rb', line 45

def method_missing(api_method, *args) # :nodoc:
  call(api_method, *args)
  if @response.code.to_i == 200
    return "PONG!" if @response.body == "\"PONG!\""
    @config[:format] == 'json' ? JSON.parse(@response.body) : @response.body
  else
    raise(API::Error.new(JSON.parse(@response.body)["code"], JSON.parse(@response.body)["message"]))
  end
end

Class Method Details

.authorization_url(app_id, redirect_url) ⇒ Object

Generate a Mandrill authorization_url. Returns a URL to redirect users to so that they will be prompted to enter their Mandrill username and password to authorize a connection between your application and their Mandrill account.

If authorized successfully, a POST request will be sent to the redirect_url with a “key” parameter containing the API key for that user’s Mandrill account. Be sure to store this key somewhere, as you will need it to run API requests later.

If authorization fails for some reason, an “error” parameter will be present in the POST request, containing an error message.

Example

redirect_to Mandrill::API.authorization_url(“12345”,“example.com/callback”)



30
31
32
# File 'lib/mandrill/api.rb', line 30

def self.authorization_url(app_id, redirect_url)
  "#{AUTH_URL}?id=#{app_id}&redirect_url=#{URI.escape(redirect_url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
end

Instance Method Details

#call(api_method, *args) ⇒ Object

Call the API



67
68
69
70
71
72
73
74
75
76
# File 'lib/mandrill/api.rb', line 67

def call(api_method, *args)
  req_endpoint = "#{API_URL}/#{@config[:api_version]}/#{api_method.to_s}/#{args.first.to_s}.#{@config[:format]}"
  req_body = {:key => @api_key}
  req_body.merge!(args.last) if args.last.is_a?(Hash)
  #ensure request is in json format if required
  if @config[:format] == "json"
    req_body = req_body.to_json
  end
  @response = HTTPI.post(req_endpoint, req_body.to_json.to_s)
end

#public_methodsObject

Display the supported methods



62
63
64
# File 'lib/mandrill/api.rb', line 62

def public_methods # :nodoc:
  [:messages, :senders, :tags, :templates, :urls, :users]
end

#respond_to?(api_method, *args) ⇒ Boolean

Check the API to see if a method is supported

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/mandrill/api.rb', line 56

def respond_to?(api_method, *args) # :nodoc:
  call(api_method, *args)
  @response.code == 500 ? false : true
end