Class: LetterMX::API
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
- #add_subscriber(subscriber = {}) ⇒ Object
- #count_subscribers(options = {}) ⇒ Object
- #get_subscriber(id, options = {}) ⇒ Object
-
#initialize(username, password, type = LetterMX::API::CONTENT_TYPES[:json]) ⇒ API
constructor
A new instance of API.
- #list_subscribers(options = {}) ⇒ Object
- #remove_subscriber(id) ⇒ Object
- #subscribers_api_limit_default(options = {}) ⇒ Object
- #subscribers_api_limit_max(options = {}) ⇒ Object
- #update_subscriber(id, subscriber = {}) ⇒ Object
Constructor Details
#initialize(username, password, type = LetterMX::API::CONTENT_TYPES[:json]) ⇒ API
Returns a new instance of API.
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={}) = @default_options.clone .merge!(body: build_request_body({ contact: subscriber })) LetterMX::Subscriber.new(self.class.post("/contacts", )) end |
#count_subscribers(options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/letter_mx/api.rb', line 65 def count_subscribers(={}) = @default_options.merge() LetterMX::Subscriber.new(self.class.get("/contacts/count", )) end |
#get_subscriber(id, options = {}) ⇒ Object
76 77 78 79 |
# File 'lib/letter_mx/api.rb', line 76 def get_subscriber(id, ={}) = @default_options.merge() LetterMX::Subscriber.new(self.class.get("/contacts/#{id}", )) end |
#list_subscribers(options = {}) ⇒ Object
60 61 62 63 |
# File 'lib/letter_mx/api.rb', line 60 def list_subscribers(={}) = @default_options.merge() LetterMX::Subscriber.new(self.class.get("/contacts", )) 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(={}) = @default_options.merge() LetterMX::Subscriber.new(self.class.get("/contacts/limit-default", )) end |
#subscribers_api_limit_max(options = {}) ⇒ Object
96 97 98 99 |
# File 'lib/letter_mx/api.rb', line 96 def subscribers_api_limit_max(={}) = @default_options.merge() LetterMX::Subscriber.new(self.class.get("/contacts/limit-max", )) end |
#update_subscriber(id, subscriber = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/letter_mx/api.rb', line 81 def update_subscriber(id, subscriber={}) = @default_options.clone .merge!(body: build_request_body({ contact: subscriber })) LetterMX::Subscriber.new(self.class.put("/contacts/#{id}", )) end |