Class: Howitzer::MailgunApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/howitzer/mailgun_api/client.rb

Overview

A Mailgun::Client object is used to communicate with the Mailgun API. It is a wrapper around RestClient so you don't have to worry about the HTTP aspect of communicating with Mailgun API.

Constant Summary collapse

USER_AGENT =

:nodoc:

'mailgun-sdk-ruby'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_user: 'api', api_key:, api_host: 'api.mailgun.net', api_version: 'v3', ssl: true) ⇒ Client

Returns a new instance of Client



14
15
16
17
18
19
20
21
# File 'lib/howitzer/mailgun_api/client.rb', line 14

def initialize(api_user: 'api', api_key:, api_host: 'api.mailgun.net', api_version: 'v3', ssl: true)
  @api_user = api_user
  @api_key = api_key
  @api_host = api_host
  @api_version = api_version
  @ssl = ssl
  @http_client = ::RestClient::Resource.new(endpoint, user: api_user, password: api_key, user_agent: USER_AGENT)
end

Instance Attribute Details

#api_hostObject (readonly)

Returns the value of attribute api_host



13
14
15
# File 'lib/howitzer/mailgun_api/client.rb', line 13

def api_host
  @api_host
end

#api_keyObject (readonly)

Returns the value of attribute api_key



13
14
15
# File 'lib/howitzer/mailgun_api/client.rb', line 13

def api_key
  @api_key
end

#api_userObject (readonly)

Returns the value of attribute api_user



13
14
15
# File 'lib/howitzer/mailgun_api/client.rb', line 13

def api_user
  @api_user
end

#api_versionObject (readonly)

Returns the value of attribute api_version



13
14
15
# File 'lib/howitzer/mailgun_api/client.rb', line 13

def api_version
  @api_version
end

#sslObject (readonly)

Returns the value of attribute ssl



13
14
15
# File 'lib/howitzer/mailgun_api/client.rb', line 13

def ssl
  @ssl
end

Instance Method Details

#get(resource_path, params: nil, accept: '*/*') ⇒ Mailgun::Response

Generic Mailgun GET Handler

Parameters:

  • resource_path (String)

    this is the API resource you wish to interact with. Be sure to include your domain, where it is necessary.

  • params (Hash)

    this should be a standard Hash for query containing required parameters for the requested resource.

Returns:

  • (Mailgun::Response)

    a Mailgun::Response object.

Raises:



32
33
34
35
36
37
38
39
# File 'lib/howitzer/mailgun_api/client.rb', line 32

def get(resource_path, params: nil, accept: '*/*')
  http_params = { accept: accept }
  http_params = http_params.merge(params: params) if params
  response = http_client[resource_path].get(http_params)
  Response.new(response)
rescue => e
  raise Howitzer::CommunicationError, e.message
end

#get_url(resource_url, params: nil, accept: '*/*') ⇒ Mailgun::Response

Note:

This method was introducted because of saving emails to different nodes. As result we can not use #get method, because client holds general api url

Extracts data by url in custom way

Parameters:

  • resource_url (String)

    a full url

  • params (Hash)

    this should be a standard Hash for query containing required parameters for the requested resource.

  • accept (String)

    an accept pattern for headers

Returns:

  • (Mailgun::Response)

    a Mailgun::Response object.

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/howitzer/mailgun_api/client.rb', line 52

def get_url(resource_url, params: nil, accept: '*/*')
  response = ::RestClient::Request.execute(
    method: :get,
    url: resource_url,
    user: api_user,
    password: api_key,
    user_agent: USER_AGENT,
    accept: accept,
    params: params
  )
  Response.new(response)
rescue => e
  raise Howitzer::CommunicationError, e.message
end