active_rest_client 1.1.0 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93a842b5f48d12dcd6e53fe6ffef4ff8bed6ba35
4
- data.tar.gz: 2fe79284a9107ababa2758575b61906b2814b9d1
3
+ metadata.gz: cd42ad7d428093e385b492a421a17154204040a3
4
+ data.tar.gz: e5b6ee744601dac89141d2bf306d8414cb590060
5
5
  SHA512:
6
- metadata.gz: f45afa7dde42bc05d0b22cb913ce121b99dd77d20ce2f67e2b7990f3ea0b686cb8c533c4e91a03eb0eb043a00c0bb0953f5e8390e77b945bff319f321136a941
7
- data.tar.gz: 12307bd1cb569a0dba41172e052a60667ef27b7c81aacf7f1b2db9df30806d26ab75b717fc9aca10ea4eb740c01d96081dd852ec98d3b539edf29c1ceda75609
6
+ metadata.gz: 390b20696614bd583b7f981011ccce7c2ea979c6a4bb963d6d42e6d25ca2e15bfe41e8892120c46de35cad7a2a9e99fd6aee79ffd5c45793bd9ae379c18b818c
7
+ data.tar.gz: 82ef457e99179083edd05b2043efdf297f1bbe9fcdef6f19aae3a4c0407058aa03d14f5982da5462a9ec6813e3880b7177d8c9be51066f65d98a04e487072e9d
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency 'typhoeus'
34
34
 
35
35
  spec.add_runtime_dependency "multi_json"
36
+ spec.add_runtime_dependency "crack"
36
37
  spec.add_runtime_dependency "activesupport"
37
38
  spec.add_runtime_dependency "faraday"
38
39
  spec.add_runtime_dependency "patron", ">= 0.4.9" # 0.4.18 breaks against Curl v0.7.15 but works with webmock
@@ -84,7 +84,7 @@ module ActiveRestClient
84
84
  method = MultiJson.load(MultiJson.dump(@request.method),:symbolize_keys => true)
85
85
  method[:method] = :get
86
86
  method[:options][:url] = @url
87
- method[:options][:overriden_name] = @options[:overriden_name]
87
+ method[:options][:overridden_name] = @options[:overridden_name]
88
88
  request = ActiveRestClient::Request.new(method, @request.object)
89
89
  request.url = request.forced_url = @url
90
90
  @object = request.call
@@ -1,5 +1,7 @@
1
1
  require "cgi"
2
2
  require "multi_json"
3
+ require 'crack'
4
+ require 'crack/xml'
3
5
 
4
6
  module ActiveRestClient
5
7
 
@@ -11,7 +13,7 @@ module ActiveRestClient
11
13
  @method[:options] ||= {}
12
14
  @method[:options][:lazy] ||= []
13
15
  @method[:options][:has_one] ||= {}
14
- @overriden_name = @method[:options][:overriden_name]
16
+ @overridden_name = @method[:options][:overridden_name]
15
17
  @object = object
16
18
  @response_delegate = ActiveRestClient::RequestDelegator.new(nil)
17
19
  @params = params
@@ -116,7 +118,8 @@ module ActiveRestClient
116
118
  fake = fake.call(self)
117
119
  end
118
120
  ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Faked response found"
119
- return handle_response(OpenStruct.new(status:200, body:fake, response_headers:{"X-ARC-Faked-Response" => "true"}))
121
+ content_type = @method[:options][:fake_content_type] || "application/json"
122
+ return handle_response(OpenStruct.new(status:200, body:fake, response_headers:{"X-ARC-Faked-Response" => "true", "Content-Type" => content_type}))
120
123
  end
121
124
  if object_is_class?
122
125
  @object.send(:_filter_request, :before, @method[:name], self)
@@ -322,7 +325,7 @@ module ActiveRestClient
322
325
  if (200..399).include?(status)
323
326
  if @method[:options][:plain]
324
327
  return @response = response.body
325
- elsif is_json_response?
328
+ elsif is_json_response? || is_xml_response?
326
329
  if @response.respond_to?(:proxied) && @response.proxied
327
330
  ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{@instrumentation_name} - Response was proxied, unable to determine size"
328
331
  else
@@ -333,7 +336,7 @@ module ActiveRestClient
333
336
  raise ResponseParseException.new(status:status, body:@response.body)
334
337
  end
335
338
  else
336
- if is_json_response?
339
+ if is_json_response? || is_xml_response?
337
340
  error_response = generate_new_object(mutable: false)
338
341
  else
339
342
  error_response = @response.body
@@ -362,10 +365,10 @@ module ActiveRestClient
362
365
  @method[:options][:has_many] ||= {}
