streamsend 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -28,6 +28,6 @@ PLATFORMS
28
28
  ruby
29
29
 
30
30
  DEPENDENCIES
31
- rspec (~> 2.5)
31
+ rspec (~> 2.9)
32
32
  streamsend!
33
33
  webmock (~> 1.6)
@@ -0,0 +1,36 @@
1
+ module StreamSend
2
+ class List < Resource
3
+ def self.all
4
+ response = StreamSend.get("/audiences/#{audience_id}/lists.xml")
5
+
6
+ case response.code
7
+ when 200
8
+ response["lists"].collect { |data| new(data) }
9
+ else
10
+ raise "Could not find any lists. (#{response.code})"
11
+ end
12
+ end
13
+
14
+ def self.find(list_id)
15
+ response = StreamSend.get("/audiences/#{audience_id}/lists/#{list_id}.xml")
16
+
17
+ case response.code
18
+ when 200
19
+ new(response["list"])
20
+ else
21
+ raise "Could not find any lists. (#{response.code})"
22
+ end
23
+ end
24
+
25
+ def self.create(list_name)
26
+ response = StreamSend.post("/audiences/#{audience_id}/lists.xml", :query => {:list => {:name => list_name}})
27
+
28
+ if response.code == 201
29
+ response.headers["location"] =~ /audiences\/\d\/lists\/(\d+)$/
30
+ new_list_id = $1.to_i
31
+ else
32
+ raise "Could not create a list. (#{response.body})"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -15,5 +15,9 @@ module StreamSend
15
15
  def id
16
16
  @data["id"]
17
17
  end
18
+
19
+ def self.audience_id
20
+ @audience_id ||= StreamSend.get("/audiences.xml").parsed_response["audiences"].first["id"]
21
+ end
18
22
  end
19
23
  end
@@ -1,9 +1,5 @@
1
1
  module StreamSend
2
2
  class Subscriber < Resource
3
- def self.audience_id
4
- @audience_id ||= StreamSend.get("/audiences.xml").parsed_response["audiences"].first["id"]
5
- end
6
-
7
3
  def self.all
8
4
  response = StreamSend.get("/audiences/#{audience_id}/people.xml")
9
5
 
@@ -30,6 +26,21 @@ module StreamSend
30
26
  end
31
27
  end
32
28
 
29
+ def self.create(person_hash)
30
+ response = StreamSend.post("/audiences/#{audience_id}/people.xml", :query => {:person => person_hash})
31
+
32
+ case response.code
33
+ when 201
34
+ response.headers["location"] =~ /audiences\/\d+\/people\/(\d+)$/
35
+ subscriber_id = $1
36
+ unless subscriber_id.nil?
37
+ subscriber_id.to_i
38
+ end
39
+ else
40
+ raise "Could not create the subscriber. (#{response.code})"
41
+ end
42
+ end
43
+
33
44
  def show
34
45
  response = StreamSend.get("/audiences/#{audience_id}/people/#{id}.xml")
35
46
 
data/lib/streamsend.rb CHANGED
@@ -3,7 +3,7 @@ require 'httparty'
3
3
 
4
4
  require "streamsend/resource"
5
5
  require "streamsend/subscriber"
6
- require "streamsend/version"
6
+ require "streamsend/list"
7
7
 
8
8
  module StreamSend
9
9
  include HTTParty
