streamsend-ruby 0.0.2.pre1 → 0.0.2.pre2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
1
  source :rubygems
2
2
 
3
3
  gem 'httparty'
4
+ gem 'xml-simple'
4
5
 
5
6
  group :development, :test do
6
7
  gem 'webmock'
7
8
  gem 'rspec', "< 2.0"
8
- end
9
+ end
@@ -4,7 +4,7 @@ module Spec # :nodoc:
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
6
  TINY = 2
7
- PRE = "pre1" # VALID VALUES: nil, "pre1", "pre2", ...
7
+ PRE = "pre2" # VALID VALUES: nil, "pre1", "pre2", ...
8
8
 
9
9
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
10
10
 
@@ -1,13 +1,14 @@
1
1
  require 'rubygems'
2
2
  require 'httparty'
3
+ require "xmlsimple"
3
4
 
4
5
  module StreamSend
5
6
  include HTTParty
6
7
  format :xml
7
8
 
8
9
  def self.configure(username, password, host = "app.streamsend.com")
9
- base_uri host
10
- basic_auth username, password
10
+ base_uri(host)
11
+ basic_auth(username, password)
11
12
  end
12
13
 
13
14
  class Resource
@@ -20,10 +21,6 @@ module StreamSend
20
21
  @new_record
21
22
  end
22
23
 
23
- def saved!
24
- @new_record = false
25
- end
26
-
27
24
  def method_missing(method, *args, &block)
28
25
  if method.to_s.match(/^(.*)=$/)
29
26
  @data[$1] = *args
@@ -34,6 +31,14 @@ module StreamSend
34
31
  end
35
32
  end
36
33
 
34
+ def errors=(vals = [])
35
+ @errors = vals
36
+ end
37
+
38
+ def errors
39
+ @errors
40
+ end
41
+
37
42
  def attributes
38
43
  @data
39
44
  end
@@ -42,6 +47,14 @@ module StreamSend
42
47
  @data["id"]
43
48
  end
44
49
 
50
+ def to_xml
51
+ XmlSimple.xml_out({:person => self.attributes.reject { |_,v| v.nil? || v.blank? }}, :keep_root => true)
52
+ end
53
+
54
+ def self.to_xml(input_hash=self.attributes)
55
+ XmlSimple.xml_out({:person => input_hash.reject { |_,v| v.nil? || v.blank? }}, :keep_root => true)
56
+ end
57
+
45
58
  def self.find(options={})
46
59
  response = StreamSend.get(resource_path(options))
47
60
  case response.code
@@ -62,18 +75,15 @@ module StreamSend
62
75
 
63
76
  def save
64
77
  if self.new_record?
65
- attributes = StreamSend.post(self.class.resource_path("audience_id" => self.audience_id),
66
- self.attributes)
78
+ response = StreamSend.post(self.class.resource_path("audience_id" => self.audience_id),
79
+ :body => self.to_xml, :headers => { "Content-Type" => "application/xml" })
67
80
  self.id = attributes["id"]
68
- self.created_at = Time.now
69
- self.saved!
70
81
  else
71
- attributes = StreamSend.put(self.class.resource_path({ "id" => self.id,
82
+ response = StreamSend.put(self.class.resource_path({ "id" => self.id,
72
83
  "audience_id" => self.audience_id,
73
- }.merge( self.attributes)))
74
- self.updated_at = TIme.now
75
- self.saved!
84
+ }), :body => self.to_xml, :headers => { "Content-Type" => "application/xml" })
76
85
  end
86
+ handle_response(response)
77
87
  end
78
88
 
79
89
  def self.resource_name
@@ -84,6 +94,27 @@ module StreamSend
84
94
  def self.resource_path(options={})
85
95
  ""
86
96
  end
97
+
98
+ private
99
+ def saved!
100
+ if @new_record
101
+ self.created_at = Time.now
102
+ else
103
+ self.updated_at = Time.now
104
+ end
105
+ @new_record = false
106
+ end
107
+
108
+ def handle_response(response)
109
+ case response.code.to_i
110
+ when 200, 201
111
+ saved!
112
+ true
113
+ else
114
+ self.errors = response.parsed_response["errors"].values unless response.parsed_response.empty?
115
+ false
116
+ end
117
+ end
87
118
  end
88
119
 
89
120
  class Audience < Resource
@@ -111,18 +142,16 @@ module StreamSend
111
142
 
112
143
  case response.code
113
144
  when 200
114
- response["people"].collect { |data| new(data, true) }
145
+ response["people"].collect { |data| new(data, true)}
115
146
  else
116
147
  raise "Could not find any subscribers. Make sure your audience ID is correct. (#{response.code})"
117
148
  end
118
149
  end
119
150
 
120
- def self.create(audience_id, subscriber_hash)
121
- if attributes = StreamSend.post(resource_path("audience_id" => audience_id), subscriber_hash)
122
- new(attributes, true)
123
- else
124
- raise("Could not create the subscriber.")
125
- end
151
+ def self.create(subscriber_hash)
152
+ resource = new(subscriber_hash)
153
+ resource.save
154
+ resource
126
155
  end
127
156
 
128
157
  def self.find_by_email_address(email_address, options={})
@@ -24,13 +24,13 @@ describe "StreamSend" do
24
24
  end
25
25
  end
26
26
 
27
- describe "#id" do
27
+ describe ".id" do
28
28
  it "should return id" do
29
29
  StreamSend::Resource.new({"id" => 99}).id.should == 99
30
30
  end
31
31
  end
32
32
 
33
- describe "#attributes" do
33
+ describe ".attributes" do
34
34
  before do
35
35
  @attributes = { "id" => 99, "email_address" => "foo@example.org" }
36
36
  end
@@ -38,6 +38,42 @@ describe "StreamSend" do
38
38
  StreamSend::Resource.new(@attributes).attributes.should == @attributes
39
39
  end
40
40
  end
41
+
42
+ describe ".to_xml" do
43
+ before do
44
+ @resource = StreamSend::Resource.new("audience_id" => 2, "email_address" => "foo@example.com", "unsubscribed_at" => nil)
45
+ @xml = "<person audience_id=\"2\" email_address=\"foo@example.com\" />\n"
46
+ end
47
+
48
+ it "should convert our object to xml" do
49
+ @resource.to_xml.should == @xml
50
+ end
51
+
52
+ it "should not raise an error if there are nil values in the attributes" do
53
+ lambda { @resource.to_xml}.should_not raise_error
54
+ end
55
+
56
+ describe "#to_xml" do
57
+ it "should return xml" do
58
+ StreamSend::Resource.to_xml("audience_id" => 2, "email_address" => "foo@example.com").should == @xml
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#errors" do
64
+ before do
65
+ @resource = StreamSend::Resource.new({})
66
+ @resource.errors = ["You did it wrong!"]
67
+ end
68
+
69
+ it "should populate the errors" do
70
+ @resource.errors.should include("You did it wrong!")
71
+ end
72
+
73
+ it "should not set errors on our attributes" do
74
+ @resource.attributes.has_key?("errors").should_not be_true, "errors should not be an attribute of Resource"
75
+ end
76
+ end
41
77
  end
42
78
 
43
79
  describe "Audience" do
@@ -74,10 +110,22 @@ describe "StreamSend" do
74
110
  audience.id.should == @audience.id
75
111
  audience.created_at.should == Time.parse("2009-07-23T18:07:46Z")
76
112
  end
113
+
114
+ describe ".resource_path" do
115
+ it "should return a valid path to the resource" do
116
+ StreamSend::Audience.resource_path("id" => 1).should == "/audiences/1.xml"
117
+ end
118
+ end
77
119
  end
78
120
  end
79
121
 
80
122
  describe "Subscriber" do
123
+ describe ".resource_path" do
124
+ it "should return a valid path to the resource" do
125
+ StreamSend::Subscriber.resource_path("id" => 12, "audience_id" => 1).should == "/audiences/1/people/12.xml"
126
+ end
127
+ end
128
+
81
129
  describe ".all" do
82
130
  describe "with subscribers" do
83
131
  before(:each) do
@@ -95,7 +143,7 @@ describe "StreamSend" do
95
143
  stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").to_return(:body => xml)
96
144
  end
97
145
 
98
- it "should return array of one subscriber object" do
146
+ it "should return an array of one subscriber object" do
99
147
  subscribers = StreamSend::Subscriber.all(1)
100
148
  subscribers.size.should == 1
101
149
 
@@ -176,57 +224,21 @@ describe "StreamSend" do
176
224
  end
177
225
  end
178
226
 