363
366
  name = name.to_sym rescue nil
364
367
  if @method[:options][:has_many][name]
365
- overriden_name = name
368
+ overridden_name = name
366
369
  object = @method[:options][:has_many][name].new
367
370
  elsif @method[:options][:has_one][name]
368
- overriden_name = name
371
+ overridden_name = name
369
372
  object = @method[:options][:has_one][name].new
370
373
  else
371
374
  object = create_object_instance
@@ -377,16 +380,16 @@ module ActiveRestClient
377
380
 
378
381
  attributes.each do |k,v|
379
382
  k = k.to_sym
380
- overriden_name = select_name(k, overriden_name)
383
+ overridden_name = select_name(k, overridden_name)
381
384
  if @method[:options][:lazy].include?(k)
382
- object._attributes[k] = ActiveRestClient::LazyAssociationLoader.new(overriden_name, v, self, overriden_name:(overriden_name))
385
+ object._attributes[k] = ActiveRestClient::LazyAssociationLoader.new(overridden_name, v, self, overridden_name:(overridden_name))
383
386
  elsif v.is_a? Hash
384
- object._attributes[k] = new_object(v, overriden_name )
387
+ object._attributes[k] = new_object(v, overridden_name )
385
388
  elsif v.is_a? Array
386
389
  object._attributes[k] = ActiveRestClient::ResultIterator.new
387
390
  v.each do |item|
388
391
  if item.is_a? Hash
389
- object._attributes[k] << new_object(item, overriden_name)
392
+ object._attributes[k] << new_object(item, overridden_name)
390
393
  else
391
394
  object._attributes[k] << item
392
395
  end
@@ -469,11 +472,17 @@ module ActiveRestClient
469
472
  @response.response_headers['Content-Type'].nil? || @response.response_headers['Content-Type'].include?('json')
470
473
  end
471
474
 
475
+ def is_xml_response?
476
+ @response.response_headers['Content-Type'].include?('xml')
477
+ end
478
+
472
479
  def generate_new_object(options={})
473
480
  if @response.body.is_a?(Array) || @response.body.is_a?(Hash)
474
481
  body = @response.body
475
- else
482
+ elsif is_json_response?
476
483
  body = @response.body.blank? ? {} : MultiJson.load(@response.body)
484
+ elsif is_xml_response?
485
+ body = @response.body.blank? ? {} : Crack::XML.parse(@response.body)
477
486
  end
478
487
  body = begin
479
488
  @method[:name].nil? ? body : translator.send(@method[:name], body)
@@ -483,10 +492,10 @@ module ActiveRestClient
483
492
  if body.is_a? Array
484
493
  result = ActiveRestClient::ResultIterator.new(@response)
485
494
  body.each do |json_object|
486
- result << new_object(json_object, @overriden_name)
495
+ result << new_object(json_object, @overridden_name)
487
496
  end
488
497
  else
489
- result = new_object(body, @overriden_name)
498
+ result = new_object(body, @overridden_name)
490
499
  result._status = @response.status
491
500
  result._headers = @response.response_headers
492
501
  result._etag = @response.response_headers['ETag']
@@ -1,3 +1,3 @@
1
1
  module ActiveRestClient
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.9"
3
3
  end
@@ -166,7 +166,7 @@ describe ActiveRestClient::Caching do
166
166
  cached_response = ActiveRestClient::CachedResponse.new(
167
167
  status:200,
168
168
  result:object,
169
- etag:@etag,
169
+ etag:etag,
170
170
  expires:Time.now + 30)
171
171
  expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(Marshal.dump(cached_response))
172
172
  expect_any_instance_of(ActiveRestClient::Connection).not_to receive(:get)
@@ -179,7 +179,7 @@ describe ActiveRestClient::Caching do
179
179
  expect_any_instance_of(CachingExampleCacheStore5).to receive(:read).once.with("Person:/").and_return(nil)
180
180
  expect_any_instance_of(CachingExampleCacheStore5).not_to receive(:write)
181
181
  expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(status:200, body:"{\"result\":true}", headers:{}))
182
- ret = Person.all
182
+ Person.all
183
183
  end
184
184
 
185
185
  it "should write the response to the cache if there's an etag" do
@@ -187,7 +187,7 @@ describe ActiveRestClient::Caching do
187
187
  expect_any_instance_of(CachingExampleCacheStore5).to receive(:write).once.with("Person:/", an_instance_of(String), {})
188
188
  expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, body:"{\"result\":true}", response_headers:{etag:"1234567890"})))
189
189
  Person.perform_caching true
190
- ret = Person.all
190
+ Person.all
191
191
  end
192
192
 
