Skip to content

Commit 2796435

Browse files
authored
Merge pull request #143 from baron-hines/RUBYAPI-38
RUBYAPI-38: new response attributes and request option
2 parents 2f05060 + 2fbcb6c commit 2796435

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v1.4.19
2+
- create SparkQLErrors attribute for SparkApi::Response and populate when present
3+
- create Errors attribute for SparkApi::Response and populate when present
4+
- create SparkApi::Request option `full_response` that allows methods to return object instead of body only.
5+
16
v1.4.18
27
- include the errors from the response in the instance of ClientErrors
38

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.18
1+
1.4.19

lib/spark_api/request.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ def request(method, path, body, options)
9292
SparkApi.logger.error { "Authentication failed or server is sending us expired tokens, nothing we can do here." }
9393
raise
9494
end
95-
response.body
95+
96+
if options[:full_response]
97+
return response
98+
else
99+
return response.body
100+
end
96101
rescue Faraday::Error::ConnectionFailed => e
97102
if self.ssl_verify && e.message =~ /certificate verify failed/
98103
SparkApi.logger.error { SparkApi::Errors.ssl_verification_error }

lib/spark_api/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SparkApi
22
# API Response interface
33
module Response
4-
ATTRIBUTES = [:code, :message, :results, :success, :pagination, :details, :d]
4+
ATTRIBUTES = [:code, :message, :results, :success, :pagination, :details, :d, :errors, :sparkql_errors]
55
attr_accessor *ATTRIBUTES
66
def success?
77
@success
@@ -23,6 +23,8 @@ def initialize(d)
2323
self.success = self.d["Success"]
2424
self.pagination = self.d["Pagination"]
2525
self.details = self.d["Details"] || []
26+
self.errors = self.d["Errors"]
27+
self.sparkql_errors = self.d['SparkQLErrors']
2628
super(results)
2729
rescue Exception => e
2830
SparkApi.logger.error "Unable to understand the response! #{d}"

spec/unit/spark_api/request_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757
r.success?.should be(false)
5858
r.message.should be == "I am a failure."
5959
end
60+
it "should have SparkQLErrors when present" do
61+
err = {"Token" => "ExpirationDate", "Status" => "Dropped"}
62+
r = SparkApi::ApiResponse.new({"D"=>{"Success" => false, "Message" => "I am a failure from .",
63+
"SparkQLErrors" => [err]
64+
}})
65+
r.sparkql_errors.first.should eq(err)
66+
end
67+
it "should have Errors when present" do
68+
err = {"Type" => "InvalidAttribute", "Attribute" => "DisplayName", "Message" => "DisplayName is wrong."}
69+
r = SparkApi::ApiResponse.new({"D"=>{"Success" => false, "Message" => "I am a failure from .",
70+
"Errors" => [err]
71+
}})
72+
r.errors.first.should eq(err)
73+
end
6074
end
6175

6276
describe SparkApi::Request do
@@ -197,6 +211,13 @@ def version()
197211
subject.post('/stringdata', 'I am a lonely String!').success?.should == true
198212
end
199213

214+
it "should allow response object to be returned instead of body" do
215+
r = subject.get('/system', { full_response: true })
216+
217+
r.is_a?(Faraday::Response).should be(true)
218+
r.status.should eq(200)
219+
end
220+
200221
it "should give me BigDecimal results for large floating point numbers" do
201222
MultiJson.default_adapter.should eq(:yajl) unless jruby?
202223
result = subject.get('/listings/1000')[0]

0 commit comments

Comments
 (0)