placid 0.0.2 → 0.0.3
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/History.md +7 -0
- data/lib/placid/exceptions.rb +5 -0
- data/lib/placid/helper.rb +7 -16
- data/placid.gemspec +1 -1
- data/spec/placid_helper_spec.rb +39 -0
- metadata +4 -4
data/History.md
CHANGED
data/lib/placid/exceptions.rb
CHANGED
data/lib/placid/helper.rb
CHANGED
@@ -2,6 +2,7 @@ require 'uri'
|
|
2
2
|
require 'json'
|
3
3
|
require 'hashie'
|
4
4
|
require 'rest-client'
|
5
|
+
require 'active_support/core_ext/array' # for extract_options!
|
5
6
|
require 'placid/exceptions'
|
6
7
|
|
7
8
|
module Placid
|
@@ -15,19 +16,6 @@ module Placid
|
|
15
16
|
URI.escape(text.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
16
17
|
end
|
17
18
|
|
18
|
-
# If the last arg in `args` is a hash, pop it off and return it. Otherwise,
|
19
|
-
# return an empty hash. `args` is modified in-place. Behaves like
|
20
|
-
# ActiveSupport's `String#extract_options!` method.
|
21
|
-
#
|
22
|
-
# @param [Array] args
|
23
|
-
# Zero or more arguments, the last of which might be a Hash.
|
24
|
-
#
|
25
|
-
# @return [Hash]
|
26
|
-
#
|
27
|
-
def extract_options(args)
|
28
|
-
args.last.is_a?(::Hash) ? args.pop : {}
|
29
|
-
end
|
30
|
-
|
31
19
|
# Return a full URL for a REST API request to the given path, relative to
|
32
20
|
# the configured `Placid::Config.rest_url`. Each path component is
|
33
21
|
# URI-escaped.
|
@@ -66,13 +54,16 @@ module Placid
|
|
66
54
|
#
|
67
55
|
def request(method, *path)
|
68
56
|
method = method.to_sym
|
69
|
-
params = extract_options
|
57
|
+
params = path.extract_options!
|
70
58
|
params = {:params => params} if method == :get
|
71
|
-
|
59
|
+
rest_url = url(*path)
|
72
60
|
begin
|
73
|
-
response = RestClient.send(method,
|
61
|
+
response = RestClient.send(method, rest_url, params)
|
74
62
|
rescue RestClient::Exception => e
|
75
63
|
response = e.response
|
64
|
+
rescue Errno::ECONNREFUSED => e
|
65
|
+
raise RestConnectionError,
|
66
|
+
"Could not connect to REST API: #{rest_url} (#{e.message})"
|
76
67
|
rescue => e
|
77
68
|
raise
|
78
69
|
end
|
data/placid.gemspec
CHANGED
data/spec/placid_helper_spec.rb
CHANGED
@@ -33,6 +33,13 @@ describe Placid::Helper do
|
|
33
33
|
json.should == ["fail"]
|
34
34
|
end
|
35
35
|
|
36
|
+
it "returns a RestConnectionError when connection is refused" do
|
37
|
+
RestClient.stub(:get).and_raise(Errno::ECONNREFUSED)
|
38
|
+
lambda do
|
39
|
+
json = request('get')
|
40
|
+
end.should raise_error(Placid::RestConnectionError, /Could not connect/)
|
41
|
+
end
|
42
|
+
|
36
43
|
it "passes other exceptions through" do
|
37
44
|
RestClient.stub(:get).and_raise(URI::InvalidURIError)
|
38
45
|
lambda do
|
@@ -62,6 +69,38 @@ describe Placid::Helper do
|
|
62
69
|
end
|
63
70
|
end
|
64
71
|
|
72
|
+
describe "#get" do
|
73
|
+
it "sends a GET request to the given path" do
|
74
|
+
RestClient.should_receive(:get).
|
75
|
+
with('http://localhost/foo/bar', {:params => {:baz => 'hi'}})
|
76
|
+
get('foo', 'bar', :baz => 'hi')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#post" do
|
81
|
+
it "sends a POST request to the given path" do
|
82
|
+
RestClient.should_receive(:post).
|
83
|
+
with('http://localhost/foo/bar', {:baz => 'hi'})
|
84
|
+
post('foo', 'bar', :baz => 'hi')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#put" do
|
89
|
+
it "sends a PUT request to the given path" do
|
90
|
+
RestClient.should_receive(:put).
|
91
|
+
with('http://localhost/foo/bar', {:baz => 'hi'})
|
92
|
+
put('foo', 'bar', :baz => 'hi')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#delete" do
|
97
|
+
it "sends a DELETE request to the given path" do
|
98
|
+
RestClient.should_receive(:delete).
|
99
|
+
with('http://localhost/foo/bar', {:baz => 'hi'})
|
100
|
+
delete('foo', 'bar', :baz => 'hi')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
65
104
|
describe "#get_mash" do
|
66
105
|
it "returns a Hashie::Mash for hash data" do
|
67
106
|
data = {
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: placid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Eric Pierce
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-23 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: hashie
|