wrest 2.1.9 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG +6 -0
  3. data/LICENCE +1 -1
  4. data/README.md +47 -40
  5. data/bin/wrest +2 -1
  6. data/bin/wrest_shell.rb +10 -8
  7. data/lib/wrest/async_request/event_machine_backend.rb +3 -1
  8. data/lib/wrest/async_request/thread_backend.rb +5 -2
  9. data/lib/wrest/async_request/thread_pool.rb +4 -2
  10. data/lib/wrest/async_request.rb +7 -6
  11. data/lib/wrest/cache_proxy.rb +39 -28
  12. data/lib/wrest/caching/memcached.rb +21 -18
  13. data/lib/wrest/caching/redis.rb +22 -22
  14. data/lib/wrest/caching.rb +16 -14
  15. data/lib/wrest/callback.rb +19 -16
  16. data/lib/wrest/components/container/alias_accessors.rb +51 -47
  17. data/lib/wrest/components/container/typecaster.rb +146 -95
  18. data/lib/wrest/components/container.rb +171 -152
  19. data/lib/wrest/components/mutators/base.rb +43 -34
  20. data/lib/wrest/components/mutators/camel_to_snake_case.rb +7 -3
  21. data/lib/wrest/components/mutators/{xml_mini_type_caster.rb → xml_type_caster.rb} +29 -16
  22. data/lib/wrest/components/mutators.rb +21 -19
  23. data/lib/wrest/components/translators/content_types.rb +20 -16
  24. data/lib/wrest/components/translators/json.rb +19 -16
  25. data/lib/wrest/components/translators/txt.rb +19 -15
  26. data/lib/wrest/components/translators/xml/conversions.rb +56 -0
  27. data/lib/wrest/components/translators/xml.rb +60 -18
  28. data/lib/wrest/components/translators.rb +7 -6
  29. data/lib/wrest/components.rb +11 -8
  30. data/lib/wrest/core_ext/hash/conversions.rb +10 -10
  31. data/lib/wrest/core_ext/hash.rb +4 -2
  32. data/lib/wrest/core_ext/string/conversions.rb +14 -13
  33. data/lib/wrest/core_ext/string.rb +5 -3
  34. data/lib/wrest/exceptions.rb +4 -2
  35. data/lib/wrest/hash_with_case_insensitive_access.rb +8 -8
  36. data/lib/wrest/hash_with_indifferent_access.rb +442 -0
  37. data/lib/wrest/http_codes.rb +20 -19
  38. data/lib/wrest/http_shared/headers.rb +2 -0
  39. data/lib/wrest/http_shared/standard_headers.rb +2 -2
  40. data/lib/wrest/http_shared/standard_tokens.rb +8 -6
  41. data/lib/wrest/http_shared.rb +5 -3
  42. data/lib/wrest/multipart.rb +20 -11
  43. data/lib/wrest/native/connection_factory.rb +15 -11
  44. data/lib/wrest/native/delete.rb +15 -11
  45. data/lib/wrest/native/get.rb +60 -55
  46. data/lib/wrest/native/options.rb +15 -11
  47. data/lib/wrest/native/patch.rb +27 -0
  48. data/lib/wrest/native/post.rb +15 -11
  49. data/lib/wrest/native/post_multipart.rb +22 -18
  50. data/lib/wrest/native/put.rb +16 -12
  51. data/lib/wrest/native/put_multipart.rb +22 -18
  52. data/lib/wrest/native/redirection.rb +13 -12
  53. data/lib/wrest/native/request.rb +144 -106
  54. data/lib/wrest/native/response.rb +87 -78
  55. data/lib/wrest/native/session.rb +49 -40
  56. data/lib/wrest/native.rb +14 -11
  57. data/lib/wrest/test/request_patches.rb +10 -3
  58. data/lib/wrest/test.rb +3 -1
  59. data/lib/wrest/uri/builders.rb +14 -12
  60. data/lib/wrest/uri.rb +92 -50
  61. data/lib/wrest/uri_template.rb +11 -7
  62. data/lib/wrest/utils.rb +129 -0
  63. data/lib/wrest/version.rb +3 -1
  64. data/lib/wrest.rb +31 -33
  65. data/lib/wrest_no_ext.rb +2 -0
  66. metadata +91 -56
  67. data/lib/wrest/components/mutators/xml_simple_type_caster.rb +0 -37
  68. data/lib/wrest/xml_mini/jdom/xpath_filter.rb +0 -17
  69. data/lib/wrest/xml_mini/jdom.rb +0 -6
  70. data/lib/wrest/xml_mini/libxml/xpath_filter.rb +0 -12
  71. data/lib/wrest/xml_mini/libxml.rb +0 -8
  72. data/lib/wrest/xml_mini/nokogiri/xpath_filter.rb +0 -15
  73. data/lib/wrest/xml_mini/nokogiri.rb +0 -7
  74. data/lib/wrest/xml_mini/rexml/xpath_filter.rb +0 -15
  75. data/lib/wrest/xml_mini/rexml.rb +0 -8
  76. data/lib/wrest/xml_mini.rb +0 -8
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -7,26 +9,27 @@
7
9
  # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
 
