Class: Maileon::API
- Inherits:
-
Object
- Object
- Maileon::API
- Defined in:
- lib/maileon/api.rb
Instance Attribute Summary collapse
-
#apikey ⇒ Object
Returns the value of attribute apikey.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #create_contact(params, body = {}) ⇒ Object
- #delete_contact(params) ⇒ Object
-
#initialize(apikey = nil, debug = false) ⇒ API
constructor
A new instance of API.
- #ping ⇒ Object
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
#apikey ⇒ Object
Returns the value of attribute apikey.
4 5 6 |
# File 'lib/maileon/api.rb', line 4 def apikey @apikey end |
#debug ⇒ Object
Returns the value of attribute debug.
4 5 6 |
# File 'lib/maileon/api.rb', line 4 def debug @debug end |
#host ⇒ Object
Returns the value of attribute host.
4 5 6 |
# File 'lib/maileon/api.rb', line 4 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/maileon/api.rb', line 4 def path @path end |
#session ⇒ Object
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
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]) = 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=#{}&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
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 |
#ping ⇒ Object
21 22 23 |
# File 'lib/maileon/api.rb', line 21 def ping @session.get(:path => "#{@path}/ping", :headers => get_headers) end |