193
193
  it "should write the response to the cache if there's a hard expiry" do
@@ -195,7 +195,7 @@ describe ActiveRestClient::Caching do
195
195
  expect_any_instance_of(CachingExampleCacheStore5).to receive(:write).once.with("Person:/", an_instance_of(String), an_instance_of(Hash))
196
196
  expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, body:"{\"result\":true}", response_headers:{expires:(Time.now + 30).rfc822})))
197
197
  Person.perform_caching = true
198
- ret = Person.all
198
+ Person.all
199
199
  end
200
200
 
201
201
  it "should not write the response to the cache if there's an invalid expiry" do
@@ -203,7 +203,7 @@ describe ActiveRestClient::Caching do
203
203
  expect_any_instance_of(CachingExampleCacheStore5).to_not receive(:write).once.with("Person:/", an_instance_of(String), an_instance_of(Hash))
204
204
  expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(status:200, body:"{\"result\":true}", headers:{expires:"0"}))
205
205
  Person.perform_caching = true
206
- ret = Person.all
206
+ Person.all
207
207
  end
208
208
 
209
209
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ class XmlResponseExample < ActiveRestClient::Base
4
+ base_url "http://www.example.com/v1/"
5
+ get :atom, "/atom", fake: %Q{
6
+ <?xml version="1.0" encoding="utf-8"?>
7
+ <feed xmlns="http://www.w3.org/2005/Atom">
8
+
9
+ <title>Example Feed</title>
10
+ <link href="http://example.org/"/>
11
+ <updated>2003-12-13T18:30:02Z</updated>
12
+ <author>
13
+ <name>John Doe</name>
14
+ </author>
15
+ <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
16
+
17
+ <entry>
18
+ <title>Atom-Powered Robots Run Amok</title>
19
+ <link href="http://example.org/2003/12/13/atom03"/>
20
+ <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
21
+ <updated>2003-12-13T18:30:02Z</updated>
22
+ <summary>Some text.</summary>
23
+ </entry>
24
+
25
+ <entry>
26
+ <title>Something else cool happened</title>
27
+ <link href="http://example.org/2015/08/11/andyjeffries"/>
28
+ <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6b</id>
29
+ <updated>2015-08-11T18:30:02Z</updated>
30
+ <summary>Some other text.</summary>
31
+ </entry>
32
+
33
+ </feed>}.strip_heredoc, fake_content_type: "application/xml"
34
+ end
35
+
36
+ describe XmlResponseExample do
37
+ it "should parse the response without error" do
38
+ expect {
39
+ XmlResponseExample.atom
40
+ }.to_not raise_error
41
+ end
42
+
43
+ it "provides the feed title" do
44
+ @atom = XmlResponseExample.atom
45
+ expect(@atom.feed.title).to eq("Example Feed")
46
+ end
47
+
48
+ it "each entry item has a title" do
49
+ @atom = XmlResponseExample.atom
50
+ expect(@atom.feed.entry.class).to eq(ActiveRestClient::ResultIterator)
51
+ end
52
+
53
+ it "provides a list of entry items" do
54
+ @atom = XmlResponseExample.atom
55
+ expect(@atom.feed.entry[0].title).to eq("Atom-Powered Robots Run Amok")
56
+ expect(@atom.feed.entry[1].title).to eq("Something else cool happened")
57
+ end
58
+
59
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_rest_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Which Ltd
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-26 00:00:00.000000000 Z
12
+ date: 2015-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -193,6 +193,20 @@ dependencies:
193
193
  - - ">="
194
194
  - !ruby/object:Gem::Version
195
195
  version: '0'
196
+ - !ruby/object:Gem::Dependency
197
+ name: crack
198
+ requirement: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ type: :runtime
204
+ prerelease: false
205
+ version_requirements: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
196
210
  - !ruby/object:Gem::Dependency
197
211
  name: activesupport
198
212
  requirement: !ruby/object:Gem::Requirement
@@ -293,6 +307,7 @@ files:
293
307
  - spec/lib/request_spec.rb
294
308
  - spec/lib/result_iterator_spec.rb
295
309
  - spec/lib/validation_spec.rb
310
+ - spec/lib/xml_spec.rb
296
311
  - spec/spec_helper.rb
297
312
  homepage: http://whichdigital.github.io/
298
313
  licenses:
@@ -339,5 +354,6 @@ test_files:
339
354
  - spec/lib/request_spec.rb
340
355
  - spec/lib/result_iterator_spec.rb
341
356
  - spec/lib/validation_spec.rb
357
+ - spec/lib/xml_spec.rb
342
358
  - spec/spec_helper.rb
343
359
  has_rdoc: