eroi 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg/
2
+ test/fixtures.yml
data/README.rdoc CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  API interface to eROI.
4
4
 
5
+ This gem only contains contact management. If you need more support, please fork and add it.
6
+
5
7
  == Note on Patches/Pull Requests
6
8
 
7
9
  * Fork the project.
data/Rakefile CHANGED
@@ -19,7 +19,7 @@ end
19
19
  require 'rake/testtask'
20
20
  Rake::TestTask.new(:test) do |test|
21
21
  test.libs << 'lib' << 'eroi'
22
- test.pattern = 'test/**/*_test.rb'
22
+ test.pattern = 'test/eroi/*_test.rb'
23
23
  test.verbose = true
24
24
  end
25
25
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/eroi.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{eroi}
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["CardPlayer"]
12
- s.date = %q{2009-12-03}
12
+ s.date = %q{2009-12-04}
13
13
  s.description = %q{API interface to eROI.}
14
14
  s.email = %q{techteam@cardplayer.com}
15
15
  s.extra_rdoc_files = [
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
- "LICENSE",
20
+ ".gitignore",
21
+ "LICENSE",
21
22
  "Manifest",
22
23
  "README.rdoc",
23
24
  "Rakefile",
@@ -25,9 +26,12 @@ Gem::Specification.new do |s|
25
26
  "eroi.gemspec",
26
27
  "lib/eroi.rb",
27
28
  "lib/eroi/client.rb",
29
+ "lib/eroi/request.rb",
28
30
  "lib/eroi/response.rb",
29
31
  "lib/eroi/version.rb",
30
32
  "test/eroi/client_test.rb",
33
+ "test/fixtures.yml.sample",
34
+ "test/remote/client_test.rb",
31
35
  "test/test_helper.rb"
32
36
  ]
33
37
  s.homepage = %q{http://github.com/cardplayer/eroi}
@@ -37,6 +41,7 @@ Gem::Specification.new do |s|
37
41
  s.summary = %q{API interface to eROI.}
38
42
  s.test_files = [
39
43
  "test/eroi/client_test.rb",
44
+ "test/remote/client_test.rb",
40
45
  "test/test_helper.rb"
41
46
  ]
42
47
 
data/lib/eroi/client.rb CHANGED
@@ -4,71 +4,46 @@ module EROI
4
4
  end
5
5
 
6
6
  class Client
7
- POST_API_URL = 'http://emailer.emailroi.com/dbadmin/xml_post.pl'
8
- GET_API_URL = 'http://emailer.emailroi.com/dbadmin/xml_retrieve2.pl'
7
+ attr_reader :user_token, :api_password
9
8
 
10
9
  def initialize(user_token, api_password)
11
10
  @user_token = user_token
12
11
  @api_password = api_password
13
12
  end
14
13
 
14
+ def contact(email, options = {})
15
+ Request::Get.send(self,
16
+ { :contact => email }.merge(options))
17
+ end
18
+
15
19
  def add_contact(fields)
16
- send_post(build_contact_record(fields))
20
+ Request::Post.send(self, build_contact_record(fields))
17
21
  end
18
22
 
19
23
  alias :update_contact :add_contact
20
24
 
21
25
  def change_contact_email(current_email, new_email)
22
- send_post(build_contact_record(
23
- :email => current_email, :change_email => new_email))
26
+ Request::Post.send(self, build_contact_record(
27
+ :email => current_email,
28
+ :change_email => new_email))
24
29
  end
25
30
 
26
31
  def remove_contact(email)
27
- send_post(build_contact_record( :email => email, :clear_record => 1 ))
28
- end
29
-
30
- def user_field_definitions
31
- response = send_get(:getUserFieldDefinitions => 1)
32
-
33
- if response.success?
34
- fields = {}
35
- response.data['UserFieldDefinitions']['UserField'].each_with_index do |field,i|
36
- fields[field] = "User#{i + 1}"
37
- end
38
- [ response, fields ]
39
- else
40
- [ response, {} ]
41
- end
32
+ Request::Post.send(self, build_contact_record(
33
+ :email => email,
34
+ :clear_record => 1 ))
42
35
  end
43
36
 
44
37
  private
45
38
 
46
39
  def build_contact_record(fields)
47
40
  xml = Builder::XmlMarkup.new
48
- xml.record do |r|
41
+ xml.tag!('Record') do |r|
49
42
  fields.each do |k,v|
50
43
  r.tag!(k.to_s.camelize, v)
51
44
  end
52
45
  end
53
46
  xml
54
47
  end
55
-
56
- def send_get(fields)
57
- uri = URI.parse(GET_API_URL)
58
- uri.query = fields.merge({
59
- :user_token => @user_token,
60
- :api_password => @api_password }).collect { |k,v| "#{k}=#{v}" }.join('&')
61
-
62
- Response::Get.new(Crack::XML.parse(Net::HTTP.get(uri))['Retrieve'])
63
- end
64
-
65
- def send_post(xml)
66
- response = Net::HTTP.post_form(
67
- URI.parse(POST_API_URL),
68
- { :user_token => @user_token,
69
- :api_password => @api_password,
70
- :body => xml }).body
71
- Response::Post.new(Crack::XML.parse("<Response>#{response}</Response>")['Response'])
72
- end
73
48
  end
74
49
  end
@@ -0,0 +1,28 @@
1
+ module EROI
2
+ module Request
3
+ class Get
4
+ API_URL = 'http://emailer.emailroi.com/dbadmin/xml_retrieve2.pl'
5
+
6
+ def self.send(client, fields)
7
+ uri = URI.parse(API_URL)
8
+ uri.query = fields.merge({
9
+ :user_token => client.user_token,
10
+ :api_password => client.api_password }).collect { |k,v| "#{k}=#{v}" }.join('&')
11
+ Response::Get.new(Crack::XML.parse(Net::HTTP.get(uri)))
12
+ end
13
+ end
14
+
15
+ class Post
16
+ API_URL = 'http://emailer.emailroi.com/dbadmin/xml_post.pl'
17
+
18
+ def self.send(client, xml)
19
+ response = Net::HTTP.post_form(
20
+ URI.parse(API_URL),
21
+ { :user_token => client.user_token,
22
+ :api_password => client.api_password,
23
+ :xml_body => xml }).body
24
+ Response::Post.new(Crack::XML.parse("<Response>#{response}</Response>"))
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/eroi/response.rb CHANGED
@@ -12,9 +12,15 @@ module EROI
12
12
 
13
13
  class Get < Base
14
14
  def success?
15
- !@data['ErrorCode']
15
+ (@data['ErrorCode'] || @data['xml']) ? false : true
16
16
  end
17
17
 
18
+ def contact
19
+ @data['Retrieve']['Record']
20
+ end
21
+
22
+ alias :contacts :contact
23
+
18
24
  def error_message
19
25
  case @data['ErrorCode'].to_i
20
26
  when 1
@@ -29,13 +35,13 @@ module EROI
29
35
 
30
36
  class Post < Base
31
37
  def success?
32
- @data['Compiled'] == 'Yes' &&
33
- @data['DBConnect'] == 'OK' &&
34
- @data['XMLUpload'] == 'Complete'
38
+ @data['Response']['Compiled'] == 'Yes' &&
39
+ @data['Response']['DBConnect'] == 'OK' &&
40
+ @data['Response']['XMLUpload'] == 'Complete'
35
41
  end
36
42
 
37
43
  def number_of_records
38
- @data['ImportRecords'].to_i
44
+ @data['Response']['ImportRecords'].to_i
39
45
  end
40
46
  end
41
47
  end
data/lib/eroi.rb CHANGED
@@ -5,8 +5,9 @@ require 'net/http'
5
5
  require 'uri'
6
6
  require 'crack'
7
7
  require 'builder'
8
- require 'activesupport'
8
+ require 'active_support'
9
9
 
10
10
  require 'eroi/version'
11
+ require 'eroi/request'
11
12
  require 'eroi/response'
12
13
  require 'eroi/client'
@@ -3,17 +3,31 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
3
3
  class TestClient < Test::Unit::TestCase
4
4
  context "client module" do
5
5
  should "create a new eroi client" do
6
- client = EROI.new(user_token, api_password)
6
+ credentials = fixture(:test)
7
+ client = EROI.new(credentials[:user_token], credentials[:api_password])
7
8
  assert_equal EROI::Client, client.class
8
9
  end
9
10
  end
10
11
 
11
12
  context "using the eroi client" do
12
13
  setup do
13
- @client = EROI.new(user_token, api_password)
14
+ credentials = fixture(:test)
15
+ @client = EROI.new(credentials[:user_token], credentials[:api_password])
14
16
  FakeWeb.register_uri(
15
- :post, EROI::Client::POST_API_URL,
17
+ :post, EROI::Request::Post::API_URL,
16
18
  :body => successful_post_response)
19
+ FakeWeb.register_uri(
20
+ :get, /#{EROI::Request::Get::API_URL}*/,
21
+ :body => successful_get_response)
22
+ end
23
+
24
+ context "when finding a contact" do
25
+ should "respond with a success" do
26
+ response = @client.contact('longbob@longbob.com', :mailing_lists => 'TestList')
27
+
28
+ assert_equal true, response.success?
29
+ assert_equal 'longbob@longbob.com', response.contact['Email']
30
+ end
17
31
  end
18
32
 
19
33
  context "when adding a contact" do
@@ -22,7 +36,7 @@ class TestClient < Test::Unit::TestCase
22
36
  :email => 'longbob@longbob.com',
23
37
  :firstname => 'Longbob',
24
38
  :lastname => 'Longson',
25
- :mailing_lists => 'List1,List2')
39
+ :mailing_lists => 'TestList')
26
40
 
27
41
  assert_equal true, response.success?
28
42
  assert_equal 1, response.number_of_records
@@ -45,7 +59,7 @@ class TestClient < Test::Unit::TestCase
45
59
  :email => 'longbob@longbob.com',
46
60
  :firstname => 'Longbob',
47
61
  :lastname => 'Longson',
48
- :mailing_lists => 'List1,List2')
62
+ :mailing_lists => 'TestList')
49
63
 
50
64
  assert_equal true, response.success?
51
65
  assert_equal 1, response.number_of_records
@@ -61,36 +75,62 @@ class TestClient < Test::Unit::TestCase
61
75
  end
62
76
  end
63
77
 
64
- context "when retreiving user field definitions" do
65
- setup do
66
- FakeWeb.register_uri(
67
- :get, /#{EROI::Client::GET_API_URL}*/,
68
- :body => successful_get_response)
69
- end
70
-
71
- should "respond with a success" do
72
- response, user_fields = @client.user_field_definitions
73
-
74
- expected_fields = { 'State' => 'User1', 'City' => 'User2' }
75
-
76
- assert_equal true, response.success?
77
- assert_equal expected_fields, user_fields
78
- end
79
- end
80
-
81
78
  context "when there is an error" do
82
79
  setup do
83
80
  FakeWeb.register_uri(
84
- :get, /#{EROI::Client::GET_API_URL}*/,
81
+ :get, /#{EROI::Request::Get::API_URL}*/,
85
82
  :body => unsuccessful_get_response(1))
86
83
  end
87
84
 
88
85
  should "respond with a failure" do
89
- response, fields = @client.user_field_definitions
86
+ response, fields = @client.contact('longbob@longbob.com')
90
87
 
91
88
  assert_equal false, response.success?
92
- assert /Invalid/ =~ response.error_message
93
89
  end
94
90
  end
95
91
  end
92
+
93
+ def successful_post_response
94
+ <<-EOF
95
+ <Compiled>Yes</Compiled>
96
+ <DBConnect>OK</DBConnect>
97
+ <EditionSuccess>MailingListName_someEditionName</EditionSuccess>
98
+ <ImportRecords>1</ImportRecords>
99
+ <ExistingRecords>1526</ExistingRecords>
100
+ <FinalCompleted>1</FinalCompleted>
101
+ <Duplicates>1</Duplicates>
102
+ <InvalidLists>0</InvalidLists>
103
+ <Triggers></Triggers>
104
+ <XMLUpload>Complete</XMLUpload>
105
+ EOF
106
+ end
107
+
108
+ def successful_get_response
109
+ <<-EOF
110
+ <Retrieve>
111
+ <Record>
112
+ <rec>523</rec>
113
+ <Email>longbob@longbob.com</Email>
114
+ <Firstname>Joe</Firstname>
115
+ <Lastname>Somebody</Lastname>
116
+ <Company>Some Company</Company>
117
+ <User1>some data here</User1>
118
+ <User2>We'll put more data here</User2>
119
+ <Notes>And we'll put more notes here</Notes>
120
+ <Edition Name='SomeEdition'>
121
+ <Sent Format='YYYYMMDDhhmm'>20030913143010</Sent>
122
+ <Read>5</Read>
123
+ <Click URL='http://www.somelink.com'>3</Click>
124
+ <Click URL='http://www.anotherlink.com/page.htm'>1</Click>
125
+ <S2F>2</S2F>
126
+ </Edition>
127
+ <Event id='1' ListEdition='somelist_someedition' Date='2003-Nov-11'>Sent</Event>
128
+ </Record>
129
+ </Retrieve>
130
+ EOF
131
+ end
132
+
133
+ def unsuccessful_get_response(code = 1)
134
+ "<xml><error>Unable to authorize supplied username and password</error></xml>"
135
+ end
96
136
  end
