midwife-client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Midwife
2
2
  class Client
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -1,7 +1,6 @@
1
1
  require "net/http"
2
2
  require "uri"
3
3
  require "json"
4
- require 'excon'
5
4
 
6
5
  module Midwife
7
6
  class RequestError < StandardError
@@ -16,9 +15,17 @@ module Midwife
16
15
  private
17
16
 
18
17
  def request(path, attributes)
19
- response = Excon.post(@endpoint + path, :query => attributes)
18
+ uri = URI.parse(@endpoint + path)
20
19
 
21
- if response.status.to_s =~ /2../
20
+ request = Net::HTTP::Post.new(uri.path)
21
+ request["content-type"] = "application/json"
22
+ request.body = attributes.to_json
23
+
24
+ response = Net::HTTP.start(uri.host, uri.port) { |http|
25
+ http.request(request)
26
+ }
27
+
28
+ if response.code =~ /2../
22
29
  JSON.parse(response.body)
23
30
  else
24
31
  raise RequestError.new(response)
@@ -43,9 +50,6 @@ module Midwife
43
50
  raise ArgumentError, "list_id is required"
44
51
  }
45
52
 
46
- # transform into an array
47
- attributes['subscription_ids[]'] = attributes.delete(:subscription_ids) || []
48
-
49
53
  request "/lists/#{list_id}/recipients", attributes
50
54
  end
51
55
 
@@ -65,6 +69,12 @@ module Midwife
65
69
  request "/campaigns/#{campaign_id}/deliver", attributes
66
70
  end
67
71
 
72
+ def deliver_campaign_test(attributes)
73
+ campaign_id = attributes.delete(:campaign_id)
74
+
75
+ request "/campaigns/#{campaign_id}/deliver_test", attributes
76
+ end
77
+
68
78
  def unsubscribe(attributes)
69
79
  request "/unsubscribe", attributes
70
80
  end
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_dependency 'json'
23
- s.add_dependency 'excon', '~> 0.6.3'
24
23
 
25
24
  s.add_development_dependency 'rspec', '~> 2.5'
26
25
  end
data/spec/client_spec.rb CHANGED
@@ -33,14 +33,6 @@ describe Midwife::Client do
33
33
 
34
34
  before do
35
35
  client.reset
36
- @campaign_attributes = {
37
- 'name' => 'April 2011 eVetTen',
38
- 'from_name' => 'Robert Eversole',
39
- 'from_email' => 'robert.eversole@recruitmilitary.com',
40
- 'subject' => '[RecruitMilitary] April eVetTen',
41
- 'html_body' => '<p>Hello world</p>',
42
- 'plain_body' => 'hello world'
43
- }
44
36
  end
45
37
 
46
38
  def last_request
@@ -63,7 +55,7 @@ describe Midwife::Client do
63
55
 
64
56
  last_request.path.should == "/lists/42/recipients"
65
57
  last_request.method.should == :post
66
- last_request.params.should == { :email => 'michael@jordan.com', 'subscription_ids[]' => [1, 2] }
58
+ last_request.params.should == { :email => 'michael@jordan.com', :subscription_ids => [1, 2] }
67
59
  end
68
60
 
69
61
  context 'when list_id is missing' do
@@ -77,11 +69,26 @@ describe Midwife::Client do
77
69
 
78
70
  describe '#create_campaign' do
79
71
  it 'creates a campaign' do
80
- client.create_campaign(@campaign_attributes)
72
+ client.create_campaign(
73
+ :filters => [{:column => 'location', :operator => 'near', :value => '45140', :extras => { :distance => 150 }}],
74
+ :name => 'April 2011 eVetTen',
75
+ :from_name => 'Robert Eversole',
76
+ :from_email => 'robert.eversole@recruitmilitary.com',
77
+ :subject => '[RecruitMilitary] April eVetTen',
78
+ :html_body => '<p>Hello world</p>',
79
+ :plain_body => 'hello world')
81
80
 
82
81
  last_request.path.should == "/campaigns"
83
82
  last_request.method.should == :post
84
- last_request.params.should == @campaign_attributes
83
+ last_request.params.should == {
84
+ :filters => [{:column => 'location', :operator => 'near', :value => '45140', :extras => { :distance => 150 }}],
85
+ :name => 'April 2011 eVetTen',
86
+ :from_name => 'Robert Eversole',
87
+ :from_email => 'robert.eversole@recruitmilitary.com',
88
+ :subject => '[RecruitMilitary] April eVetTen',
89
+ :html_body => '<p>Hello world</p>',
90
+ :plain_body => 'hello world'
91
+ }
85
92
  end
86
93
  end
87
94
 
@@ -120,15 +127,25 @@ describe Midwife::Client do
120
127
  last_request.params.should == { :email => 'foo@bar.com', :list_id => 42 }
121
128
  end
122
129
  end
130
+
131
+ describe '#deliver_campaign_test' do
132
+ it 'generates the correct request' do
133
+ client.deliver_campaign_test(:campaign_id => 42, :emails => ['a@b.com', 'c@d.com'])
134
+
135
+ last_request.path.should == "/campaigns/42/deliver_test"
136
+ last_request.method.should == :post
137
+ last_request.params.should == { :emails => ['a@b.com', 'c@d.com'] }
138
+ end
139
+ end
123
140
  end
124
141
 
125
142
  context 'when processing a non-success response' do
126
143
  let(:client) { Midwife::Client.new }
127
- let(:response) { double(:status => 404) }
144
+ let(:response) { double(:code => '404') }
128
145
 
129
146
 
130
147
  before do
131
- Excon.stub!(:post).and_return(response)
148
+ Net::HTTP.stub(:start).and_return(response)
132
149
  end
133
150
 
134
151
  it 'raises an exception' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midwife-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Guterl
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-23 00:00:00 -04:00
18
+ date: 2011-05-25 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,26 +32,10 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: excon
37
- prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 1
44
- segments:
45
- - 0
46
- - 6
47
- - 3
48
- version: 0.6.3
49
- type: :runtime
50
- version_requirements: *id002
51
35
  - !ruby/object:Gem::Dependency
52
36
  name: rspec
53
37
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
55
39
  none: false
56
40
  requirements:
57
41
  - - ~>
@@ -62,7 +46,7 @@ dependencies:
62
46
  - 5
63
47
  version: "2.5"
64
48
  type: :development
65
- version_requirements: *id003
49
+ version_requirements: *id002
66
50
  description: A ruby wrapper for the Midwife HTTP API
67
51
  email:
68
52
  - mguterl@gmail.com