placid 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +5 -0
- data/History.md +7 -0
- data/lib/placid/exceptions.rb +5 -0
- data/lib/placid/helper.rb +13 -2
- data/lib/placid/model.rb +1 -1
- data/placid.gemspec +1 -1
- data/spec/placid_helper_spec.rb +36 -6
- data/spec/placid_model_spec.rb +2 -1
- metadata +4 -4
data/.yardopts
CHANGED
data/History.md
CHANGED
data/lib/placid/exceptions.rb
CHANGED
data/lib/placid/helper.rb
CHANGED
@@ -51,7 +51,12 @@ module Placid
|
|
51
51
|
# Optional parameters to send in the request.
|
52
52
|
#
|
53
53
|
# @return [Hash]
|
54
|
-
# Parsed response
|
54
|
+
# Parsed response
|
55
|
+
#
|
56
|
+
# @raise [RestConnectionError]
|
57
|
+
# If there is a problem connecting to the REST API
|
58
|
+
# @raise [JSONParseError]
|
59
|
+
# If the response from the REST API cannot be parsed as JSON
|
55
60
|
#
|
56
61
|
def request(method, *path)
|
57
62
|
method = method.to_sym
|
@@ -68,7 +73,13 @@ module Placid
|
|
68
73
|
rescue => e
|
69
74
|
raise
|
70
75
|
end
|
71
|
-
|
76
|
+
# Try to parse the response
|
77
|
+
begin
|
78
|
+
return JSON.parse(response)
|
79
|
+
rescue => e
|
80
|
+
raise JSONParseError.new(
|
81
|
+
"Cannot parse response from #{rest_url} as JSON", response)
|
82
|
+
end
|
72
83
|
end
|
73
84
|
|
74
85
|
# Send a GET to a path that returns a single JSON object, and return the
|
data/lib/placid/model.rb
CHANGED
@@ -96,7 +96,7 @@ module Placid
|
|
96
96
|
@meta ||= get_mash(model, 'meta')
|
97
97
|
end
|
98
98
|
|
99
|
-
# Return
|
99
|
+
# Return an array of Hashie::Mashes for all model instances.
|
100
100
|
#
|
101
101
|
def self.list
|
102
102
|
return get_mashes(model.pluralize).map do |mash|
|
data/placid.gemspec
CHANGED
data/spec/placid_helper_spec.rb
CHANGED
@@ -59,24 +59,31 @@ describe Placid::Helper do
|
|
59
59
|
end.should raise_error(URI::InvalidURIError)
|
60
60
|
end
|
61
61
|
|
62
|
-
it "
|
62
|
+
it "raises a JSONParseError if there is no response" do
|
63
63
|
RestClient.stub(:get) { nil }
|
64
|
-
|
65
|
-
|
64
|
+
lambda do
|
65
|
+
json = request('get')
|
66
|
+
end.should raise_error(Placid::JSONParseError, /Cannot parse/)
|
66
67
|
end
|
67
68
|
|
68
69
|
it "accepts a params hash as the last argument" do
|
69
|
-
RestClient.should_receive(:post).
|
70
|
+
RestClient.should_receive(:post).
|
71
|
+
with('http://localhost/foo', {:bar => 'hi'}).
|
72
|
+
and_return('{}')
|
70
73
|
json = request('post', 'foo', :bar => 'hi')
|
71
74
|
end
|
72
75
|
|
73
76
|
it "sends an empty params hash if none is given" do
|
74
|
-
RestClient.should_receive(:post).
|
77
|
+
RestClient.should_receive(:post).
|
78
|
+
with('http://localhost/foo', {}).
|
79
|
+
and_return('{}')
|
75
80
|
json = request('post', 'foo')
|
76
81
|
end
|
77
82
|
|
78
83
|
it "sends :params => params for get requests" do
|
79
|
-
RestClient.should_receive(:get).
|
84
|
+
RestClient.should_receive(:get).
|
85
|
+
with('http://localhost/foo', {:params => {:x => 'y'}}).
|
86
|
+
and_return('{}')
|
80
87
|
json = request('get', 'foo', :x => 'y')
|
81
88
|
end
|
82
89
|
end
|
@@ -89,10 +96,33 @@ describe Placid::Helper do
|
|
89
96
|
}
|
90
97
|
RestClient.stub(:get => JSON(data))
|
91
98
|
mash = get_mash
|
99
|
+
mash.should be_a(Hashie::Mash)
|
92
100
|
mash.first_name.should == 'Nathan'
|
93
101
|
mash.last_name.should == 'Stark'
|
94
102
|
end
|
95
103
|
|
104
|
+
it "returns nested Hashie::Mashes for nested hash data" do
|
105
|
+
data = {
|
106
|
+
'person' => {
|
107
|
+
'first_name' => 'Jack',
|
108
|
+
'last_name' => 'Carter',
|
109
|
+
},
|
110
|
+
'address' => {
|
111
|
+
'street' => '123 Main Street',
|
112
|
+
'city' => 'Eureka',
|
113
|
+
},
|
114
|
+
}
|
115
|
+
RestClient.stub(:get => JSON(data))
|
116
|
+
mash = get_mash
|
117
|
+
mash.should be_a(Hashie::Mash)
|
118
|
+
mash.person.should be_a(Hashie::Mash)
|
119
|
+
mash.person.first_name.should == 'Jack'
|
120
|
+
mash.person.last_name.should == 'Carter'
|
121
|
+
mash.address.should be_a(Hashie::Mash)
|
122
|
+
mash.address.street.should == '123 Main Street'
|
123
|
+
mash.address.city.should == 'Eureka'
|
124
|
+
end
|
125
|
+
|
96
126
|
it "raises an exception when the JSON cannot be parsed" do
|
97
127
|
data = ['a', 'b', 'c']
|
98
128
|
RestClient.stub(:get => JSON(data))
|
data/spec/placid_model_spec.rb
CHANGED
@@ -168,7 +168,8 @@ describe Placid::Model do
|
|
168
168
|
it "can call #request on an instance" do
|
169
169
|
thing = Thing.new
|
170
170
|
RestClient.should_receive(:get).
|
171
|
-
with('http://localhost/thing/foo', {:params => {:x => 'y'}})
|
171
|
+
with('http://localhost/thing/foo', {:params => {:x => 'y'}}).
|
172
|
+
and_return('{}')
|
172
173
|
thing.request(:get, 'thing', 'foo', :x => 'y')
|
173
174
|
end
|
174
175
|
end
|
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: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
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-07-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: hashie
|