email_center_api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,40 +0,0 @@
1
- module EmailCenterApi
2
- class Base
3
- include HTTParty
4
- default_timeout 10
5
-
6
- def to_i
7
- id
8
- end
9
-
10
- def self.get(*args, &block)
11
- base_uri EmailCenterApi.endpoint
12
- basic_auth EmailCenterApi.username, EmailCenterApi.password
13
- super(*args, &block)
14
- end
15
-
16
- def self.post(*args, &block)
17
- base_uri EmailCenterApi.endpoint
18
- basic_auth EmailCenterApi.username, EmailCenterApi.password
19
- super(*args, &block)
20
- end
21
-
22
- def self.raise_errors(response)
23
- if response['msg']
24
- raise "Api Error: #{response['msg']}"
25
- else
26
- raise "Email Center Api: General Error!"
27
- end
28
- end
29
-
30
- def self.get_with_retry(*args, &block)
31
- retries = 0
32
- begin
33
- get(*args, &block)
34
- rescue Timeout::Error
35
- raise if (self.retries += 1) > 3
36
- retry
37
- end
38
- end
39
- end
40
- end
@@ -1,32 +0,0 @@
1
- module EmailCenterApi
2
- module Configuration
3
- VALID_CONNECTION_KEYS = [:endpoint].freeze
4
- VALID_OPTIONS_KEYS = [:username, :password].freeze
5
- VALID_CONFIG_KEYS = VALID_CONNECTION_KEYS + VALID_OPTIONS_KEYS
6
-
7
- DEFAULT_USERNAME = 'test'
8
- DEFAULT_PASSWORD = 'test'
9
- DEFAULT_ENDPOINT = 'https://maxemail.emailcenteruk.com/api/json/'
10
- # Build accessor methods for every config options so we can do this, for example:
11
- # EmailCenterApi.endpoint = 'https://maxemail.emailcenteruk.com/api/json/'
12
- attr_accessor *VALID_CONFIG_KEYS
13
-
14
- # Make sure we have the default values set when we get 'extended'
15
- def self.extended(base)
16
- base.reset
17
- end
18
-
19
- #Configure the gem with a configure block
20
- def configure
21
- yield self
22
- end
23
-
24
- def reset
25
- self.username = DEFAULT_USERNAME
26
- self.password = DEFAULT_PASSWORD
27
- self.endpoint = DEFAULT_ENDPOINT
28
- end
29
-
30
- end # Configuration
31
- end
32
-
@@ -1,90 +0,0 @@
1
- module EmailCenterApi
2
- class List < EmailCenterApi::Base
3
- attr_accessor :name, :created_ts, :list_total, :folder_id, :type, :id, :status, :update_ts
4
-
5
- def initialize(name, created_ts, list_total, folder_id, type, id, status, update_ts)
6
- self.name = name
7
- self.created_ts = created_ts
8
- self.list_total = list_total.to_i
9
- self.folder_id = folder_id.to_i
10
- self.type = type
11
- self.id = id.to_i
12
- self.status = status
13
- self.update_ts = update_ts
14
- end
15
-
16
- def insert_recipient(options)
17
- self.class.insert_recipient(id, options)
18
- end
19
-
20
- def delete_recipient(recipient)
21
- self.class.delete_recipient(id,recipient.to_i)
22
- end
23
-
24
- def recipients
25
- response = self.class.get_with_retry("/list", :query => {:method => "fetchRecipients", "listId" => id, :limit => 100, :start => 0, :sort => "email_address", :dir => "ASC", :filter => ["email_address"]})
26
- recipients = []
27
- if response.success?
28
- response['records'].each do |record|
29
- recipients << EmailCenterApi::Recipient.new(record['recipient_id'],record['email_address'],record['update_ts'])
30
- end
31
- else
32
- raise_errors(response)
33
- end
34
- recipients
35
- end
36
-
37
- def self.find(list_id)
38
- response = get_with_retry("/list?method=find&listId=#{list_id}")
39
- if response.success?
40
- self.new(response['name'], response['created_ts'], response['list_total'], response['folder_id'],
41
- response['type'], response['list_id'], response['status'], response['update_ts'])
42
- else
43
- raise_errors(response)
44
- end
45
- end
46
-
47
- def self.all
48
- response = get_with_retry("/list?method=fetchAll")
49
- if response.success?
50
- lists = []
51
- response.each do |list|
52
- lists << self.new(list['name'], list['created_ts'], list['list_total'], list['folder_id'],
53
- list['type'], list['list_id'], list['status'], list['update_ts'])
54
- end
55
- lists
56
- else
57
- raise response.response
58
- end
59
- end
60
-
61
- def self.find_by_name(name)
62
- lists = all.collect {|list| list if list.name == name}.compact
63
- if lists.empty?
64
- raise "Api Error: No list named #{name} was found"
65
- else
66
- lists.first
67
- end
68
- end
69
-
70
- def self.insert_recipient(list_id, options)
71
- body = { :method => "insertRecipient", :listId => list_id, :data => options }
72
- response = post("/list", {:body => body })
73
- if response.success?
74
- Recipient.new(response['recipient_id'],response['email_address'],response['update_ts'])
75
- else
76
- raise_errors(response)
77
- end
78
- end
79
-
80
- def self.delete_recipient(list_id, recipient_id)
81
- body = { :method => "deleteRecipient", :listId => list_id, :recipientId => recipient_id }
82
- response = post("/list", { :body => body })
83
- if response.success?
84
- response
85
- else
86
- raise_errors(response)
87
- end
88
- end
89
- end
90
- end
@@ -1,62 +0,0 @@
1
- module EmailCenterApi
2
- class Recipient < EmailCenterApi::Base
3
- attr_accessor :id, :email_address, :domain_name, :updated_at
4
-
5
- def initialize(id,email_address, updated_at)
6
- self.id = id.to_i
7
- self.email_address = email_address
8
- self.updated_at = updated_at
9
- end
10
-
11
- def lists
12
- lists = self.class.get_with_retry("/recipient?method=fetchLists&recipientId=#{id}")
13
- if lists.success?
14
- lists.collect{ |list| list if list['subscribed'] == "1" }.compact.collect do |list|
15
- List.find(list['list_id'])
16
- end
17
- else
18
- raise_errors(lists)
19
- end
20
- end
21
-
22
- def unsubscribed?
23
- !unsubscribed_from.empty?
24
- end
25
-
26
- def unsubscribed_from
27
- lists = self.class.get_with_retry("/recipient", :query => { :method => "fetchLists", "recipientId" => id })
28
- if lists.success?
29
- lists.collect{ |list| list if list['subscribed'] == "0" }.compact.collect do |list|
30
- List.find(list['list_id'])
31
- end
32
- else
33
- raise_errors(lists)
34
- end
35
- end
36
-
37
- def self.find(id)
38
- recipient = get_with_retry("/recipient?method=find&recipientId=#{id}")
39
- if recipient.success?
40
- self.new(recipient['recipient_id'],recipient['email_address'],recipient['update_ts'])
41
- else
42
- raise_errors(recipient)
43
- end
44
- end
45
-
46
- def self.find_by_email(email_address)
47
- id = get_with_retry("/recipient", :query => {:method => "findByEmailAddress", "emailAddress" => email_address })
48
- if id.success?
49
- recipient = get_with_retry("/recipient?method=find&recipientId=#{id.body}")
50
- if recipient.success?
51
- self.new(recipient['recipient_id'],recipient['email_address'],recipient['update_ts'])
52
- else
53
- raise_errors(recipient)
54
- end
55
- else
56
- raise_errors(id)
57
- end
58
- end
59
-
60
-
61
- end
62
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'configuration' do
4
- after(:each) do
5
- EmailCenterApi.reset
6
- end
7
-
8
- EmailCenterApi::Configuration::VALID_CONFIG_KEYS.each do |key|
9
- describe ".#{key}" do
10
- it 'should return the default value' do
11
- EmailCenterApi.send(key).should == EmailCenterApi::Configuration.const_get("DEFAULT_#{key.to_s.upcase}")
12
- end
13
- end
14
- end
15
-
16
- describe '.configure' do
17
- EmailCenterApi::Configuration::VALID_CONFIG_KEYS.each do |key|
18
- it "should set the #{key}" do
19
- EmailCenterApi.configure do |config|
20
- config.send("#{key}=", key)
21
- end
22
- EmailCenterApi.send(key).should == key
23
- end
24
- end
25
- end
26
-
27
-
28
- end
@@ -1,119 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe EmailCenterApi::List do
4
- describe ".find" do
5
- it "returns the list with the right id" do
6
- EmailCenterApi::List.find(4).id.should == 4
7
- end
8
-
9
- it "Throws an exception if the list is not found" do
10
- expect { EmailCenterApi::List.find(10) }.to raise_error "Api Error: Unable to find requested list"
11
- end
12
- end
13
-
14
- describe ".all" do
15
- it "returns an array" do
16
- EmailCenterApi::List.all.class.should == Array
17
- end
18
-
19
- it "should have the correct number of elements" do
20
- EmailCenterApi::List.all.size.should == 11
21
- end
22
-
23
- it "has List objects as elements in the array" do
24
- EmailCenterApi::List.all.first.class.should == EmailCenterApi::List
25
- end
26
-
27
- it "sets up the list objects with the correct attributes" do
28
- EmailCenterApi::List.all.first.id.should == 19
29
- EmailCenterApi::List.all.first.status.should == "available"
30
- end
31
- end
32
-
33
- describe ".find_by_name" do
34
- it "should return the correct named list" do
35
- EmailCenterApi::List.find_by_name("Master Unsubscribe List").id.should == 4
36
- end
37
-
38
- it "should throw an error if the list dosn't exist" do
39
- expect { EmailCenterApi::List.find_by_name("Stupid Monkey List") }.to raise_error
40
- end
41
- end
42
-
43
- describe ".insert_recipient" do
44
- time = "#{Time.now}"
45
- email = "james@example.com"
46
- id = 123456
47
- FakeWeb.register_uri(:post, "https://test:test@maxemail.emailcenteruk.com/api/json/list", :body => "{\"recipient_id\":\"#{id}\",\"email_address\":\"#{email}\",\"subscribed\":\"0\",\"update_ts\":\"#{time}\"}", :content_type => "application/json; charset=utf-8")
48
- options = { :email => email, :subscribed => "0" }
49
-
50
- it "hits the api with the correct post request" do
51
- EmailCenterApi::Base.stub_chain(:post, :success?).and_return(true)
52
- EmailCenterApi::Base.stub_chain(:post, :[]).and_return(1)
53
- EmailCenterApi::Base.should_receive(:post).with("/list", {:body=>{:data=> options, :listId=>25, :method=>"insertRecipient"}})
54
- EmailCenterApi::List.insert_recipient(25, options)
55
- end
56
-
57
- it "returns a recipient object" do
58
- EmailCenterApi::List.insert_recipient(25, options).class.should == EmailCenterApi::Recipient
59
- end
60
-
61
- it "returns a recipient with the right attributes" do
62
- EmailCenterApi::List.insert_recipient(25, options).id.should == id
63
- EmailCenterApi::List.insert_recipient(25, options).email_address.should == email
64
- EmailCenterApi::List.insert_recipient(25, options).updated_at.should == time
65
- end
66
-
67
- describe "an instance method .insert_recipient" do
68
- list = EmailCenterApi::List.new("A List", Time.now.to_s, 123, 14, "Sometype", 25, "A Status", Time.now.to_s)
69
- it "works in a similar way" do
70
- list.insert_recipient(options).class.should == EmailCenterApi::Recipient
71
- list.insert_recipient(options).id.should == id
72
- list.insert_recipient(options).email_address.should == email
73
- list.insert_recipient(options).updated_at.should == time
74
- end
75
- end
76
- end
77
-
78
- describe ".delete_recipient" do
79
- it "hits the api with the correct post request" do
80
- EmailCenterApi::Base.stub_chain(:post, :success?).and_return(true)
81
- EmailCenterApi::Base.should_receive(:post).with("/list", {:body=>{:recipientId=> 1234, :listId=>25, :method=>"deleteRecipient"}})
82
- EmailCenterApi::List.delete_recipient(25,1234)
83
- end
84
-
85
- it "returns true if the recipient was removed from the list" do
86
- FakeWeb.register_uri(:post, "https://test:test@maxemail.emailcenteruk.com/api/json/list", :body => "true", :content_type => "application/json; charset=utf-8")
87
- EmailCenterApi::List.delete_recipient(25,1234)
88
- end
89
-
90
- it "throws an error if the recipient dosn't exist on the list" do
91
- FakeWeb.register_uri(:post, "https://test:test@maxemail.emailcenteruk.com/api/json/list", :body => "{\"success\":false,\"msg\":\"Unable to find requested list recipient\",\"code\":0,\"errors\":[]}", :content_type => "application/json; charset=utf-8", :status => 400)
92
- expect { EmailCenterApi::List.delete_recipient(25,1234) }.to raise_error "Api Error: Unable to find requested list recipient"
93
- end
94
-
95
- it "has an instance method that accepts a recipient object" do
96
- list = EmailCenterApi::List.new("A List", Time.now.to_s, 123, 14, "Sometype", 25, "A Status", Time.now.to_s)
97
- EmailCenterApi::Base.stub_chain(:post, :success?).and_return(true)
98
- EmailCenterApi::Base.should_receive(:post).with("/list", {:body=>{:recipientId=> 1234, :listId=>25, :method=>"deleteRecipient"}})
99
- recipient = EmailCenterApi::Recipient.new(1234,"something@whatever.com",Time.now.to_s)
100
- list.delete_recipient(recipient)
101
- end
102
- end
103
-
104
- describe ".recipients" do
105
- id = rand(10000)
106
- id2 = rand(10000)
107
- recipients = "{\"list_total\":\"2\",\"count\":2,\"offset\":\"0\",\"limit\":\"100\",\"sort\":\"email_address\",\"dir\":\"ASC\",\"records\":[{\"record_type\":\"campaign\",\"subscribed\":\"0\",\"unsubscribe_method\":\"manual\",\"update_ts\":\"2012-09-11 16:43:10\",\"recipient_id\":\"#{id}\",\"email_address\":\"testing@exam41313ple123245.com\",\"Default.Date of Birth\":null,\"Default.EmailAddress\":null,\"Default.First Name\":null,\"Default.Gender\":null,\"Default.Last Name\":null,\"Default.Location\":null,\"Reviews.Country\":null,\"Reviews.product_image\":null,\"Reviews.purchase_date\":null,\"Reviews.questionnaire_no\":null,\"Reviews.questionnaire_short\":null,\"Reviews.questionnaire_yes\":null,\"Reviews.retailer_from\":null,\"Reviews.retailer_image\":null,\"Reviews.retailer_name\":null,\"Reviews.retailer_privacy_link\":null,\"Reviews.retailer_product_name\":null,\"Reviews.retailer_terms_conditions\":null,\"Reviews.retailer_website\":null,\"Reviews.subject\":null,\"Reviews.tracking_image\":null,\"Reviews.unsubscribe_link\":null,\"Reviews.variant\":null,\"Sent.FirstReview\":null},{\"record_type\":\"campaign\",\"subscribed\":\"1\",\"unsubscribe_method\":null,\"update_ts\":\"2012-09-11 16:26:09\",\"recipient_id\":\"#{id2}\",\"email_address\":\"testing@example.com\",\"Default.Date of Birth\":null,\"Default.EmailAddress\":null,\"Default.First Name\":null,\"Default.Gender\":null,\"Default.Last Name\":null,\"Default.Location\":null,\"Reviews.Country\":null,\"Reviews.product_image\":null,\"Reviews.purchase_date\":null,\"Reviews.questionnaire_no\":null,\"Reviews.questionnaire_short\":null,\"Reviews.questionnaire_yes\":null,\"Reviews.retailer_from\":null,\"Reviews.retailer_image\":null,\"Reviews.retailer_name\":null,\"Reviews.retailer_privacy_link\":null,\"Reviews.retailer_product_name\":null,\"Reviews.retailer_terms_conditions\":null,\"Reviews.retailer_website\":null,\"Reviews.subject\":null,\"Reviews.tracking_image\":null,\"Reviews.unsubscribe_link\":null,\"Reviews.variant\":null,\"Sent.FirstReview\":null}]}"
108
- FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?dir=ASC&filter[]=email_address&limit=100&listId=25&start=0&method=fetchRecipients&sort=email_address", :body => recipients, :content_type => "application/json; charset=utf-8")
109
- list = EmailCenterApi::List.new("A List", Time.now.to_s, 123, 14, "Sometype", 25, "A Status", Time.now.to_s)
110
-
111
- it "returns an array of the recipients in the list" do
112
- list.recipients.first.class.should == EmailCenterApi::Recipient
113
- list.recipients.first.id.should == id
114
- list.recipients.last.id.should == id2
115
- list.recipients.first.email_address.should == "testing@exam41313ple123245.com"
116
- end
117
-
118
- end
119
- end
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe EmailCenterApi::Recipient do
4
- describe ".find" do
5
- it 'returns a recipient with the correct id' do
6
- EmailCenterApi::Recipient.find(1234).id.should == 1234
7
- end
8
-
9
- it 'returns a recipient with the correct params' do
10
- EmailCenterApi::Recipient.find(1234).email_address.should == "test@example.co.uk"
11
- EmailCenterApi::Recipient.find(1234).updated_at.should == "2012-01-21 07:15:13"
12
- end
13
-
14
- it 'rases an error if the id is not found' do
15
- expect { EmailCenterApi::Recipient.find(5678) }.to raise_error "Api Error: Unable to find selected recipient"
16
- end
17
- end
18
-
19
- describe ".unsubscribed_from" do
20
- it "returns all the lists where subscribed == 0" do
21
- EmailCenterApi::Recipient.find(1234).unsubscribed_from.first.id.should == 25
22
- end
23
- end
24
-
25
- describe ".unsubscribed?" do
26
- it "returns true if the recipient is unsubscribed from somthing" do
27
- EmailCenterApi::Recipient.find(1234).unsubscribed?.should == true
28
- end
29
-
30
- it "returns false if the recipient is not unsubscribed from anything" do
31
- EmailCenterApi::Recipient.find(2345).unsubscribed?.should == false
32
- end
33
- end
34
-
35
- describe ".lists" do
36
- it "returns all the lists where subscribed == 1" do
37
- EmailCenterApi::Recipient.find(1234).lists.first.id.should == 26
38
- end
39
- end
40
-
41
- describe ".find" do
42
- it "finds the recipient with the given id" do
43
- EmailCenterApi::Recipient.find(1234).id.should == 1234
44
- end
45
-
46
- it "creates the objects with the correct params" do
47
- EmailCenterApi::Recipient.find(1234).email_address.should == "test@example.co.uk"
48
- end
49
- end
50
-
51
- describe ".find_by_email" do
52
- it "finds the correct recipient" do
53
- EmailCenterApi::Recipient.find_by_email("test@example.co.uk").id.should == 1234
54
- end
55
- end
56
- end