10
-
11
12
  module Wrest
12
13
  module Components
13
- module Translators::Json
14
- extend self
14
+ module Translators
15
+ module Json
16
+ module_function
15
17
 
16
- def deserialise(response, options = {})
17
- ActiveSupport::JSON.decode(response.body)
18
- end
18
+ def deserialise(response, _options = {})
19
+ JSON.parse(response.body)
20
+ end
19
21
 
20
- def deserialize(response, options = {})
21
- deserialise(response, options)
22
- end
23
-
24
- def serialise(hash, options = {})
25
- hash.to_json(options)
26
- end
27
-
28
- def serialize(hash, options = {})
29
- serialise(hash, options)
22
+ def deserialize(response, options = {})
23
+ deserialise(response, options)
24
+ end
25
+
26
+ def serialise(hash, options = {})
27
+ hash.to_json(options)
28
+ end
29
+
30
+ def serialize(hash, options = {})
31
+ serialise(hash, options)
32
+ end
30
33
  end
31
34
  end
32
35
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -7,24 +9,26 @@
7
9
  # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
  module Wrest
10
- module Components::Translators
11
- module Txt
12
- extend self
12
+ module Components
13
+ module Translators
14
+ module Txt
15
+ module_function
13
16
 
14
- def deserialise(response,options={})
15
- response.body
16
- end
17
+ def deserialise(response, _options = {})
18
+ response.body
19
+ end
17
20
 
18
- def deserialize(response, options = {})
19
- deserialise(response, options)
20
- end
21
+ def deserialize(response, options = {})
22
+ deserialise(response, options)
23
+ end
21
24
 
22
- def serialise(hash, options = {})
23
- hash.inspect
24
- end
25
-
26
- def serialize(hash, options = {})
27
- serialise(hash, options)
25
+ def serialise(hash, _options = {})
26
+ hash.inspect
27
+ end
28
+
29
+ def serialize(hash, options = {})
30
+ serialise(hash, options)
31
+ end
28
32
  end
29
33
  end
30
34
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wrest
4
+ module Components
5
+ module Translators
6
+ module Xml
7
+ module Conversions # :nodoc:
8
+ module Document # :nodoc:
9
+ def to_hash
10
+ root.to_hash
11
+ end
12
+ end
13
+
14
+ module Node # :nodoc:
15
+ CONTENT_ROOT = '__content__'
16
+
17
+ # Convert XML document to hash.
18
+ #
19
+ # hash::
20
+ # Hash to merge the converted element into.
21
+ def to_hash(hash = {})
22
+ node_hash = {}
23
+
24
+ # Insert node hash into parent hash correctly.
25
+ case hash[name]
26
+ when Array then hash[name] << node_hash
27
+ when Hash then hash[name] = [hash[name], node_hash]
28
+ when nil then hash[name] = node_hash
29
+ end
30
+
31
+ # Handle child elements
32
+ children.each do |c|
33
+ if c.element?
34
+ c.to_hash(node_hash)
35
+ elsif c.text? || c.cdata?
36
+ node_hash[CONTENT_ROOT] ||= +''
37
+ node_hash[CONTENT_ROOT] << c.content
38
+ end
39
+ end
40
+
41
+ # Remove content node if it is blank and there are child tags
42
+ node_hash.delete(CONTENT_ROOT) if node_hash.length > 1 && Utils.object_blank?(node_hash[CONTENT_ROOT])
43
+
44
+ # Handle attributes
45
+ attribute_nodes.each { |a| node_hash[a.node_name] = a.value }
46
+
47
+ hash
48
+ end
49
+ end
50
+ end
51
+ Nokogiri::XML::Document.include(Conversions::Document)
52
+ Nokogiri::XML::Node.include(Conversions::Node)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -6,29 +8,69 @@
6
8
  # Unless required by applicable law or agreed to in writing, software distributed under the License
7
9
  # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
10
  # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ require_relative 'xml/conversions'
13
+
9
14
  module Wrest
10
- module Components::Translators
11
- module Xml
12
- extend self
15
+ module Components
16
+ module Translators
17
+ module Xml
18
+ module_function
13
19
 
14
- def deserialise(response,options={})
15
- if(!options[:xpath].nil?)
16
- ActiveSupport::XmlMini.filter(response.body,options[:xpath])
17
- else
18
- Hash.from_xml(response.body)
20
+ def deserialise(response, options = {})
21
+ data = response.body
22
+ data = StringIO.new(data || '') unless data.respond_to?(:read)
23
+ return {} if data.eof?
24
+
25
+ if options[:xpath].nil?
26
+ parse(data)
27
+ else
28
+ search(data, options[:xpath])
29
+ end
19
30
  end
20
- end
21
31
 
22
- def deserialize(response, options = {})
23
- deserialise(response, options)
24
- end
32
+ def deserialize(response, options = {})
33
+ deserialise(response, options)
34
+ end
25
35
 
26
- def serialise(hash, options = {})
27
- hash.to_xml(options)
28
- end
29
-
30
- def serialize(hash, options = {})
31
- serialise(hash, options)
36
+ def serialise(hash, _options = {})
37
+ to_xml(hash)
38
+ end
39
+
40
+ def serialize(hash, options = {})
41
+ serialise(hash, options)
42
+ end
43
+
44
+ def to_xml(hash, builder = nil)
45
+ builder ||= Nokogiri::XML::Builder.new
46
+ hash.each_with_object(builder) do |(key, value), inner_builder|
47
+ if value.is_a?(Hash)
48
+ inner_builder.send(key.to_s) do |child_builder|
49
+ to_xml(value, child_builder)
50
+ end
51
+ else
52
+ inner_builder.send(key.to_s, value.to_s)
53
+ end
54
+ end.to_xml
55
+ end
56
+
57
+ # Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
58
+ # data::
59
+ # XML Document string or IO to parse
60
+ def parse(data)
61
+ build_nokogiri_doc(data).to_hash
62
+ end
63
+
64
+ def search(data, xpath)
65
+ build_nokogiri_doc(data).xpath(xpath)
66
+ end
67
+
68
+ def build_nokogiri_doc(data)
69
+ doc = Nokogiri::XML(data)
70
+ raise doc.errors.join("\n") if doc.errors.length.positive?
71
+
72
+ doc
73
+ end
32
74
  end
33
75
  end
34
76
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -7,7 +9,6 @@
7
9
  # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
 
10
-
11
12
  module Wrest
12
13
  module Components
13
14
  # Contains strategies/lambdas which know how to deserialise
@@ -17,13 +18,13 @@ module Wrest
17
18
  # the content type
18
19
  def self.lookup(content_type)
19
20
  translator = CONTENT_TYPES[content_type]
20
- translator || (raise Wrest::Exceptions::UnsupportedContentType.new("Unsupported content type #{content_type}"))
21
+ translator || (raise Wrest::Exceptions::UnsupportedContentType, "Unsupported content type #{content_type}")
21
22
  end
22
23
  end
23
24
  end
24
25
  end
25
26
 
26
- require "wrest/components/translators/txt"
27
- require "wrest/components/translators/xml"
28
- require "wrest/components/translators/json"
29
- require "wrest/components/translators/content_types"
27
+ require 'wrest/components/translators/txt'
28
+ require 'wrest/components/translators/xml'
29
+ require 'wrest/components/translators/json'
30
+ require 'wrest/components/translators/content_types'
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
9
11
 
10
12
  module Wrest
11
13
  # A component is a building block that can
@@ -15,5 +17,6 @@ module Wrest
15
17
  end
16
18
  end
17
19
 
18
- require "wrest/components/container"
19
- require "wrest/components/translators"
20
+ require 'wrest/components/container'
21
+ require 'wrest/components/mutators'
22
+ require 'wrest/components/translators'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -8,37 +10,35 @@
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
 
10
12
  module Wrest
11
- module CoreExt #:nodoc:
12
- module Hash #:nodoc:
13
+ module CoreExt # :nodoc:
14
+ module Hash # :nodoc:
13
15
  # Makes it easier to build other objects from a Hash
14
16
  module Conversions
15
-
16
17
  # This method accepts a hash mutator (found in Wrest::Compononents)
17
18
  # to build a new hash map by making changes to an existing one.
18
19
  #
19
- # No, this method does not mutate the state of the hash it is invoked on,
20
+ # No, this method does not mutate the state of the hash it is invoked on,
20
21
  # but rather builds a new one.
21
22
  #
22
- # Yes, the name is misleading in that respect. However, one
23
+ # Yes, the name is misleading in that respect. However, one
23
24
  # hopes the absence of an exclamation mark will increase clarity.
24
25
  #
25
26
  # Uses include mutating the hash produced by deserialising xml
26
27
  # by using the meta data in the hash to type cast values.
27
- #
28
+ #
28
29
  # Example:
29
30
  # "http://search.yahooapis.com/NewsSearchService/V1/newsSearch".to_uri.get(
30
- # :appid => 'YahooDemo',
31
+ # :appid => 'YahooDemo',
31
32
  # :output => 'xml',
