midwife-client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in midwife-client.gemspec
4
+ gemspec
@@ -0,0 +1,28 @@
1
+ # Midwife Client
2
+
3
+ A wrapper for the Midwife API
4
+
5
+ ## Usage
6
+
7
+ client = Midwife::Client.new('API_KEY')
8
+
9
+ list = client.create_list(:name => 'April 2011 eVetTen')
10
+
11
+ client.create_recipient(:list_id => list['id'],
12
+ :first_name => 'Michael',
13
+ :last_name => 'Jordan',
14
+ :email => 'michael@jordan.com')
15
+
16
+ campaign = client.create_campaign(:name => 'April 2011 eVetTen',
17
+ :from_name => 'Robert Eversole',
18
+ :from_email => 'robert.eversole@recruitmilitary.com',
19
+ :subject => '[RecruitMilitary] April eVetTen',
20
+ :html_body => html_body,
21
+ :plain_body => plain_body)
22
+
23
+ client.deliver_campaign(:campaign_id => campaign['id'])
24
+
25
+ ## TODO
26
+
27
+ * Better specs (currently connects to a local instance of midwife)
28
+ * Error handling
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task :default => :spec
@@ -0,0 +1 @@
1
+ require 'midwife/client'
@@ -0,0 +1,66 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
4
+
5
+ module Midwife
6
+ class RequestError < StandardError
7
+ attr_reader :response
8
+
9
+ def initialize(response)
10
+ @response = response
11
+ end
12
+ end
13
+
14
+ module Request
15
+ private
16
+
17
+ def request(path, attributes)
18
+ uri = URI.parse(@endpoint + path)
19
+ response = Net::HTTP.post_form(uri, attributes)
20
+
21
+ if response.code =~ /2../
22
+ JSON.parse(response.body)
23
+ else
24
+ raise RequestError.new(response)
25
+ end
26
+ end
27
+ end
28
+
29
+ class Client
30
+
31
+ include Request
32
+
33
+ def initialize(endpoint = 'http://localhost:3000')
34
+ @endpoint = endpoint
35
+ end
36
+
37
+ def create_list(attributes)
38
+ request "/lists", attributes
39
+ end
40
+
41
+ def create_recipient(attributes)
42
+ list_id = attributes.delete(:list_id) {
43
+ raise ArgumentError, "list_id is required"
44
+ }
45
+
46
+ request "/lists/#{list_id}/recipients", attributes
47
+ end
48
+
49
+ def create_campaign(attributes)
50
+ request "/campaigns", attributes
51
+ end
52
+
53
+ def deliver_campaign(attributes)
54
+ campaign_id = attributes.delete(:campaign_id) {
55
+ raise ArgumentError, "campaign_id is required"
56
+ }
57
+
58
+ unless attributes.has_key?(:list_id)
59
+ raise ArgumentError, "list_id is required"
60
+ end
61
+
62
+ request "/campaigns/#{campaign_id}/deliver", attributes
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module Midwife
2
+ class Client
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "midwife/client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "midwife-client"
7
+ s.version = Midwife::Client::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Michael Guterl"]
10
+ s.email = ["mguterl@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A ruby wrapper for the Midwife HTTP API}
13
+ s.description = %q{A ruby wrapper for the Midwife HTTP API}
14
+
15
+ s.rubyforge_project = "midwife-client"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'json'
23
+
24
+ s.add_development_dependency 'rspec', '~> 2.5'
25
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ module TestRequest
4
+
5
+ class Request < Struct.new(:path, :params, :method); end
6
+
7
+ def request(path, attributes)
8
+ requests << Request.new(path, attributes, :post)
9
+ end
10
+
11
+ def requests
12
+ @requests ||= []
13
+ end
14
+
15
+ def reset
16
+ @requests = nil
17
+ end
18
+
19
+ end
20
+
21
+ describe Midwife::Client do
22
+
23
+ it 'is able to be created' do
24
+ Midwife::Client.new
25
+ end
26
+
27
+ context 'requests' do
28
+ let(:client) {
29
+ client = Midwife::Client.new
30
+ client.extend TestRequest
31
+ client
32
+ }
33
+
34
+ before do
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
+ end
45
+
46
+ def last_request
47
+ client.requests.last
48
+ end
49
+
50
+ describe '#create_list' do
51
+ it 'creates a list' do
52
+ client.create_list(:name => 'April 2011 eVetTen')
53
+
54
+ last_request.path.should == '/lists'
55
+ last_request.method.should == :post
56
+ last_request.params.should == { :name => 'April 2011 eVetTen' }
57
+ end
58
+ end
59
+
60
+ describe '#create_recipient' do
61
+ it 'adds a recipient to the list' do
62
+ client.create_recipient(:list_id => 42, :email => 'michael@jordan.com')
63
+
64
+ last_request.path.should == "/lists/42/recipients"
65
+ last_request.method.should == :post
66
+ last_request.params.should == { :email => 'michael@jordan.com' }
67
+ end
68
+
69
+ context 'when list_id is missing' do
70
+ it 'raises an error' do
71
+ expect {
72
+ client.create_recipient(:email => 'michael@jordan.com')
73
+ }.to raise_error
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#create_campaign' do
79
+ it 'creates a campaign' do
80
+ client.create_campaign(@campaign_attributes)
81
+
82
+ last_request.path.should == "/campaigns"
83
+ last_request.method.should == :post
84
+ last_request.params.should == @campaign_attributes
85
+ end
86
+ end
87
+
88
+ describe '#deliver_campaign' do
89
+ it 'delivers a campaign to the specified list' do
90
+ client.deliver_campaign(:campaign_id => 2, :list_id => 3)
91
+
92
+ last_request.path.should == "/campaigns/2/deliver"
93
+ last_request.method.should == :post
94
+ last_request.params.should == { :list_id => 3 }
95
+ end
96
+
97
+ context 'when campaign id is missing' do
98
+ it 'raises an error' do
99
+ expect {
100
+ client.deliver_campaign(:list_id => 42)
101
+ }.to raise_error
102
+ end
103
+ end
104
+
105
+ context 'when list id is missing' do
106
+ it 'raises an error' do
107
+ expect {
108
+ client.deliver_campaign(:campaign_id => 42)
109
+ }.to raise_error
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ context 'when processing a non-success response' do
116
+ let(:client) { Midwife::Client.new }
117
+ let(:response) { double(:code => '404') }
118
+
119
+
120
+ before do
121
+ Net::HTTP.stub(:post_form).and_return(response)
122
+ end
123
+
124
+ it 'raises an exception' do
125
+ expect {
126
+ client.create_list(:name => 'illegal name')
127
+ }.to raise_error(Midwife::RequestError)
128
+ end
129
+
130
+ context 'the exception' do
131
+ it 'contains the entire response' do
132
+ begin
133
+ client.create_list(:name => 'illegal name')
134
+ rescue => e
135
+ e.response.should == response
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+
4
+ require 'rspec'
5
+ require 'midwife/client'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: midwife-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Michael Guterl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-04 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 9
44
+ segments:
45
+ - 2
46
+ - 5
47
+ version: "2.5"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: A ruby wrapper for the Midwife HTTP API
51
+ email:
52
+ - mguterl@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - README.md
63
+ - Rakefile
64
+ - lib/midwife-client.rb
65
+ - lib/midwife/client.rb
66
+ - lib/midwife/client/version.rb
67
+ - midwife-client.gemspec
68
+ - spec/client_spec.rb
69
+ - spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: ""
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project: midwife-client
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A ruby wrapper for the Midwife HTTP API
104
+ test_files:
105
+ - spec/client_spec.rb