Class: LetterMX::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/letter_mx/api.rb

Overview

connection = LetterMX::API.new( username: ‘the_app_key’, password: ‘the_api_key’ )

connection.add_subscriber( {
                              email: '[email protected]',
                              first_name: 'John',
                              last_name: 'Doe',
                              optional_1: 'Customer',
                              optional_2: 'Active',
                              optional_3: 'Male',
                              optin: false
                          } )

Defined Under Namespace

Classes: ApiException

Constant Summary collapse

CONTENT_TYPES =
{
  json: "json",
  # TODO: Implement XML
  # xml: "xml"
}

Instance Method Summary collapse

Constructor Details

#initialize(username, password, type = LetterMX::API::CONTENT_TYPES[:json]) ⇒ API

Returns a new instance of API.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/letter_mx/api.rb', line 32

def initialize(username, password, type=LetterMX::API::CONTENT_TYPES[:json])
  raise ArgumentError, "First argument (username) must not be blank." if username.nil? || username.size == 0
  raise ArgumentError, "Second argument (password) must not be blank." if password.nil? || password.size == 0
  raise ArgumentError, "Third argument (type) must be on of #{LetterMX::API::CONTENT_TYPES.values}." unless LetterMX::API::CONTENT_TYPES.has_value?(type)

  @auth = { username: username, password: password }
  @type = type
  @default_options = { basic_auth: @auth }

  case @type
  when LetterMX::API::CONTENT_TYPES[:json]
    @default_options.merge!({
      headers: {
        "Accept" => "application/json",
        "Content-Type" => "application/json; charset=utf-8"
      }
    })
  # TODO: Implement XML
  # when LetterMX::API::CONTENT_TYPES[:xml]
  #   @default_options.merge!({
  #     headers: {
  #       "Accept" => "application/xml",
  #       "Content-Type" => "application/xml"
  #     }
  #   })
  end
end

Instance Method Details

#add_subscriber(subscriber = {}) ⇒ Object



70
71
72
73
74
# File 'lib/letter_mx/api.rb', line 70

def add_subscriber(subscriber={})
  options = @default_options.clone
  options.merge!(body: build_request_body({ contact: subscriber }))
  LetterMX::Subscriber.new(self.class.post("/contacts", options))
end

#count_subscribers(options = {}) ⇒ Object



65
66
67
68
# File 'lib/letter_mx/api.rb', line 65

def count_subscribers(options={})
  options = @default_options.merge(options)
  LetterMX::Subscriber.new(self.class.get("/contacts/count", options))
end

#get_subscriber(id, options = {}) ⇒ Object



76
77
78
79
# File 'lib/letter_mx/api.rb', line 76

def get_subscriber(id, options={})
  options = @default_options.merge(options)
  LetterMX::Subscriber.new(self.class.get("/contacts/#{id}", options))
end

#list_subscribers(options = {}) ⇒ Object



60
61
62
63
# File 'lib/letter_mx/api.rb', line 60

def list_subscribers(options={})
  options = @default_options.merge(options)
  LetterMX::Subscriber.new(self.class.get("/contacts", options))
end

#remove_subscriber(id) ⇒ Object



87
88
89
# File 'lib/letter_mx/api.rb', line 87

def remove_subscriber(id)
  LetterMX::Subscriber.new(self.class.delete("/contacts/#{id}", @default_options))
end

#subscribers_api_limit_default(options = {}) ⇒ Object



91
92
93
94
# File 'lib/letter_mx/api.rb', line 91

def subscribers_api_limit_default(options={})
  options = @default_options.merge(options)
  LetterMX::Subscriber.new(self.class.get("/contacts/limit-default", options))
end

#subscribers_api_limit_max(options = {}) ⇒ Object



96
97
98
99
# File 'lib/letter_mx/api.rb', line 96

def subscribers_api_limit_max(options={})
  options = @default_options.merge(options)
  LetterMX::Subscriber.new(self.class.get("/contacts/limit-max", options))
end

#update_subscriber(id, subscriber = {}) ⇒ Object



81
82
83
84
85
# File 'lib/letter_mx/api.rb', line 81

def update_subscriber(id, subscriber={})
  options = @default_options.clone
  options.merge!(body: build_request_body({ contact: subscriber }))
  LetterMX::Subscriber.new(self.class.put("/contacts/#{id}", options))
end