32
33
  # :query => 'India',
33
34
  # :results=> '3',
34
35
  # :start => '1'
35
- # ).deserialise.mutate_using(XmlSimpleTypeCaster.new)
36
+ # ).deserialise.mutate_using(XmlTypeCaster.new)
36
37
  def mutate_using(mutator)
37
38
  mutated_hash = {}
38
- self.each{|tuple| mutated_hash.store(*mutator.mutate(tuple))}
39
+ each { |tuple| mutated_hash.store(*mutator.mutate(tuple)) }
39
40
  mutated_hash
40
41
  end
41
-
42
42
  end
43
43
  end
44
44
  end
@@ -1,5 +1,7 @@
1
- require "wrest/core_ext/hash/conversions"
1
+ # frozen_string_literal: true
2
2
 
3
- class Hash #:nodoc:
3
+ require 'wrest/core_ext/hash/conversions'
4
+
5
+ class Hash # :nodoc:
4
6
  include Wrest::CoreExt::Hash::Conversions
5
7
  end
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
 
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
9
11
 
10
12
  module Wrest
11
- module CoreExt #:nodoc:
12
- module String #:nodoc:
13
+ module CoreExt # :nodoc:
14
+ module String # :nodoc:
13
15
  # Makes it easier to build other objects from a String
14
16
  # This module is opt-out - if you don't want the to_uri
15
- # and to_uri_template convenience method on String, set
16
- # the NoStringExtensions constant on the Wrest module
17
+ # and to_uri_template convenience method on String, set
18
+ # the NoStringExtensions constant on the Wrest module
17
19
  # before requiring wrest.
18
20
  #
19
21
  # module Wrest
@@ -21,15 +23,14 @@ module Wrest
21
23
  # end
22
24
  # require 'wrest'
23
25
  module Conversions
24
-
25
26
  # A convenience method equivalent to Wrest::Uri.new(string)
26
27
  def to_uri(options = {})
27
- Wrest::Uri.new(self.strip, options)
28
+ Wrest::Uri.new(strip, options)
28
29
  end
29
30
 
30
31
  # A convenience method equivalent to Wrest:UriTemplate.new(uri_pattern)
31
32
  def to_uri_template(options = {})
32
- Wrest::UriTemplate.new(self.strip, options)
33
+ Wrest::UriTemplate.new(strip, options)
33
34
  end
34
35
  end
35
36
  end
@@ -1,5 +1,7 @@
1
- require "wrest/core_ext/string/conversions"
1
+ # frozen_string_literal: true
2
2
 
3
- class String #:nodoc:
3
+ require 'wrest/core_ext/string/conversions'
4
+
5
+ class String # :nodoc:
4
6
  include Wrest::CoreExt::String::Conversions unless Wrest.const_defined?('NoStringExtensions')
5
- end
7
+ end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Wrest
2
- module Exceptions #:nodoc:
4
+ module Exceptions # :nodoc:
3
5
  # Raised when a base method that should be overriden
4
6
  # is invoked on a subclass that has not implemented it.
5
7
  class MethodNotOverridden < StandardError
@@ -26,7 +28,7 @@ module Wrest
26
28
  # Raised when a request times out
27
29
  class Timeout < StandardError
28
30
  end
29
-
31
+
30
32
  class UnsupportedHttpVerb < StandardError
31
33
  end
32
34
 
@@ -1,5 +1,6 @@
1
- module Wrest
1
+ # frozen_string_literal: true
2
2
 
3
+ module Wrest
3
4
  # A hash with case-insensitive key access.
4
5
  #
5
6
  # hash = Wrest::HashWithCaseInsensitiveAccess.new 'Abcd' => 1, 'xyz' => 2
@@ -7,14 +8,14 @@ module Wrest
7
8
  # hash['abcd'] #=> 1
8
9
  # hash['aBCd'] #=> 1
9
10
  #
10
- class HashWithCaseInsensitiveAccess < ::Hash #:nodoc:
11
-
12
- def initialize(hash={})
11
+ class HashWithCaseInsensitiveAccess < ::Hash # :nodoc:
12
+ def initialize(hash = {})
13
13
  super()
14
14
  hash.each do |key, value|
15
15
  self[convert_key(key)] = value
16
16
  end
17
17
  end
18
+
18
19
  def [](key)
19
20
  super(convert_key(key))
20
21
  end
@@ -44,9 +45,8 @@ module Wrest
44
45
 
45
46
  protected
46
47
 
47
- def convert_key(key)
48
- key.is_a?(String) ? key.downcase : key
49
- end
50
-
48
+ def convert_key(key)
49
+ key.is_a?(String) ? key.downcase : key
50
+ end
51
51
  end
52
52
  end