Class: Maileon::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey = nil, debug = false) ⇒ API

Returns a new instance of API.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/maileon/api.rb', line 6

def initialize(apikey=nil, debug=false)
  @host = 'https://api.maileon.com'
  @path = '/1.0/'

  unless apikey
    apikey = ENV['MAILEON_APIKEY']
  end

  raise 'You must provide Maileon API key' unless apikey

  @apikey = Base64.encode64(apikey).strip
  @debug = debug
  @session = Excon.new @host, :debug => debug
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



4
5
6
# File 'lib/maileon/api.rb', line 4

def apikey
  @apikey
end

#debugObject

Returns the value of attribute debug.



4
5
6
# File 'lib/maileon/api.rb', line 4

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/maileon/api.rb', line 4

def host
  @host
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/maileon/api.rb', line 4

def path
  @path
end

#sessionObject

Returns the value of attribute session.



4
5
6
# File 'lib/maileon/api.rb', line 4

def session
  @session
end

Instance Method Details

#create_contact(params, body = {}) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/maileon/api.rb', line 25

def create_contact(params, body={})
  raise ArgumentError.new("No parameters.") if params.empty?
  raise ArgumentError.new("Email is mandatory.") if params[:email].nil?
  raise Maileon::Errors::ValidationError.new("Invalid email format.") unless is_valid_email(params[:email])
  email = URI::escape(params[:email])
  permission = params[:permission] ||= 1
  sync_mode = params[:sync_mode] ||= 2
  src = params[:src]
  subscription_page = params[:subscription_page]
  doi = params[:doi] ||= true
  doiplus = params[:doiplus] ||= true
  doimailing = params[:doimailing]
  url = "contacts/#{email}?permission=#{permission}&sync_mode=#{sync_mode}&doi=#{doi}&doiplus=#{doiplus}"
  url << "&doimailing=#{doimailing}" unless doimailing.nil?
  url << "&src=#{src}" unless src.nil?
  url << "&subscription_page=#{subscription_page}" unless subscription_page.nil?
  @session.post(:path => "#{@path}#{url}", :headers => get_headers, :body => body.to_json)
end

#delete_contact(params) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
# File 'lib/maileon/api.rb', line 44

def delete_contact(params)
  raise ArgumentError.new("No parameters.") if params.empty?
  raise ArgumentError.new("Email is mandatory.") if params[:email].nil?
  raise Maileon::Errors::ValidationError.new("Invalid email format.") unless is_valid_email(params[:email])
  email = URI::escape(params[:email])
  url = "contacts/#{email}"
  @session.delete(:path => "#{@path}#{url}", :headers => get_headers('xml'))
end

#pingObject



21
22
23
# File 'lib/maileon/api.rb', line 21

def ping
  @session.get(:path => "#{@path}/ping", :headers => get_headers)
end