email_center_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test_email_center.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in email_center_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ed Robinson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # EmailCenterApi
2
+
3
+ A RubyGem That wraps EmailCenter's maxemail JSON Api
4
+
5
+ see http://maxemail.emailcenteruk.com/manual/doku.php?id=maxemail:v6:webservices
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'email_center_api'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install email_center_api
20
+
21
+ ## Usage
22
+
23
+ Experimental and Feature Incomplete. Will allow some manipulation of
24
+ lists and recipients.
25
+
26
+ Try it out with something like this:
27
+
28
+ require 'rubygems'
29
+ require 'email_center_api'
30
+ require 'pry'
31
+
32
+
33
+ EmailCenterApi.configure do |config|
34
+ config.username = 'yourusername'
35
+ config.password = 'yourpassword'
36
+ end
37
+
38
+ binding.pry
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Write Some Tests
45
+ 4. Write Some Code
46
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 6. Push to the branch (`git push origin my-new-feature`)
48
+ 7. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'email_center_api/version'
5
+ require 'base64'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "email_center_api"
9
+ gem.version = EmailCenterApi::VERSION
10
+ gem.authors = ["Ed Robinson"]
11
+ gem.email = Base64.decode64("ZWQucm9iaW5zb25AcmVldm9vLmNvbQ==\n")
12
+ gem.description = %q{ A RubyGem That wraps EmailCenter's maxemail JSON Api }
13
+ gem.summary = %q{ A RubyGem That wraps EmailCenter's maxemail JSON Api }
14
+ gem.homepage = "https://github.com/reevoo/email_center_api"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ gem.add_dependency("httparty", "~> 0.9.0")
21
+ gem.add_dependency("json", "~> 1.7.5")
22
+ gem.add_development_dependency("fakeweb", "~> 1.3.0")
23
+ gem.add_development_dependency("rspec", "~> 2.11.0")
24
+ gem.add_development_dependency("pry")
25
+ end
@@ -0,0 +1,40 @@
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
@@ -0,0 +1,32 @@
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
+
@@ -0,0 +1,90 @@
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
@@ -0,0 +1,62 @@
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
@@ -0,0 +1,3 @@
1
+ module EmailCenterApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "httparty"
2
+ require "email_center_api/version"
3
+ require "email_center_api/configuration"
4
+ require "email_center_api/base"
5
+
6
+
7
+ module EmailCenterApi
8
+ extend Configuration
9
+ end
10
+
11
+ require "email_center_api/list"
12
+ require "email_center_api/recipient"
13
+
@@ -0,0 +1,28 @@
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
data/spec/list_spec.rb ADDED
@@ -0,0 +1,119 @@
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
@@ -0,0 +1,56 @@
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
@@ -0,0 +1,48 @@
1
+ require 'email_center_api'
2
+ require 'email_center_api/list'
3
+ require 'email_center_api/recipient'
4
+ require 'email_center_api/configuration'
5
+
6
+ require 'fakeweb'
7
+ require 'httparty'
8
+ FakeWeb.allow_net_connect = false
9
+ #Subscribed and Unsubscribed lists
10
+ lists_body = "[{\"recipient_id\":\"1234\",\"list_id\":\"25\",\"record_type\":\"campaign\",\"subscribed\":\"0\",\"unsubscribe_method\":\"manual\",\"update_ts\":\"2012-09-11 16:43:10\",\"list_name\":\"testunsub\",\"list_type\":\"suppression\",\"email_address\":\"test@example.co.uk\"},
11
+ {\"recipient_id\":\"1234\",\"list_id\":\"26\",\"record_type\":\"campaign\",\"subscribed\":\"1\",\"update_ts\":\"2012-09-11 16:43:10\",\"list_name\":\"testsublist\",\"list_type\":\"something\",\"email_address\":\"test@example.co.uk\"}]"
12
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?recipientId=1234&method=fetchLists", :body => lists_body, :content_type => "application/json" )
13
+ #Subscribed list only
14
+ sub_list_body = "[{\"recipient_id\":\"1234\",\"list_id\":\"26\",\"record_type\":\"campaign\",\"subscribed\":\"1\",\"update_ts\":\"2012-09-11 16:43:10\",\"list_name\":\"testsublist\",\"list_type\":\"something\",\"email_address\":\"test@example.co.uk\"}]"
15
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?recipientId=2346&method=fetchLists", :body => sub_list_body, :content_type => 'application/json')
16
+ #Find Recipient
17
+ recipient_body = "{\"recipient_id\":\"1234\",\"email_address\":\"test@example.co.uk\",\"domain_name\":\"example.co.uk\",\"update_ts\":\"2012-01-21 07:15:13\"}"
18
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?method=find&recipientId=1234", :body => recipient_body, :content_type => "application/json")
19
+ #Find another
20
+ another_recipient_body = "{\"recipient_id\":\"2346\",\"email_address\":\"test2@example.co.uk\",\"domain_name\":\"example.co.uk\",\"update_ts\":\"2012-01-21 07:15:13\"}"
21
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?method=find&recipientId=2345", :body => another_recipient_body, :content_type => "application/json")
22
+
23
+ #Recipient not found
24
+ recipient_not_found_body = "{\"success\":false,\"msg\":\"Unable to find selected recipient\",\"code\":0,\"errors\":[]}"
25
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?method=find&recipientId=5678", :body => recipient_not_found_body, :content_type => "application/json", :status => 400)
26
+ #Find List list
27
+ list = '{"list_id":"4","folder_id":"9","name":"Master Unsubscribe List","list_total":"1925","status":"available","type":"suppression","created_ts":"2009-02-13 11:18:05","update_ts":"2012-09-11 13:29:25"}'
28
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?method=find&listId=4", :body => list, :content_type => "application/json")
29
+ #List Not Found
30
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?method=find&listId=10", :body => '{"success":false,"msg":"Unable to find requested list","code":0,"errors":[]}', :content_type => "application/json", :status => 400)
31
+ #All lists
32
+ all_lists = '[{"list_id":"19","folder_id":"9","name":"111116_clicked_bad_link","list_total":"2930","status":"available","type":"include","created_ts":"2011-11-16 17:42:44","update_ts":"2011-11-16 17:43:02"},
33
+ {"list_id":"21","folder_id":"9","name":"111116_clicked_good_link","list_total":"3602","status":"available","type":"suppression","created_ts":"2011-11-16 17:43:28","update_ts":"2011-11-16 17:43:46"},
34
+ {"list_id":"23","folder_id":"9","name":"111123_clicked_initial_email","list_total":"23632","status":"available","type":"suppression","created_ts":"2011-11-23 10:02:03","update_ts":"2011-11-23 10:02:28"},
35
+ {"list_id":"7","folder_id":"9","name":"Contacts","list_total":"0","status":"available","type":"include","created_ts":"2011-08-10 16:23:39","update_ts":"2011-08-10 16:23:39"},
36
+ {"list_id":"2","folder_id":"9","name":"Master Bounce List","list_total":"4030","status":"available","type":"bounce","created_ts":"2009-02-13 11:18:05","update_ts":"2012-09-11 10:02:15"},
37
+ {"list_id":"9","folder_id":"33","name":"Master List","list_total":"7521","status":"available","type":"include","created_ts":"2011-08-15 14:50:05","update_ts":"2012-09-11 04:00:15"},
38
+ {"list_id":"4","folder_id":"9","name":"Master Unsubscribe List","list_total":"1925","status":"available","type":"suppression","created_ts":"2009-02-13 11:18:05","update_ts":"2012-09-11 13:29:25"},
39
+ {"list_id":"13","folder_id":"33","name":"Reminder List","list_total":"1642205","status":"available","type":"include","created_ts":"2011-10-03 16:22:09","update_ts":"2012-09-11 04:00:15"},
40
+ {"list_id":"5","folder_id":"9","name":"Test Data (EMC)","list_total":"9","status":"available","type":"include","created_ts":"2011-08-10 14:45:03","update_ts":"2011-08-10 14:48:34"},
41
+ {"list_id":"11","folder_id":"9","name":"Test Data (SD)","list_total":"3","status":"available","type":"include","created_ts":"2011-08-17 14:14:01","update_ts":"2011-08-17 14:16:48"},
42
+ {"list_id":"25","folder_id":"9","name":"testunsub","list_total":"0","status":"available","type":"suppression","created_ts":"2012-09-10 13:39:19","update_ts":"2012-09-10 13:39:19"}]'
43
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?method=fetchAll", :body => all_lists, :content_type => "application/json")
44
+
45
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?method=find&listId=25", :body => "{\"list_id\":\"25\",\"folder_id\":\"9\",\"name\":\"testunsub\",\"list_total\":\"2\",\"status\":\"available\",\"type\":\"suppression\",\"created_ts\":\"2012-09-10 13:39:19\",\"update_ts\":\"2012-09-12 10:23:20\"}", :content_type => "application/json")
46
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/list?method=find&listId=26", :body => "{\"list_id\":\"26\",\"folder_id\":\"9\",\"name\":\"tunsublist\",\"list_total\":\"2\",\"status\":\"available\",\"type\":\"something\",\"created_ts\":\"2012-09-10 13:39:19\",\"update_ts\":\"2012-09-12 10:23:20\"}", :content_type => "application/json")
47
+
48
+ FakeWeb.register_uri(:get, "https://test:test@maxemail.emailcenteruk.com/api/json/recipient?emailAddress=test%40example.co.uk&method=findByEmailAddress", :body => "1234")
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_center_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ed Robinson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 59
29
+ segments:
30
+ - 0
31
+ - 9
32
+ - 0
33
+ version: 0.9.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 1
45
+ segments:
46
+ - 1
47
+ - 7
48
+ - 5
49
+ version: 1.7.5
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: fakeweb
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 27
61
+ segments:
62
+ - 1
63
+ - 3
64
+ - 0
65
+ version: 1.3.0
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 35
77
+ segments:
78
+ - 2
79
+ - 11
80
+ - 0
81
+ version: 2.11.0
82
+ type: :development
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ type: :development
97
+ version_requirements: *id005
98
+ description: " A RubyGem That wraps EmailCenter's maxemail JSON Api "
99
+ email: ed.robinson@reevoo.com
100
+ executables: []
101
+
102
+ extensions: []
103
+
104
+ extra_rdoc_files: []
105
+
106
+ files:
107
+ - .gitignore
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - email_center_api.gemspec
113
+ - lib/email_center_api.rb
114
+ - lib/email_center_api/base.rb
115
+ - lib/email_center_api/configuration.rb
116
+ - lib/email_center_api/list.rb
117
+ - lib/email_center_api/recipient.rb
118
+ - lib/email_center_api/version.rb
119
+ - spec/configuration_spec.rb
120
+ - spec/list_spec.rb
121
+ - spec/recipient_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/reevoo/email_center_api
124
+ licenses: []
125
+
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.24
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: A RubyGem That wraps EmailCenter's maxemail JSON Api
156
+ test_files:
157
+ - spec/configuration_spec.rb
158
+ - spec/list_spec.rb
159
+ - spec/recipient_spec.rb
160
+ - spec/spec_helper.rb
161
+ has_rdoc: