maropost-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ class OrderItem
2
+
3
+ attr_accessor :item_id, :price, :quantity, :description, :adcode, :category
4
+
5
+ def initialize(item_id, price, quantity, description, adcode, category)
6
+ @item_id = item_id
7
+ @price = price
8
+ @quantity = quantity
9
+ @description = description
10
+ @adcode = adcode
11
+ @category = category
12
+ end
13
+
14
+ def to_hash
15
+ {
16
+ item_id: @item_id,
17
+ price: @price,
18
+ quantity: @quantity,
19
+ description: @description,
20
+ adcode: @adcode,
21
+ category: @category
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,137 @@
1
+ module MaropostApi
2
+ ##
3
+ # Contains methods that get journey contacts based on provided parameters.
4
+ # The method names themselves reveal the type of reports they are getting.
5
+ class Journeys
6
+ ##
7
+ # Creates a new instance of Reports class.
8
+ # @param account [Integer] is authentic user account id (Integer) provided by maropost.com
9
+ # @param api_key [String] is the auth token (String) that is validated on the server to authenticate the user
10
+ def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
11
+ MaropostApi.instance_variable_set(:@api_key, api_key)
12
+ MaropostApi.instance_variable_set(:@account, account)
13
+ end
14
+
15
+ ##
16
+ # gets all the journey contacts grouped by
17
+ # @param page [Integer] number that decides which page of result to retrieve
18
+ def get(page)
19
+ full_path = full_resource_path
20
+ query_params = MaropostApi.set_query_params({:page => page})
21
+
22
+ MaropostApi.get_result(full_path, query_params)
23
+ end
24
+
25
+ ##
26
+ # gets all campaigns for the provided
27
+ # @param journey_id [Integer] Unique id of Journey
28
+ # @param page [Integer] number that decides which page of result to retrieve
29
+ def get_campaigns(journey_id, page)
30
+ full_path = full_resource_path("/#{journey_id}/journey_campaigns")
31
+ query_params = MaropostApi.set_query_params({:pae => page})
32
+
33
+ MaropostApi.get_result(full_path, query_params)
34
+ end
35
+
36
+ ##
37
+ # gets contacts for the provided
38
+ # @param journey_id [Integer] Unique id of Journey
39
+ # @param page [Integer] number that decides which page of result to retrieve
40
+ def get_contacts(journey_id, page)
41
+ full_path = full_resource_path("/#{journey_id}/journey_contacts")
42
+ query_params = MaropostApi.set_query_params({:pae => page})
43
+
44
+ MaropostApi.get_result(full_path, query_params)
45
+ end
46
+
47
+ ##
48
+ # stops all journeys for the given
49
+ # @param contact_id [Integer] Unique id of Contact
50
+ # @param uid [String] Unique ID
51
+ # @param email_recipient [String] Email
52
+ def stop_all(contact_id: nil, uid: nil, email_recipient: nil)
53
+ query_params = {}
54
+ query_params[:contact_id] = contact_id unless contact_id.nil?
55
+ query_params[:uid] = uid unless uid.nil?
56
+ query_params[:email] = email_recipient unless email_recipient.nil?
57
+ full_path = full_resource_path "/stop_all_journeys"
58
+
59
+ MaropostApi.put_result(full_path, {}, MaropostApi.set_query_params(query_params))
60
+
61
+ end
62
+
63
+ ##
64
+ # pauses journey for for the provided
65
+ # @param journey_id [Integer] Unique id of Journey
66
+ # @param contact_id [Integer] Unique id of Contact
67
+ def pause_for_contact(journey_id, contact_id)
68
+ full_path = full_resource_path "/#{journey_id}/stop/#{contact_id}"
69
+ query_params = MaropostApi.set_query_params
70
+
71
+ MaropostApi.put_result(full_path, {}, query_params)
72
+ end
73
+
74
+ ##
75
+ # pauses journey for the given
76
+ # @param jouerney_id [Integer] Unique Journey ID
77
+ # @param uid [String] Unique Identifier
78
+ def pause_for_uid(journey_id, uid)
79
+ full_path = full_resource_path "/#{journey_id}/stop/uid"
80
+ query_params = MaropostApi.set_query_params({:uid => uid})
81
+
82
+ MaropostApi.put_result(full_path, {}, query_params)
83
+ end
84
+
85
+ ##
86
+ # resets journey for the given contact
87
+ # @param journey_id [Integer] Unique id of Journey
88
+ # @param contact_id [Integer] Unique id of Contact
89
+ def reset_for_contact(journey_id, contact_id)
90
+ full_path = full_resource_path "/#{journey_id}/reset/#{contact_id}"
91
+ query_params = MaropostApi.set_query_params
92
+
93
+ MaropostApi.put_result(full_path, {}, query_params)
94
+ end
95
+
96
+ ##
97
+ # resets journey for the given uid
98
+ # @param jouerney_id [Integer] Unique Journey ID
99
+ # @param uid [String] Unique Identifier
100
+ def reset_for_uid(journey_id, uid)
101
+ full_path = full_resource_path "/#{journey_id}/reset/uid"
102
+ query_params = MaropostApi.set_query_params({:uid => uid})
103
+
104
+ MaropostApi.put_result(full_path, {}, query_params)
105
+ end
106
+
107
+ ##
108
+ # starts journey for the given contact
109
+ # @param jouerney_id [Integer] Unique Journey ID
110
+ # @param contact_id [Integer] Unique id of Contact
111
+ def start_for_contact(journey_id, contact_id)
112
+ full_path = full_resource_path "/#{journey_id}/start/#{contact_id}"
113
+ query_params = MaropostApi.set_query_params
114
+
115
+ MaropostApi.put_result(full_path, {}, query_params)
116
+ end
117
+
118
+ ##
119
+ # starts journey for the given uid
120
+ # @param jouerney_id [Integer] Unique Journey ID
121
+ # @param uid [String] Unique Identifier
122
+ def start_for_uid(journey_id, uid)
123
+ full_path = full_resource_path "/#{journey_id}/start/uid"
124
+ query_params = MaropostApi.set_query_params({:uid => uid})
125
+
126
+ MaropostApi.put_result(full_path, {}, query_params)
127
+ end
128
+
129
+
130
+ private
131
+
132
+ def full_resource_path(specifics = '', root_resource = "journeys")
133
+ account = MaropostApi.instance_variable_get(:@account)
134
+ "/accounts/#{account}/#{root_resource}" << specifics
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,140 @@
1
+ require 'maropost_api/custom_types/order_item'
2
+
3
+ module MaropostApi
4
+
5
+ class ProductsAndRevenue
6
+
7
+ def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
8
+ MaropostApi.instance_variable_set(:@api_key, api_key)
9
+ MaropostApi.instance_variable_set(:@account, account)
10
+ end
11
+
12
+ def get_order(id)
13
+ full_path = full_resource_path("/find")
14
+ query_params = MaropostApi.set_query_params({"where[id]" => id})
15
+
16
+ MaropostApi.get_result(full_path, query_params)
17
+ end
18
+
19
+ def get_order_for_original_order_id(original_order_id)
20
+ full_path = full_resource_path("/#{original_order_id}")
21
+ query_params = MaropostApi.set_query_params
22
+
23
+ MaropostApi.get_result(full_path, query_params)
24
+ end
25
+
26
+ def create_order(
27
+ require_unique,
28
+ contact: {},
29
+ order: {},
30
+ order_items: [],
31
+ add_tags: [],
32
+ remove_tags: [],
33
+ uid: nil,
34
+ list_ids: nil,
35
+ grand_total: nil,
36
+ campaign_id: nil,
37
+ coupon_code: nil
38
+ )
39
+ # required contacts fields to check for
40
+ [:email,].each do |contact_field|
41
+ raise ArgumentError.new "contact[:#{contact_field}] is required!" if contact.has_key?(contact_field.to_sym) == false
42
+ end
43
+ # order items validation
44
+ order_items.each do |item|
45
+ raise TypeError.new("Each order item must be an instance of maropost_api/custom_types/OrderItem: " << item.class.to_s << " given") unless item.kind_of?(OrderItem)
46
+ order[:order_items] ||= []
47
+ order[:order_items].push(item.to_hash)
48
+ end
49
+ # required order fields to check for
50
+ [:order_date, :order_status, :original_order_id, :order_items].each do |order_field|
51
+ raise ArgumentError.new "order[:#{order_field}] is required!" if order.has_key?(order_field) == false
52
+ end
53
+
54
+ params = order
55
+ params[:contact] = contact
56
+ params[:uid] = uid
57
+ params[:list_ids] = list_ids
58
+ params[:grand_total] = grand_total
59
+ params[:campaign_id] = campaign_id
60
+ params[:coupon_code] = coupon_code
61
+
62
+ full_path = full_resource_path
63
+
64
+ MaropostApi.post_result(full_path, {"order" => params})
65
+ end
66
+
67
+ def update_order_for_original_order_id(original_order_id, order: {})
68
+ raise ArgumentError.new('original_order_id is required') if original_order_id.nil?
69
+
70
+ [:order_date, :order_status, :order_items].each do |f|
71
+ raise ArgumentError.new("order[:#{f}] is required") unless order.has_key?(f)
72
+ end
73
+ order[:order_items].each do |oi|
74
+ raise TypeError.new('each order item should be a type of ' << OrderItem.class.to_s) unless oi.kind_of? OrderItem
75
+ end
76
+ order[:order_items].map! {|i| i.to_hash }
77
+
78
+ full_path = full_resource_path("/#{original_order_id}")
79
+ form_body = {order: order}
80
+
81
+ MaropostApi.put_result(full_path, form_body)
82
+ end
83
+
84
+ def update_order_for_order_id(order_id, order: {})
85
+ raise ArgumentError.new('order_id is required') if order_id.nil?
86
+
87
+ [:order_date, :order_status, :order_items].each do |f|
88
+ raise ArgumentError.new("order[:#{f}] is required") unless order.has_key?(f)
89
+ end
90
+ order[:order_items].each do |oi|
91
+ raise TypeError.new('each order item should be a type of ' << OrderItem.class.to_s) unless oi.kind_of? OrderItem
92
+ end
93
+ order[:order_items].map! {|i| i.to_hash }
94
+
95
+ full_path = full_resource_path("/find")
96
+ query_params = MaropostApi.set_query_params({"where[id]" => order_id})
97
+
98
+ MaropostApi.put_result(full_path, {order: order}, query_params)
99
+ end
100
+
101
+ def delete_for_original_order_id(original_order_id)
102
+ full_path = full_resource_path("/#{original_order_id}")
103
+ query_params = MaropostApi.set_query_params
104
+
105
+ MaropostApi.delete_result(full_path, query_params)
106
+ end
107
+
108
+ def delete_for_order_id(order_id)
109
+ full_path = full_resource_path('/find')
110
+ query_params = MaropostApi.set_query_params({'where[id]' => order_id})
111
+
112
+ MaropostApi.delete_result(full_path, query_params)
113
+ end
114
+
115
+ def delete_products_for_original_order_id(original_order_id, product_ids)
116
+ raise TypeError.new('product_ids should be an Array') if ! product_ids.kind_of? Array
117
+ full_path = full_resource_path("/#{original_order_id}")
118
+ query_params = MaropostApi.set_query_params({"product_ids" => product_ids.join(',')})
119
+
120
+ MaropostApi.delete_result(full_path, query_params)
121
+ end
122
+
123
+ def delete_products_for_order_id(order_id, product_ids)
124
+ raise TypeError.new('product_ids should be an Array') if ! product_ids.kind_of? Array
125
+ full_path = full_resource_path("/find")
126
+ query_params = MaropostApi.set_query_params({"product_ids" => product_ids.join(','), 'where[id]' => order_id})
127
+
128
+ MaropostApi.delete_result(full_path, query_params)
129
+ end
130
+
131
+ private
132
+
133
+ def full_resource_path(specifics = '', root_resource = 'orders')
134
+ account = MaropostApi.instance_variable_get(:@account)
135
+ "/accounts/#{account}/#{root_resource}" << specifics
136
+ end
137
+
138
+ end
139
+
140
+ end
@@ -0,0 +1,91 @@
1
+ module MaropostApi
2
+ class RelationalTables
3
+
4
+ attr_accessor :table_name
5
+
6
+ def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:)
7
+ MaropostApi.instance_variable_set(:@api_key, api_key)
8
+ MaropostApi.instance_variable_set(:@account, account)
9
+ @table_name = table_name
10
+
11
+ MaropostApi.base_uri "https://rdb.maropost.com/#{account}"
12
+ end
13
+
14
+ def get
15
+ full_path = full_resource_path
16
+ query_params = MaropostApi.set_query_params
17
+
18
+ MaropostApi.get_result(full_path, query_params)
19
+ end
20
+
21
+ def show(unique_field_name, value)
22
+ raise TypeError.new("#{unique_field_name.inspect} should be a string") unless unique_field_name.is_a? String
23
+ params = {:record => {}}
24
+ params[:record][unique_field_name.to_sym] = value
25
+ full_path = full_resource_path '/show'
26
+
27
+ MaropostApi.post_result(full_path, params)
28
+ end
29
+
30
+ def create(key_value_col, *key_value_cols)
31
+ raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
32
+ if (key_value_cols)
33
+ invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
34
+ raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
35
+ end
36
+ full_path = full_resource_path '/create'
37
+ all_key_values = [key_value_col] + key_value_cols
38
+
39
+ MaropostApi.post_result(full_path, :record => all_key_values)
40
+ end
41
+
42
+ def update(key_value_col, *key_value_cols)
43
+ raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
44
+ if (key_value_cols)
45
+ invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
46
+ raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
47
+ end
48
+ full_path = full_resource_path '/update'
49
+ all_key_values = [key_value_col] + key_value_cols
50
+ query_params = MaropostApi.set_query_params
51
+
52
+ MaropostApi.put_result(full_path, {:record => all_key_values}, query_params)
53
+ end
54
+
55
+ def upsert(key_value_col, *key_value_cols)
56
+ raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
57
+ if (key_value_cols)
58
+ invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
59
+ raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
60
+ end
61
+ full_path = full_resource_path '/upsert'
62
+ all_key_values = [key_value_col] + key_value_cols
63
+
64
+ query_params = MaropostApi.set_query_params
65
+
66
+ MaropostApi.put_result(full_path, {:record => all_key_values}, query_params)
67
+ end
68
+
69
+ def delete(unique_field_name, value)
70
+ raise TypeError.new("#{unique_field_name.inspect} is not a String") unless unique_field_name.is_a? String
71
+ record = {:record => {}}
72
+ record[:record][unique_field_name] = value
73
+
74
+ # first check if key value exists
75
+ show_record = show(unique_field_name, value)
76
+ return show_record unless show_record.success
77
+
78
+ full_path = full_resource_path '/delete'
79
+ query_params = MaropostApi.set_query_params
80
+
81
+ MaropostApi.delete_result(full_path, query_params, record)
82
+ end
83
+
84
+ private
85
+
86
+ def full_resource_path(resource_name = '')
87
+ MaropostApi.base_uri + "/#{@table_name}" + "#{resource_name}"
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,176 @@
1
+ module MaropostApi
2
+
3
+ ##
4
+ # Contains methods that get reports based on provided parameters.
5
+ # The method names themselves reveal the type of reports they are getting.
6
+ class Reports
7
+ ##
8
+ # Creates a new instance of Reports class.
9
+ # @param account [Integer] is authentic user account id (Integer) provided by maropost.com
10
+ # @param api_key [String] is the auth token (String) that is validated on the server to authenticate the user
11
+ def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
12
+ MaropostApi.instance_variable_set(:@api_key, api_key)
13
+ MaropostApi.instance_variable_set(:@account, account)
14
+ end
15
+
16
+ ##
17
+ # gets all the reports grouped by +page+ (Integer)
18
+ def get(page)
19
+ full_path = full_resource_path
20
+ query_params = MaropostApi.set_query_params({'page' => page})
21
+
22
+ MaropostApi.get_result(full_path, query_params)
23
+ end
24
+
25
+ ##
26
+ # gets a report defined by its unique +id+ (Integer)
27
+ def get_report(id)
28
+ full_path = full_resource_path "/#{id}"
29
+ query_params = MaropostApi.set_query_params
30
+
31
+ MaropostApi.get_result(full_path, query_params)
32
+ end
33
+
34
+ ##
35
+ # gets all open reports described by
36
+ # @param page [Integer] Integer that tells the service what page of report to retrieve
37
+ # @param fields [Array] that contains the name of the fields of the contact to retrieve
38
+ # @param from [Date] formatted string to add a constriant to our result
39
+ # @param to [Date] Part of date range of +from+. Determines the result retrieved is in between this range of date
40
+ # @param unique [Boolean] that indicates whether to get unique reports or not
41
+ # @param email [String] email to be equated while querying for reports
42
+ # @param uid [String] carrying unique identifier for the report.
43
+ # @param per [Integer] determining how many records to get for each page
44
+ def get_opens(page:, fields: [], from: nil, to: nil, unique: nil, email: nil, uid: nil, per: nil)
45
+ full_path = full_resource_path "/opens"
46
+ params = {}
47
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
48
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
49
+
50
+ query_params = MaropostApi.set_query_params(params)
51
+
52
+ MaropostApi.get_result(full_path, query_params)
53
+ end
54
+
55
+ ##
56
+ # gets all click reports described by
57
+ # @param page [Integer] that tells the service what page of report to retrieve
58
+ # @param fields [Array] that contains the name of the fields of the contact to retrieve
59
+ # @param from [Date] formatted string to add a constriant to our result
60
+ # @param to [Part] of date range of +from+. Determines the result retrieved is in between this range of date
61
+ # @param unique [Boolean] that indicates whether to get unique reports or not
62
+ # @param email [String] email to be equated while querying for reports
63
+ # @param uid [String] carrying unique identifier for the report.
64
+ # @param per [Integer] determining how many records to get for each page
65
+ def get_clicks(page:, fields: [], from: nil, to: nil, unique: nil, email: nil, uid: nil, per: nil)
66
+ full_path = full_resource_path "/clicks"
67
+ params = {}
68
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
69
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
70
+
71
+ query_params = MaropostApi.set_query_params(params)
72
+
73
+ MaropostApi.get_result(full_path, query_params)
74
+ end
75
+
76
+ ##
77
+ # gets all bounce reports described by
78
+ # @param page [Integer] that tells the service what page of report to retrieve
79
+ # @param fields [Array] that contains the name of the fields of the contact to retrieve
80
+ # @param from [Date] formatted string to add a constriant to our result
81
+ # @param to [Part] of date range of +from+. Determines the result retrieved is in between this range of date
82
+ # @param unique [Boolean] that indicates whether to get unique reports or not
83
+ # @param email [String] email to be equated while querying for reports
84
+ # @param uid [String] carrying unique identifier for the report.
85
+ # @param per [Integer] determining how many records to get for each page
86
+ # @param type [String] Set of values determining "soft" or "hard" bounces
87
+ def get_bounces(page:, fields: [], from: nil, to: nil, unique: nil, email: nil, uid: nil, per: nil, type: nil)
88
+ full_path = full_resource_path "/bounces"
89
+ params = {}
90
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
91
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
92
+
93
+ query_params = MaropostApi.set_query_params(params)
94
+
95
+ MaropostApi.get_result(full_path, query_params)
96
+ end
97
+
98
+ ##
99
+ # gets all unsubscribe reports described by
100
+ # @param page: [Integer] that tells the service what page of report to retrieve
101
+ # @param fields: [Array] that contains the name of the fields of the contact to retrieve
102
+ # @param from: [Date] formatted string to add a constriant to our result
103
+ # @param to: [Date] part of date range of +from+. Determines the result retrieved is in between this range of date
104
+ # @param unique: [Boolean] that indicates whether to get unique reports or not
105
+ # @param email: [String] email to be equated while querying for reports
106
+ # @param uid: [String] carrying unique identifier for the report.
107
+ # @param per: [Integer] determining how many records to get for each page
108
+ def get_unsubscribes(page:, fields: [], from: nil, to: nil, unique: nil, email: nil, uid: nil, per: nil)
109
+ full_path = full_resource_path "/unsubscribes"
110
+ params = {}
111
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
112
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
113
+
114
+ query_params = MaropostApi.set_query_params(params)
115
+
116
+ MaropostApi.get_result(full_path, query_params)
117
+ end
118
+
119
+ ##
120
+ # gets all complaint reports described by
121
+ # @param page [Integer] that tells the service what page of report to retrieve
122
+ # @param fields [Array] that contains the name of the fields of the contact to retrieve
123
+ # @param from [Date] formatted string to add a constriant to our result
124
+ # @param to [Date] part of date range of +from+. Determines the result retrieved is in between this range of date
125
+ # @param unique [Boolean] that indicates whether to get unique reports or not
126
+ # @param email [String] email to be equated while querying for reports
127
+ # @param uid [String] carrying unique identifier for the report.
128
+ # @param per [Integer] determining how many records to get for each page
129
+ def get_complaints(page:, fields: [], from: nil, to: nil, unique: nil, email: nil, uid: nil, per: nil)
130
+ full_path = full_resource_path "/unsubscribes"
131
+ params = {}
132
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
133
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
134
+
135
+ query_params = MaropostApi.set_query_params(params)
136
+
137
+ MaropostApi.get_result(full_path, query_params)
138
+ end
139
+
140
+ ##
141
+ # gets all ab reports described by
142
+ # @param page [Integer] that tells the service what page of report to retrieve
143
+ # @param fields [Array] that contains the name of the fields of the contact to retrieve
144
+ # @param from [Date] formatted string to add a constriant to our result
145
+ # @param to [Date] part of date range of +from+. Determines the result retrieved is in between this range of date
146
+ # @param per [Integer] determining how many records to get for each page
147
+ def get_ab_reports(name:, page:, from: nil, to: nil, per: nil)
148
+ full_path = full_resource_path('', "ab_reports")
149
+ params = {}
150
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
151
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
152
+
153
+ query_params = MaropostApi.set_query_params(params)
154
+
155
+ MaropostApi.get_result(full_path, query_params)
156
+ end
157
+
158
+ ##
159
+ # gets all journey reports for the provided +page+
160
+ # @param page [Integer]
161
+ def get_journeys(page)
162
+ full_path = full_resource_path '/journeys'
163
+ query_params = MaropostApi.set_query_params({:page => page})
164
+
165
+ MaropostApi.get_result(full_path, query_params)
166
+ end
167
+
168
+
169
+ private
170
+
171
+ def full_resource_path(specifics = '', root_resource = 'reports')
172
+ account = MaropostApi.instance_variable_get(:@account)
173
+ "/accounts/#{account}/#{root_resource}" << specifics
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,80 @@
1
+ module MaropostApi
2
+ class TransactionalCampaigns
3
+
4
+ def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
5
+ MaropostApi.instance_variable_set(:@api_key, api_key)
6
+ MaropostApi.instance_variable_set(:@account, account)
7
+ end
8
+
9
+ def get(page)
10
+ full_path = full_resource_path
11
+ query_params = MaropostApi.set_query_params({:page => page})
12
+
13
+ MaropostApi.get_result(full_path, query_params)
14
+ end
15
+
16
+ def create(name:, subject:, preheader:, from_name:, from_email:, reply_to:, content_id:, email_preview_link:, address:, language:, add_ctags: [])
17
+ params = {}
18
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
19
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
20
+
21
+ full_path = full_resource_path
22
+
23
+ MaropostApi.post_result(full_path, :campaign => params)
24
+ end
25
+
26
+ def send_email(
27
+ campaign_id:,
28
+ content: {},
29
+ contact: {},
30
+ send_time: {},
31
+ ignore_dnm: nil,
32
+ bcc: nil,
33
+ from_name: nil,
34
+ from_email: nil,
35
+ subject: nil,
36
+ reply_to: nil,
37
+ address: nil,
38
+ tags: {},
39
+ add_ctags: []
40
+ )
41
+ email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
42
+ # check for content field values or content id
43
+ raise ArgumentError.new('Content must be a type of Integer (content_id) or a Hash (content field values)') until content.kind_of? Hash or content.kind_of? Integer
44
+ if (content.kind_of? Hash)
45
+ content = content.slice(:name, :html_part, :text_part)
46
+ raise ArgumentError.new('Content field values must have all or some of :name, :html_part and :text_part as keys') until content.empty? || content.count > 0
47
+ end
48
+ # check for contact field values or contact_id
49
+ raise ArgumentError.new('Contact must be a type of Integer (contact_id) or a Hash (contact field values)') until contact.kind_of? Hash or contact.kind_of? Integer
50
+ if (contact.kind_of? Hash)
51
+ contact = contact.slice(:email, :first_name, :last_name, :custom_field)
52
+ raise ArgumentError.new('Contact field values must have :email and any or all of :first_name and :last_name :custom_field as keys') if contact.empty? || contact[:email].nil?
53
+ raise ArgumentError.new("contact[:custom_field] must be a type of Hash - #{contact[:custom_field].class} given!") until contact[:custom_field].is_a?(Hash) || contact[:custom_field].nil?
54
+ end
55
+ raise ArgumentError.new('contact[:email] must be a valid email address') until contact.is_a? Integer or contact[:email].match? email_regex
56
+ raise ArgumentError.new('bcc must be a valid email address') until bcc.nil? or bcc.match? email_regex
57
+ raise ArgumentError.new('from_email must be a valid email address') until from_email.nil? or from_email.match? email_regex
58
+ raise ArgumentError.new('reply_to must be a valid email address') until reply_to.nil? or reply_to.match? email_regex
59
+ raise ArgumentError.new('add_ctags must be a valid Array of string') until add_ctags.is_a? Array and add_ctags.all?{|t| t.is_a? String }
60
+
61
+ params = {}
62
+ method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
63
+ params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
64
+ params[:content_id] = params.delete(:content) if params[:content].kind_of? Integer
65
+ params[:contact_id] = params.delete(:contact) if params[:contact].kind_of? Integer
66
+
67
+ full_path = full_resource_path('/deliver', 'emails')
68
+
69
+ MaropostApi.post_result(full_path, :email => params)
70
+ end
71
+
72
+ private
73
+
74
+ def full_resource_path(specifics = '', root_resource = 'transactional_campaigns')
75
+ account = MaropostApi.instance_variable_get(:@account)
76
+ "/accounts/#{account}/#{root_resource}" << specifics
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module MaropostApi
2
+ VERSION = "0.1.0"
3
+ end