@@ -0,0 +1,7 @@
1
+ test:
2
+ user_token: testusertoken
3
+ api_password: testapipassword
4
+
5
+ remote:
6
+ user_token: remoteusertoken
7
+ api_password: remoteapipassword
@@ -0,0 +1,110 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ context "using the eroi client" do
5
+ context "against a remote server" do
6
+ setup do
7
+ FakeWeb.allow_net_connect = true
8
+ credentials = fixture(:remote)
9
+ @client = EROI.new(credentials[:user_token], credentials[:api_password])
10
+ end
11
+
12
+ context "when finding a contact" do
13
+ should "respond with a success" do
14
+ @client.add_contact(
15
+ :email => 'longbob@longbob.com',
16
+ :firstname => 'Longbob',
17
+ :lastname => 'Longson',
18
+ :mailing_lists => 'MainList')
19
+
20
+ response = @client.contact('longbob@longbob.com', :mailing_lists => 'MainList')
21
+
22
+ assert_equal true, response.success?
23
+ assert_equal 'longbob@longbob.com', response.contact['Email']
24
+ end
25
+ end
26
+
27
+ context "when adding a contact" do
28
+ teardown do
29
+ @client.remove_contact('longbob@boblong.com')
30
+ end
31
+
32
+ should "respond with a success" do
33
+ response = @client.add_contact(
34
+ :email => 'longbob@longbob.com',
35
+ :firstname => 'Longbob',
36
+ :lastname => 'Longson',
37
+ :mailing_lists => 'MainList')
38
+
39
+ assert_equal true, response.success?
40
+ assert_equal 1, response.number_of_records
41
+ end
42
+ end
43
+
44
+ context "when changing a contact's email" do
45
+ teardown do
46
+ @client.remove_contact('longbob@longbob.com')
47
+ @client.remove_contact('longbob@boblong.com')
48
+ end
49
+
50
+ should "respond with a success" do
51
+ @client.add_contact(
52
+ :email => 'longbob@longbob.com',
53
+ :firstname => 'Longbob',
54
+ :lastname => 'Longson',
55
+ :mailing_lists => 'MainList')
56
+
57
+ response = @client.change_contact_email(
58
+ 'longbob@longbob.com', 'longbob@boblong.com')
59
+
60
+ assert_equal true, response.success?
61
+ assert_equal 1, response.number_of_records
62
+ end
63
+ end
64
+
65
+ context "when updating a contact" do
66
+ teardown do
67
+ @client.remove_contact('longbob@longbob.com')
68
+ end
69
+
70
+ should "respond with a success" do
71
+ @client.add_contact(
72
+ :email => 'longbob@longbob.com',
73
+ :firstname => 'Longbob',
74
+ :lastname => 'Longson',
75
+ :mailing_lists => 'MainList')
76
+
77
+ response = @client.update_contact(
78
+ :email => 'longbob@longbob.com',
79
+ :firstname => 'Longbob',
80
+ :lastname => 'Longson',
81
+ :mailing_lists => 'MainList')
82
+
83
+ assert_equal true, response.success?
84
+ assert_equal 1, response.number_of_records
85
+ end
86
+ end
87
+
88
+ context "when removing a contact" do
89
+ should "respond with a success" do
90
+ response = @client.remove_contact('longbob@longbob.com')
91
+
92
+ assert_equal true, response.success?
93
+ assert_equal 1, response.number_of_records
94
+ end
95
+ end
96
+
97
+ context "when there is an error" do
98
+ setup do
99
+ @client = EROI.new('wrong', 'credentials')
100
+ end
101
+
102
+ should "respond with a failure" do
103
+ response, fields = @client.contact('longbob@longbob.com')
104
+
105
+ assert_equal false, response.success?
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
data/test/test_helper.rb CHANGED
@@ -21,58 +21,14 @@ def stub_get(url, filename, status=nil)
21
21
  FakeWeb.register_uri(:get, url, options)
