emma-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,135 @@
1
+ # @see http://api.myemma.com/api/external/members.html
2
+
3
+ module Emma
4
+ module API
5
+ module Members
6
+ # Get detailed information on a particular member, including all custom fields.
7
+ def get_member_by_id(id, params = {})
8
+ get("/members/#{id}", params)
9
+ end
10
+
11
+ # Get a basic listing of all members in an account.
12
+ def my_members(params = {})
13
+ get("/members", params)
14
+ end
15
+
16
+ # Get detailed information on a particular member, including all custom fields, by email address instead of ID.
17
+ def get_member_by_email(email, params = {})
18
+ get("/members/email/#{email}", params)
19
+ end
20
+
21
+ # If a member has been opted out, returns the details of their optout, specifically date and mailing_id.
22
+ def get_optout_member(id)
23
+ get("/members/#{id}/optout")
24
+ end
25
+
26
+ # Update a member’s status to optout keyed on email address instead of an ID.
27
+ def optout(email)
28
+ put("/members/email/optout/#{email}")
29
+ end
30
+
31
+ # Add new members or update existing members in bulk. If you are doing actions for a single member please see the add_member() call below.
32
+ def add_members(params = {})
33
+ post("/members", params)
34
+ end
35
+
36
+ # Adds or updates a single audience member. If you are performing actions on bulk members please use the add_members() call above.
37
+ def add_member(params = {})
38
+ post("/members/add", params)
39
+ end
40
+
41
+ # Adds member and triggers the opt-out workflow. Use when you want to send a confirmation email.
42
+ def add_member_signup(params = {})
43
+ post("/members/signup", params)
44
+ end
45
+
46
+ # Delete an array of members.
47
+ def remove_members(params = {})
48
+ put("/members/delete", params)
49
+ end
50
+
51
+ # Change the status for an array of members.
52
+ def change_members_status(params = {})
53
+ put("/members/status", params)
54
+ end
55
+
56
+ # Update a single member’s information.
57
+ def update_member(id, params = {})
58
+ put("/members/#{id}", params)
59
+ end
60
+
61
+ # Delete the specified member.
62
+ def remove_member(id)
63
+ delete("/members/#{id}")
64
+ end
65
+
66
+ # Get the groups to which a member belongs.
67
+ def get_members_groups(id)
68
+ get("/members/#{id}/groups")
69
+ end
70
+
71
+ # Add a single member to one or more groups.
72
+ def add_member_to_groups(id, params)
73
+ put("/members/#{id}/groups", params)
74
+ end
75
+
76
+ # Remove a single member from one or more groups.
77
+ def remove_member_from_groups(id, params)
78
+ put("/members/#{id}/groups/remove", params)
79
+ end
80
+
81
+ # Delete all members.
82
+ def remove_all_members
83
+ delete("/members")
84
+ end
85
+
86
+ # Remove the specified member from all groups.
87
+ def remove_member_from_all_groups(id)
88
+ delete("/members/#{id}/groups")
89
+ end
90
+
91
+ # Remove multiple members from groups.
92
+ def remove_multiple_members_from_groups(params = {})
93
+ put("/members/groups/remove", params)
94
+ end
95
+
96
+ # Get the entire mailing history for a member.
97
+ def get_mailing_history_for_member(id)
98
+ get("/members/#{id}/mailings")
99
+ end
100
+
101
+ # Get a list of members affected by this import.
102
+ def imported_members(import_id)
103
+ get("/members/imports/#{import_id}/members")
104
+ end
105
+
106
+ # Get information and statistics about this import.
107
+ def import_stats(import_id)
108
+ get("/members/imports/#{import_id}")
109
+ end
110
+
111
+ # Get information about all imports for this account.
112
+ def my_imports
113
+ get("/members/imports")
114
+ end
115
+
116
+ # Update an import record to be marked as ‘deleted’.
117
+ def delete_import
118
+ delete("/members/imports/delete")
119
+ end
120
+
121
+ # Copy all account members of one or more statuses into a group.
122
+ def copy_members_to_group(group_id, params = {})
123
+ put("/members/#{group_id}/copy", params)
124
+ end
125
+
126
+ # Update the status for a group of members, based on their current
127
+ # status. Valid statuses id are (‘a’,’e’, ‘f’, ‘o’) active, error, forwarded, optout.
128
+ def update_group_members_status(status_from, status_to, group_id = nil)
129
+ params = {}
130
+ params[:group_id] = group_id unless group_id.nil?
131
+ put("/members/status/#{status_from}/to/#{status_to}", params)
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,36 @@
1
+ # @see http://api.myemma.com/api/external/response.html
2
+
3
+ module Emma
4
+ module API
5
+ module Response
6
+ # Get the response summary for an account.
7
+ # This method will return a month-based time series of data including sends, opens, clicks, mailings, forwards, and opt-outs.
8
+ # Test mailings and forwards are not included in the data returned.
9
+ def my_account_summary(params = {})
10
+ get("/response", params)
11
+ end
12
+
13
+ # Get the response summary for a particular mailing.
14
+ # This method will return the counts of each type of response activity for a particular mailing.
15
+ def single_response_summary(id)
16
+ get("/response/#{id}")
17
+ end
18
+
19
+ # Get the list of messages by id by a particular action
20
+ # actions include (sends, in_progress, deliveries, opens, links, clicks, forwards, optouts, signups, shares, customer_shares, customer_share_clicks)
21
+ def response_mailing_information(id, action = 'sends', params = {})
22
+ get("/response/#{id}/#{action}", params)
23
+ end
24
+
25
+ # Get the customer share associated with the share id.
26
+ def customer_share_information(share_id, params = {})
27
+ get("/response/#{share_id}/customer_share", params)
28
+ end
29
+
30
+ # Get overview of shares pertaining to this mailing_id.
31
+ def mailing_shares_overview(id)
32
+ get("/response/#{id}/shares/overview")
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ # @see http://api.myemma.com/api/external/searches.html
2
+
3
+ module Emma
4
+ module API
5
+ module Searches
6
+ # Retrieve a list of saved searches.
7
+ def my_searches(params = {})
8
+ get("/searches", params)
9
+ end
10
+
11
+ # Get the details for a saved search.
12
+ def single_search(id, params = {})
13
+ get("/searches/#{id}", params)
14
+ end
15
+
16
+ # Create a saved search.
17
+ def create_search(params = {})
18
+ post("/searches", params)
19
+ end
20
+
21
+ # Update a saved search.
22
+ # No parameters are required, but either the name or criteria parameter must be present for an update to occur.
23
+ def update_search(id, params = {})
24
+ put("/searches/#{id}", params)
25
+ end
26
+
27
+ # Delete a saved search. The member records referred to by the search are not affected.
28
+ def remove_search(id)
29
+ delete("/searches/#{id}")
30
+ end
31
+
32
+ # Get the members matching the search.
33
+ def get_members_of_search(id)
34
+ get("/searches/#{id}/members")
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ # @see http://api.myemma.com/api/external/triggers.html
2
+
3
+ module Emma
4
+ module API
5
+ module Triggers
6
+ # Get a basic listing of all triggers in an account.
7
+ def my_triggers(params = {})
8
+ get("/triggers")
9
+ end
10
+
11
+ # Create a new trigger.
12
+ def add_trigger(params = {})
13
+ post("/triggers", params)
14
+ end
15
+
16
+ # Look up a trigger by trigger id.
17
+ def get_trigger_by_id(id)
18
+ get("/triggers/#{id}")
19
+ end
20
+
21
+ # Update or edit a trigger.
22
+ def update_trigger(id, params = {})
23
+ put("/triggers/#{id}", params)
24
+ end
25
+
26
+ # Delete a trigger.
27
+ def remove_trigger(id)
28
+ delete("/triggers/#{id}")
29
+ end
30
+
31
+ # Get mailings sent by a trigger.
32
+ def get_trigger_mailings(id, params = {})
33
+ get("/triggers/#{id}/mailings", params)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ # @see http://api.myemma.com/api/external/webhooks.html
2
+
3
+ module Emma
4
+ module API
5
+ module Webhooks
6
+ # Get a basic listing of all webhooks associated with an account.
7
+ def my_webhooks(params = {})
8
+ get("/webhooks")
9
+ end
10
+
11
+ # Get information for a specific webhook belonging to a specific account.
12
+ def get_webhook(id)
13
+ get("/webhooks/#{id}")
14
+ end
15
+
16
+ # Get a listing of all event types that are available for webhooks.
17
+ def webhook_events(id)
18
+ get("/webhooks/#{id}/events")
19
+ end
20
+
21
+ # Create an new webhook.
22
+ def add_webhook(params = {})
23
+ post("/webhooks", params)
24
+ end
25
+
26
+ # Update an existing webhook. Takes the same params as create_webhook.
27
+ def update_webhook(id, params = {})
28
+ put("/webhooks/#{id}", params)
29
+ end
30
+
31
+ # Deletes an existing webhook.
32
+ def remove_webhook(id)
33
+ delete("/webhooks/#{id}")
34
+ end
35
+
36
+ # Delete all webhooks registered for an account.
37
+ def remove_all_webhooks
38
+ delete("/webhooks")
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,92 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'emma/configurable'
4
+ require 'emma/api/members'
5
+ require 'emma/api/fields'
6
+ require 'emma/api/groups'
7
+ require 'emma/api/mailings'
8
+ require 'emma/api/response'
9
+ require 'emma/api/searches'
10
+ require 'emma/api/triggers'
11
+ require 'emma/api/webhooks'
12
+
13
+
14
+ module Emma
15
+ class Client
16
+ include HTTParty
17
+
18
+ # Include individual modules
19
+ include Emma::API::Members
20
+ include Emma::API::Fields
21
+ include Emma::API::Groups
22
+ include Emma::API::Mailings
23
+ include Emma::API::Response
24
+ include Emma::API::Searches
25
+ include Emma::API::Triggers
26
+ include Emma::API::Webhooks
27
+
28
+ base_uri "https://api.e2ma.net"
29
+
30
+ attr_accessor :account_id, :private_key, :public_key
31
+
32
+ # On insantiation set instance variables into memory
33
+ def initialize(options = {})
34
+ Emma::Configurable.keys.each do |key|
35
+ instance_variable_set(:"@#{key}", options[key] || Emma.instance_variable_get(:"@#{key}"))
36
+ end
37
+ @default_params = {:basic_auth => {:username => @public_key, :password => @private_key}}
38
+ end
39
+
40
+ # HTTP GET Request
41
+ def get(path, query = {})
42
+ request(:get, path, query)
43
+ end
44
+
45
+ # HTTP POST Request
46
+ def post(path, params = {})
47
+ # options = {:body => params.to_json}
48
+ request(:post, path, params)
49
+ end
50
+
51
+ # HTTP PUT Request
52
+ def put(path, params = {})
53
+ # options = {:body => params.to_json}
54
+ request(:put, path, params)
55
+ end
56
+
57
+ # HTTP DELETE Request
58
+ def delete(path, query = {})
59
+ request(:delete, path, query)
60
+ end
61
+
62
+ private
63
+ # Performs Call to API
64
+ def request(method, path, params = {})
65
+ self.class.debug_output($stderr) if @debug
66
+ setup_http_body(method, params)
67
+
68
+ uri = base_api_uri + path
69
+ response = self.class.send(method.to_sym, uri, @default_params)
70
+ parse(response)
71
+ end
72
+
73
+ def setup_http_body(method, params)
74
+ unless params.empty?
75
+ @default_params.merge!({:query => params}) if [:get, :delete].include? method
76
+ @default_params.merge!({:body => params.to_json}) if [:put, :post].include? method
77
+ end
78
+ @default_params
79
+ end
80
+
81
+ # Appends account id to base_uri
82
+ def base_api_uri
83
+ "#{self.class.base_uri}/#{@account_id}"
84
+ end
85
+
86
+ # Return parsed response from API Call
87
+ def parse(response)
88
+ return JSON.parse('[' + response.body + ']').first
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,21 @@
1
+ module Emma
2
+ module Configurable
3
+ class << self
4
+ # all keys (instance vars) available
5
+ def keys
6
+ @keys ||= [
7
+ :account_id,
8
+ :public_key,
9
+ :private_key,
10
+ :debug
11
+ ]
12
+ end
13
+ end
14
+
15
+ private
16
+ # return mapped Hash of available keys
17
+ def options
18
+ Hash[Emma::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Emma
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'vcr'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'emma'
9
+
10
+ VCR.configure do |c|
11
+ c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
12
+ c.default_cassette_options = { :record => :new_episodes }
13
+ c.hook_into :webmock
14
+ end
@@ -0,0 +1,88 @@
1
+ require_relative 'helper'
2
+
3
+ class TestEmmaInterface < Test::Unit::TestCase
4
+
5
+ context "attributes" do
6
+ setup do
7
+ @_env_account_id = ENV['EMMA_ACCOUNT_ID']
8
+ @_env_public_key = ENV['EMMA_PUBLIC_KEY']
9
+ @_env_private_key = ENV['EMMA_PRIVATE_KEY']
10
+
11
+ ENV.delete 'EMMA_ACCOUNT_ID'
12
+ ENV.delete 'EMMA_PUBLIC_KEY'
13
+ ENV.delete 'EMMA_PRIVATE_KEY'
14
+
15
+ @account_id = '12345678'
16
+ @pub_key = 'test_public_key'
17
+ @private_key = 'test_private_key'
18
+ @debug = true
19
+ end
20
+
21
+ teardown do
22
+ ENV['EMMA_ACCOUNT_ID'] = @_env_account_id
23
+ ENV['EMMA_PUBLIC_KEY'] = @_env_public_key
24
+ ENV['EMMA_PRIVATE_KEY'] = @_env_private_key
25
+ end
26
+
27
+ should "No API due to no Account ID" do
28
+ @em = Emma::Setup.new
29
+ assert_equal nil, @em.account_id
30
+ end
31
+
32
+ should "No API due to no Pub Key" do
33
+ @em = Emma::Setup.new(@account_id)
34
+ assert_equal nil, @em.public_key
35
+ end
36
+
37
+ should "No API due to no Private Key" do
38
+ @em = Emma::Setup.new(@account_id, @public_key)
39
+ assert_equal nil, @em.private_key
40
+ end
41
+
42
+ should "set the Account ID via the setter" do
43
+ @em = Emma::Setup.new
44
+ @em.account_id = @account_id
45
+ assert_equal @account_id, @em.account_id
46
+ end
47
+
48
+ should "set the Public Key via the setter" do
49
+ @em = Emma::Setup.new
50
+ @em.public_key = @pub_key
51
+ assert_equal @pub_key, @em.public_key
52
+ end
53
+
54
+ should "set the Private Key via the setter" do
55
+ @em = Emma::Setup.new
56
+ @em.private_key = @private_key
57
+ assert_equal @private_key, @em.private_key
58
+ end
59
+
60
+ should "turn on debug mode via the setter" do
61
+ @em = Emma::Setup.new
62
+ @em.debug = @debug
63
+ assert_equal @debug, @em.debug
64
+ end
65
+
66
+ should "set the Account ID via the Environment variable" do
67
+ ENV['EMMA_ACCOUNT_ID'] = @account_id
68
+ @em = Emma::Setup.new
69
+ assert_equal @account_id, @em.account_id
70
+ ENV.delete 'EMMA_ACCOUNT_ID'
71
+ end
72
+
73
+ should "set the Public Key via the Environment variable" do
74
+ ENV['EMMA_PUBLIC_KEY'] = @public_key
75
+ @em = Emma::Setup.new
76
+ assert_equal @public_key, @em.public_key
77
+ ENV.delete 'EMMA_PUBLIC_KEY'
78
+ end
79
+
80
+ should "set the Private Key via the Environment variable" do
81
+ ENV['EMMA_PRIVATE_KEY'] = @private_key
82
+ @em = Emma::Setup.new
83
+ assert_equal @private_key, @em.private_key
84
+ ENV.delete 'EMMA_PRIVATE_KEY'
85
+ end
86
+ end
87
+
88
+ end