179
- =begin
180
- describe "#show" do
181
- describe "with valid subscriber instance" do
182
- before(:each) do
183
- xml = <<-"XML".gsub(/^\s+/,'')
184
- <?xml version="1.0" encoding="UTF-8"?>
185
- <person>
186
- <id type="integer">2</id>
187
- <email-address>scott@gmail.com</email-address>
188
- <created-at type="datetime">2009-09-18T01:27:05Z</created-at>
189
- <first-name>Scott</first-name>
190
- <last-name>Albertson</last-name>
191
- </person>
192
- XML
193
-
194
- stub_http_request(:get, "http://#{@username}:#{@password}@#{@host}/audiences/1/people/2.xml").to_return(:body => xml)
195
- end
196
-
197
- it "should return subscriber" do
198
- subscriber = StreamSend::Subscriber.new({"id" => 2}).show(1)
199
-
200
- subscriber.should be_instance_of(StreamSend::Subscriber)
201
- subscriber.id.should == 2
202
- subscriber.email_address.should == "scott@gmail.com"
203
- subscriber.created_at.should == Time.parse("2009-09-18T01:27:05Z")
204
- subscriber.first_name.should == "Scott"
205
- subscriber.last_name.should == "Albertson"
206
- end
207
- end
208
-
209
- describe "with invalid subscriber instance" do
210
- it "should return nil" do
211
- lambda { StreamSend::Subscriber.new({:id => 99}).show(1) }.should raise_error
212
- end
213
- end
214
-
215
- describe "with invalid audience" do
216
- it "should raise exception" do
217
- lambda { StreamSend::Subscriber.new({:id => 2}).show(99) }.should raise_error
218
- end
219
- end
220
- end
221
- =end
222
-
223
227
  describe "#create" do
224
228
  before(:each) do
225
- @subscriber = StreamSend::Subscriber.new(:email_address =>"scott@example.com")
226
- stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").with(:body => @subscriber).to_return({ :response => true })
229
+ @subscriber = StreamSend::Subscriber.new(:email_address =>"scott@example.com", "audience_id" => 1)
230
+ stub_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml?").
231
+ with(:body => "<person audience_id=\"1\" email_address=\"scott@example.com\" />\n",
232
+ :headers => {'Content-Type'=>'application/xml'}).
233
+ to_return(:status => 201, :body => "", :headers => {})
234
+
235
+ @bad_subscriber = {:email_address =>"scott@example", "audience_id" => 1}
236
+
237
+ stub_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml?").
238
+ with(:body => "<person audience_id=\"1\" email_address=\"scott@example\" />\n",
239
+ :headers => {'Content-Type'=>'application/xml'}).
240
+ to_return(:status => 422, :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n <error>Email address does not appear to be valid</error>\n</errors>\n", :headers => {})
227
241
 
228
- @bad_subscriber = StreamSend::Subscriber.new(:email_address =>"scott@example")
229
- stub_http_request(:post, "http://#{@username}:#{@password}@#{@host}/audiences/1/people.xml").with(:body => @bad_subscriber).to_return({ :response => Exception })
230
242
  end
231
243
 
232
244
  describe "with valid subscriber instance" do
@@ -240,8 +252,16 @@ describe "StreamSend" do
240
252
  end
241
253
 
242
254
  describe "with an invalid email address" do
243
- it "should raise exception" do
244
- StreamSend::Subscriber.create(1, @bad_subscriber.attributes).should raise_error
255
+ before do
256
+ @subscriber = StreamSend::Subscriber.create(@bad_subscriber)
257
+ end
258
+
259
+ it "should not save and be a new record" do
260
+ @subscriber.should be_new_record
261
+ end
262
+
263
+ it "should set errors on the unsaved record" do
264
+ @subscriber.errors.should include("Email address does not appear to be valid")
245
265
  end
246
266
  end
247
267
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: streamsend-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 270495464
4
+ hash: -1876988183
5
5
  prerelease: true
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
9
  - 2
10
- - pre1
11
- version: 0.0.2.pre1
10
+ - pre2
11
+ version: 0.0.2.pre2
12
12
  platform: ruby
13
13
  authors:
14
14
  - Gaslight Software
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-08-04 00:00:00 -04:00
20
+ date: 2011-08-24 00:00:00 -04:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -116,6 +116,6 @@ rubyforge_project: streamsend-ruby
116
116
  rubygems_version: 1.3.7
117
117
  signing_key:
118
118
  specification_version: 3
119
- summary: streamsend-ruby 0.0.2.pre1
119
+ summary: streamsend-ruby 0.0.2.pre2
120
120
  test_files:
121
121
  - spec/streamsend_spec.rb