placid 0.0.4 → 0.0.5

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/.yardopts CHANGED
@@ -1 +1,6 @@
1
1
  --markup markdown
2
+ --readme README.md
3
+ lib/**/*.rb
4
+ -
5
+ History.md
6
+
data/History.md CHANGED
@@ -1,6 +1,13 @@
1
1
  Placid History
2
2
  ==============
3
3
 
4
+ 0.0.5
5
+ -----
6
+
7
+ - Raise JSONParseError on bad responses instead of returning {}
8
+ - Add `data` attribute to JSONParseError to see what failed to parse
9
+
10
+
4
11
  0.0.4
5
12
  -----
6
13
 
@@ -1,6 +1,11 @@
1
1
  module Placid
2
2
  # Error parsing a JSON response
3
3
  class JSONParseError < RuntimeError
4
+ def initialize(message, data='')
5
+ super(message)
6
+ @data = data
7
+ end
8
+ attr_reader :data
4
9
  end
5
10
 
6
11
  # Error connecting to the REST API
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, or an empty hash if parsing failed
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
- return JSON.parse(response) rescue {}
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 a Hashie::Mash with a list of all model instances.
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "placid"
3
- s.version = "0.0.4"
3
+ s.version = "0.0.5"
4
4
  s.summary = "Models from REST"
5
5
  s.description = <<-EOS
6
6
  EOS
@@ -59,24 +59,31 @@ describe Placid::Helper do
59
59
  end.should raise_error(URI::InvalidURIError)
60
60
  end
61
61
 
62
- it "returns an empty hash if there is no response" do
62
+ it "raises a JSONParseError if there is no response" do
63
63
  RestClient.stub(:get) { nil }
64
- json = request('get')
65
- json.should == {}
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).with('http://localhost/foo', {:bar => 'hi'})
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).with('http://localhost/foo', {})
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).with('http://localhost/foo', {:params => {:x => 'y'}})
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))
@@ -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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
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-31 00:00:00 Z
18
+ date: 2012-07-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: hashie