22
22
  end
23
23
 
24
- def user_token
25
- 'test_user_token'
24
+ def fixture(key)
25
+ yaml_data = YAML.load(File.read(File.join(File.dirname(__FILE__), 'fixtures.yml')))
26
+ symbolize_keys(yaml_data)
27
+ yaml_data[key]
26
28
  end
27
29
 
28
- def api_password
29
- 'test_api_password'
30
- end
31
-
32
- def successful_post_response
33
- <<-EOF
34
- <Compiled>Yes</Compiled>
35
- <DBConnect>OK</DBConnect>
36
- <EditionSuccess>MailingListName_someEditionName</EditionSuccess>
37
- <ImportRecords>1</ImportRecords>
38
- <ExistingRecords>1526</ExistingRecords>
39
- <FinalCompleted>1</FinalCompleted>
40
- <Duplicates>1</Duplicates>
41
- <InvalidLists>0</InvalidLists>
42
- <Triggers></Triggers>
43
- <XMLUpload>Complete</XMLUpload>
44
- EOF
45
- end
46
-
47
- def successful_get_response
48
- <<-EOF
49
- <Retrieve>
50
- <Record>
51
- <rec>523</rec>
52
- <Email>someone@somecompany.com</Email>
53
- <Firstname>Joe</Firstname>
54
- <Lastname>Somebody</Lastname>
55
- <Company>Some Company</Company>
56
- <User1>some data here</User1>
57
- <User2>We'll put more data here</User2>
58
- <Notes>And we'll put more notes here</Notes>
59
- <Edition Name="SomeEdition">
60
- <Sent Format="YYYYMMDDhhmm">20030913143010</Sent>
61
- <Read>5</Read>
62
- <Click URL="http://www.somelink.com">3</Click>
63
- <Click URL="http://www.anotherlink.com/page.htm">1</Click>
64
- <S2F>2</S2F>
65
- </Edition>
66
- <Event id="1" ListEdition="somelist_someedition" Date="2003-Nov-11">Sent</Event>
67
- </Record>
68
- <UserFieldDefinitions>
69
- <UserField Field="User1" Type="Text">State</UserField>
70
- <UserField Field="User2" Type="Text">City</UserField>
71
- </UserFieldDefinitions>
72
- </Retrieve>
73
- EOF
74
- end
75
-
76
- def unsuccessful_get_response(code = 1)
77
- "<Retrieve><ErrorCode>#{code}</ErrorCode></Retrieve>"
30
+ def symbolize_keys(hash)
31
+ return unless hash.is_a?(Hash)
32
+ hash.symbolize_keys!
33
+ hash.each{|k,v| symbolize_keys(v)}
78
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eroi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - CardPlayer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-03 00:00:00 -08:00
12
+ date: 2009-12-04 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
25
  files:
26
+ - .gitignore
26
27
  - LICENSE
27
28
  - Manifest
28
29
  - README.rdoc
@@ -31,9 +32,12 @@ files:
31
32
  - eroi.gemspec
32
33
  - lib/eroi.rb
33
34
  - lib/eroi/client.rb
35
+ - lib/eroi/request.rb
34
36
  - lib/eroi/response.rb
35
37
  - lib/eroi/version.rb
36
38
  - test/eroi/client_test.rb
39
+ - test/fixtures.yml.sample
40
+ - test/remote/client_test.rb
37
41
  - test/test_helper.rb
38
42
  has_rdoc: true
39
43
  homepage: http://github.com/cardplayer/eroi
@@ -65,4 +69,5 @@ specification_version: 3
65
69
  summary: API interface to eROI.
66
70
  test_files:
67
71
  - test/eroi/client_test.rb
72
+ - test/remote/client_test.rb
68
73
  - test/test_helper.rb