jimmyz-fs-communicator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/fs_communicator.rb +18 -1
- data/spec/fs_communicator_spec.rb +71 -2
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/fs_communicator.rb
CHANGED
@@ -18,6 +18,23 @@ class FsCommunicator
|
|
18
18
|
@session = o[:session]
|
19
19
|
end
|
20
20
|
|
21
|
+
def post(url,payload)
|
22
|
+
uri = URI.parse(self.domain+url)
|
23
|
+
full_url = set_extra_params(uri)
|
24
|
+
request = Net::HTTP::Post.new(full_url)
|
25
|
+
request.body = payload
|
26
|
+
request['Content-Type'] = "text/xml"
|
27
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
28
|
+
if uri.scheme == 'https'
|
29
|
+
http.use_ssl = true
|
30
|
+
http.ca_file = File.join File.dirname(__FILE__), 'assets','entrust-ca.crt'
|
31
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
32
|
+
end
|
33
|
+
res = http.start do |ht|
|
34
|
+
ht.request(request)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
21
38
|
def get(url,credentials = {})
|
22
39
|
uri = URI.parse(self.domain+url)
|
23
40
|
full_url = set_extra_params(uri,credentials)
|
@@ -38,7 +55,7 @@ class FsCommunicator
|
|
38
55
|
end
|
39
56
|
|
40
57
|
private
|
41
|
-
def set_extra_params(uri,credentials)
|
58
|
+
def set_extra_params(uri,credentials = {})
|
42
59
|
if credentials[:username] && credentials[:password]
|
43
60
|
sessionized_url = add_key(uri)
|
44
61
|
else
|
@@ -3,11 +3,13 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
3
3
|
module HttpCommunicatorHelper
|
4
4
|
|
5
5
|
def stub_net_objects
|
6
|
-
@request = mock("Net::HTTP::Get")
|
7
|
-
|
6
|
+
@request = mock("Net::HTTP::Get|Post")
|
7
|
+
@request.stub!(:[]=)
|
8
|
+
@request.stub!(:body=)
|
8
9
|
@http = mock("Net::HTTP")
|
9
10
|
Net::HTTP.stub!(:new).and_return(@http)
|
10
11
|
Net::HTTP::Get.stub!(:new).and_return(@request)
|
12
|
+
Net::HTTP::Post.stub!(:new).and_return(@request)
|
11
13
|
@http.stub!(:use_ssl=)
|
12
14
|
@http.stub!(:ca_file=)
|
13
15
|
@http.stub!(:verify_mode=)
|
@@ -114,4 +116,71 @@ describe FsCommunicator do
|
|
114
116
|
|
115
117
|
end
|
116
118
|
|
119
|
+
describe "POST on a URL" do
|
120
|
+
before(:each) do
|
121
|
+
options = {
|
122
|
+
:domain => 'https://api.familysearch.org',
|
123
|
+
:key => '1111-1111',
|
124
|
+
:user_agent => "FsCommunicator/0.1",
|
125
|
+
:session => 'SESSID'
|
126
|
+
}
|
127
|
+
@com = FsCommunicator.new options
|
128
|
+
stub_net_objects
|
129
|
+
@url = '/familytree/v1/person/KWQS-BBQ'
|
130
|
+
@session_url = @url + "?sessionId=#{@com.session}"
|
131
|
+
@payload = "<familytree></familytree>"
|
132
|
+
end
|
133
|
+
|
134
|
+
def do_post(url, payload = '')
|
135
|
+
@com.post(url, payload)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should initialize a Net::HTTP object to make the request" do
|
139
|
+
Net::HTTP.should_receive(:new).with('api.familysearch.org',443).and_return(@http)
|
140
|
+
do_post(@url)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should create a POST request with url containing a session" do
|
144
|
+
Net::HTTP::Post.should_receive(:new).with(@session_url).and_return(@request)
|
145
|
+
do_post(@url)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should tack a sessionId as an additional parameter if params already set" do
|
149
|
+
url = "/familytree/v1/person/KWQS-BBQ?view=summary"
|
150
|
+
Net::HTTP::Post.should_receive(:new).with(url+"&sessionId=#{@com.session}").and_return(@request)
|
151
|
+
do_post(url)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should set the request's body to the payload attached" do
|
155
|
+
@request.should_receive(:body=).with(@payload)
|
156
|
+
@request.should_receive(:[]=).with('Content-Type','text/xml')
|
157
|
+
do_post(@url,@payload)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should set the http object to use ssl if https" do
|
161
|
+
@http.should_receive(:use_ssl=).with(true)
|
162
|
+
do_post(@url)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should not set the http object to use ssl if no http" do
|
166
|
+
@com.domain = 'http://www.dev.usys.org'
|
167
|
+
@http.should_not_receive(:use_ssl=)
|
168
|
+
do_post(@url)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should set the ca file to the entrust certificate (for FamilySearch systems)" do
|
172
|
+
@http.should_receive(:ca_file=).with(File.join(File.dirname(__FILE__),'..','lib','assets','entrust-ca.crt'))
|
173
|
+
do_post(@url)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should make the request" do
|
177
|
+
block = lambda{ |ht|
|
178
|
+
ht.request('something')
|
179
|
+
}
|
180
|
+
@http.should_receive(:start)
|
181
|
+
do_post(@url)
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
117
186
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jimmyz-fs-communicator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Zimmerman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-04 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|