@@ -0,0 +1,203 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ module StreamSend
4
+ describe "List" do
5
+ let(:app_host) { "http://test.host" }
6
+ before(:each) do
7
+ stub_http_request(:any, //).to_return(:body => "Page not found.", :status => 404)
8
+
9
+ @username = "scott"
10
+ @password = "topsecret"
11
+ @host = "test.host"
12
+
13
+ xml = <<-XML
14
+ <?xml version="1.0" encoding="UTF-8"?>
15
+ <audiences type="array">
16
+ <audience>
17
+ <id type="integer">2</id>
18
+ </audience>
19
+ </audiences>
20
+ XML
21
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences.xml").to_return(:body => xml)
22
+
23
+ StreamSend.configure(@username, @password, @host)
24
+ end
25
+
26
+ describe ".audience_id" do
27
+ it "should return the id of the first audience" do
28
+ StreamSend::List.audience_id.should == 2
29
+ end
30
+ end
31
+
32
+ describe ".all" do
33
+ describe "with lists" do
34
+ before(:each) do
35
+ xml = <<-XML
36
+ <?xml version="1.0" encoding="UTF-8"?>
37
+ <lists type="array">
38
+ <list>
39
+ <active-people-count type="integer">11</active-people-count>
40
+ <checked-by-default type="boolean" nil="true"></checked-by-default>
41
+ <created-at type="datetime">2012-01-14T18:11:50Z</created-at>
42
+ <deleted-at type="datetime" nil="true"></deleted-at>
43
+ <description nil="true"></description>
44
+ <id type="integer">42</id>
45
+ <inactive-people-count type="integer">0</inactive-people-count>
46
+ <name>First list</name>
47
+ <pending-people-count type="integer">0</pending-people-count>
48
+ <public type="boolean" nil="true"></public>
49
+ <status type="enum">idle</status>
50
+ <unsubscribed-people-count type="integer">0</unsubscribed-people-count>
51
+ <updated-at type="datetime">2012-01-14T18:11:50Z</updated-at>
52
+ <audience-id type="integer">2</audience-id>
53
+ </list>
54
+ </lists>
55
+ XML
56
+
57
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/2/lists.xml").to_return(:body => xml)
58
+ end
59
+
60
+ it "should return array of one list object" do
61
+ lists = StreamSend::List.all
62
+ lists.size.should == 1
63
+
64
+ lists.first.should be_instance_of(StreamSend::List)
65
+ lists.first.id.should == 42
66
+ lists.first.name.should == "First list"
67
+ lists.first.created_at.should == Time.parse("2012-01-14T18:11:50Z")
68
+ end
69
+ end
70
+
71
+ describe "with no lists" do
72
+ before(:each) do
73
+ xml = <<-XML
74
+ <?xml version="1.0" encoding="UTF-8"?>
75
+ <lists type="array"/>
76
+ XML
77
+
78
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/2/lists.xml").to_return(:body => xml)
79
+ end
80
+
81
+ it "should return an empty array" do
82
+ StreamSend::List.all.should == []
83
+ end
84
+ end
85
+
86
+ describe "with invalid audience" do
87
+ before(:each) do
88
+ xml = <<-XML
89
+ <?xml version="1.0" encoding="UTF-8"?>
90
+ <people type="array"/>
91
+ XML
92
+
93
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/99/people.xml").to_return(:body => xml)
94
+ end
95
+
96
+ it "should raise an exception" do
97
+ lambda { StreamSend::List.all }.should raise_error
98
+ end
99
+ end
100
+ end
101
+
102
+ describe ".find" do
103
+ describe "with matching list" do
104
+ before(:each) do
105
+ xml = <<-XML
106
+ <?xml version="1.0" encoding="UTF-8"?>
107
+ <list>
108
+ <active-people-count type="integer">11</active-people-count>
109
+ <checked-by-default type="boolean" nil="true"></checked-by-default>
110
+ <created-at type="datetime">2012-01-14T18:11:50Z</created-at>
111
+ <deleted-at type="datetime" nil="true"></deleted-at>
112
+ <description nil="true"></description>
113
+ <id type="integer">42</id>
114
+ <inactive-people-count type="integer">0</inactive-people-count>
115
+ <name>First list</name>
116
+ <pending-people-count type="integer">0</pending-people-count>
117
+ <public type="boolean" nil="true"></public>
118
+ <status type="enum">idle</status>
119
+ <unsubscribed-people-count type="integer">0</unsubscribed-people-count>
120
+ <updated-at type="datetime">2012-01-14T18:11:50Z</updated-at>
121
+ <audience-id type="integer">2</audience-id>
122
+ </list>
123
+ XML
124
+
125
+ stub_http_request(:get, /audiences\/2\/lists\/42.xml/).with(:id => "42").to_return(:body => xml)
126
+ end
127
+
128
+ it "should return list" do
129
+ list = StreamSend::List.find(42)
130
+
131
+ list.should be_instance_of(StreamSend::List)
132
+ list.id.should == 42
133
+ list.name.should == "First list"
134
+ list.created_at.should == Time.parse("2012-01-14T18:11:50Z")
135
+ end
136
+ end
137
+
138
+ describe "with no matching list" do
139
+ before(:each) do
140
+ xml = <<-XML
141
+ <?xml version="1.0" encoding="UTF-8"?>
142
+ <lists type="array"\>
143
+ XML
144
+
145
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/2/people.xml?email_address=bad.email@gmail.com").to_return(:status => 404)
146
+ end
147
+
148
+ it "should return throw an exception" do
149
+ lambda {
150
+ StreamSend::List.find(-1)
151
+ }.should raise_error
152
+ end
153
+ end
154
+
155
+ describe "with invalid audience" do
156
+ before(:each) do
157
+ xml = <<-XML
158
+ <?xml version="1.0" encoding="UTF-8"?>
159
+ <people type="array"\>
160
+ XML
161
+
162
+ stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/99/people.xml?email_address=bad.email@gmail.com").to_return(:body => xml)
163
+ end
164
+
165
+ it "should raise an exception" do
166
+ lambda { StreamSend::List.find("scott@gmail.com") }.should raise_error
167
+ end
168
+ end
169
+ end
170
+
171
+
172
+ describe ".create" do
173
+ describe "with no existing list using the same name" do
174
+ before(:each) do
175
+ @new_list_name = "list 1"
176
+ new_list_params = { :list => {:name => @new_list_name} }
177
+ stub_http_request(:post, /audiences\/2\/lists.xml/).with(new_list_params).to_return(:status => 201, :headers => {:location => "#{app_host}/audiences/2/lists/42"} )
178
+ end
179
+
180
+ it "should return list" do
181
+ list_id = StreamSend::List.create(@new_list_name)
182
+ list_id.should == 42
183
+ end
184
+ end
185
+
186
+ describe "with an existing list using the same name" do
187
+ before(:each) do
188
+ @new_list_name = "list 1"
189
+ new_list_params = { :list => {:name => @new_list_name} }
190
+ @error_message = "<error>Name has already been taken<error>"
191
+ stub_http_request(:post, /audiences\/2\/lists.xml/).with(new_list_params).to_return(:status => 422, :body => @error_message)
192
+ end
193
+
194
+ it "should raise an error" do
195
+ lambda {
196
+ StreamSend::List.create(@new_list_name)
197
+ }.should raise_error
198
+ end
199
+ end
200
+ end
201
+
202
+ end
203
+ end
@@ -145,6 +145,47 @@ module StreamSend
145
145
  end
146
146
  end
147
147
 
148
+ describe ".create" do
149
+ describe "with valid subscriber parameters" do
150
+ describe "with no existing subscribers using the given email address" do
151
+ before(:each) do
152
+ stub_http_request(:post, /audiences\/2\/people.xml/).with(:person => {"email_address" => "foo@bar.com", "first_name" => "JoeBob"}).to_return(:body => "", :headers => {"location" => "http://test.host/audiences/2/people/1"}, :status => 201)
153
+ end
154
+
155
+ it "should return the new subscriber's id" do
156
+ subscriber_id = StreamSend::Subscriber.create({"email_address" => "foo@bar.com", "first_name" => "JoeBob"})
157
+
158
+ subscriber_id.should_not be_nil
159
+ subscriber_id.should == 1
160
+ end
161
+ end
162
+
163
+ describe "with an existing subscriber using the given email address" do
164
+ before(:each) do
165
+ stub_http_request(:post, /audiences\/2\/people.xml/).with(:person => {"email_address" => "foo@bar.com", "first_name" => "JoeBob"}).to_return(:body => "<error>Email address has already been taken<error>")
166
+ end
167
+
168
+ it "should raise an exception" do
169
+ lambda {
170
+ subscriber_id = StreamSend::Subscriber.create({"email_address" => "foo@bar.com", "first_name" => "JoeBob"})
171
+ }.should raise_error
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "with invalid subscriber parameters" do
177
+ before(:each) do
178
+ stub_http_request(:post, /audiences\/2\/people.xml/).with({"email_address" => "foo.com", "first_name" => "JoeBob"}).to_return(:body => "<error>Email address does not appear to be valid</error>")
179
+ end
180
+
181
+ it "should raise an exception" do
182
+ lambda {
183
+ subscriber_id = StreamSend::Subscriber.create({"email_address" => "foo@bar.com", "first_name" => "JoeBob"})
184
+ }.should raise_error
185
+ end
186
+ end
187
+ end
188
+
148
189
  describe "#show" do
149
190
  describe "with valid subscriber instance" do
150
191
  before(:each) do
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'uri'
2
+ require 'rspec/expectations'
2
3
  require 'webmock/rspec'
3
4
 
4
5
  require File.join(File.dirname(__FILE__), "../lib/streamsend")
data/streamsend.gemspec CHANGED
@@ -1,5 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/streamsend/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
4
  gem.authors = ["Scott Albertson"]
@@ -7,10 +6,10 @@ Gem::Specification.new do |gem|
7
6
  gem.summary = %q{Ruby wrapper for the StreamSend API.}
8
7
  gem.description = %q{Ruby wrapper for the StreamSend API.}
9
8
  gem.homepage = %q{http://github.com/salbertson/streamsend-ruby}
10
- gem.date = %q{2012-03-27}
9
+ gem.date = %q{2012-04-02}
11
10
 
12
11
  gem.add_dependency "httparty", "0.7.4"
13
- gem.add_development_dependency "rspec", "~> 2.5"
12
+ gem.add_development_dependency "rspec", "~> 2.9"
14
13
  gem.add_development_dependency "webmock", "~> 1.6"
15
14
 
16
15
  gem.files = `git ls-files`.split($\)
@@ -18,5 +17,5 @@ Gem::Specification.new do |gem|
18
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
18
  gem.name = "streamsend"
20
19
  gem.require_paths = ["lib"]
21
- gem.version = Streamsend::VERSION
20
+ gem.version = "0.1.2"
22
21
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: streamsend
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Scott Albertson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-27 00:00:00 Z
18
+ date: 2012-04-02 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: httparty
@@ -41,11 +41,11 @@ dependencies:
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 9
44
+ hash: 17
45
45
  segments:
46
46
  - 2
47
- - 5
48
- version: "2.5"
47
+ - 9
48
+ version: "2.9"
49
49
  type: :development
50
50
  version_requirements: *id002
51
51
  - !ruby/object:Gem::Dependency
@@ -77,9 +77,10 @@ files:
77
77
  - README.rdoc
78
78
  - Rakefile
79
79
  - lib/streamsend.rb
80
+ - lib/streamsend/list.rb
80
81
  - lib/streamsend/resource.rb
81
82
  - lib/streamsend/subscriber.rb
82
- - lib/streamsend/version.rb
83
+ - spec/lib/streamsend/list_spec.rb
83
84
  - spec/lib/streamsend/resource_spec.rb
84
85
  - spec/lib/streamsend/subscriber_spec.rb
85
86
  - spec/spec_helper.rb
@@ -119,6 +120,7 @@ signing_key:
119
120
  specification_version: 3
120
121
  summary: Ruby wrapper for the StreamSend API.
121
122
  test_files:
123
+ - spec/lib/streamsend/list_spec.rb
122
124
  - spec/lib/streamsend/resource_spec.rb
123
125
  - spec/lib/streamsend/subscriber_spec.rb
124
126
  - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- module Streamsend
2
- VERSION = "0.1.1"
3
- end