wrest 3.0.0 → 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 +3 -0
  3. data/LICENCE +1 -1
  4. data/README.md +29 -28
  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 -96
  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 -56
  46. data/lib/wrest/native/options.rb +15 -11
  47. data/lib/wrest/native/patch.rb +16 -12
  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 -108
  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 -12
  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 +70 -52
  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 +98 -48
  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,18 +9,20 @@
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
- module Wrest::Native
11
- module ConnectionFactory
12
- def create_connection(options = {:timeout => 60, :verify_mode => OpenSSL::SSL::VERIFY_NONE})
13
- options[:timeout] ||= 60
14
- connection = Net::HTTP.new(self.host, self.port)
15
- connection.read_timeout = options[:timeout]
16
- if self.https?
17
- connection.use_ssl = true
18
- connection.verify_mode = options[:verify_mode] ? options[:verify_mode] : OpenSSL::SSL::VERIFY_PEER
19
- connection.ca_path = options[:ca_path] if options[:ca_path]
12
+ module Wrest
13
+ module Native
14
+ module ConnectionFactory
15
+ def create_connection(options = { timeout: 60, verify_mode: OpenSSL::SSL::VERIFY_NONE })
16
+ options[:timeout] ||= 60
17
+ connection = Net::HTTP.new(host, port)
18
+ connection.read_timeout = options[:timeout]
19
+ if https?
20
+ connection.use_ssl = true
21
+ connection.verify_mode = options[:verify_mode] || OpenSSL::SSL::VERIFY_PEER
22
+ connection.ca_path = options[:ca_path] if options[:ca_path]
23
+ end
24
+ connection
20
25
  end
21
- connection
22
26
  end
23
27
  end
24
28
  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,17 +9,19 @@
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
- module Wrest::Native
11
- class Delete < Request
12
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
- super(
14
- wrest_uri,
15
- Net::HTTP::Delete,
16
- parameters,
17
- nil,
18
- headers,
19
- options
20
- )
12
+ module Wrest
13
+ module Native
14
+ class Delete < Request
15
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Delete,
19
+ parameters,
20
+ nil,
21
+ headers,
22
+ options
23
+ )
24
+ end
21
25
  end
22
26
  end
23
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 Sidu Ponnappa
2
4
  # Licensed under the Apache License, Version 2.0 (the "License");
3
5
  # you may not use this file except in compliance with the License.
@@ -6,74 +8,76 @@
6
8
  # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7
9
  # See the License for the specific language governing permissions and limitations under the License.
8
10
 
9
- module Wrest::Native
10
- class Get < Request
11
- QUERY_PARAMS_SEPERATOR = '?'
12
- EMPTY_QUERY_PARAMS = ''
11
+ module Wrest
12
+ module Native
13
+ class Get < Request
14
+ QUERY_PARAMS_SEPERATOR = '?'
15
+ EMPTY_QUERY_PARAMS = ''
13
16
 
14
- attr_reader :cache_proxy
17
+ attr_reader :cache_proxy
15
18
 
16
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
17
- follow_redirects = options[:follow_redirects]
18
- options[:follow_redirects] = (follow_redirects == nil ? true : follow_redirects)
19
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
20
+ follow_redirects = options[:follow_redirects]
21
+ options[:follow_redirects] = (follow_redirects.nil? ? true : follow_redirects)
19
22
 
20
- cache_store = (options[:cache_store] || Wrest::Caching.default_store) unless options[:disable_cache]
21
- @cache_proxy = Wrest::CacheProxy::new(self, cache_store)
23
+ cache_store = (options[:cache_store] || Wrest::Caching.default_store) unless options[:disable_cache]
24
+ @cache_proxy = Wrest::CacheProxy.new(self, cache_store)
22
25
 
23
- super(
24
- wrest_uri,
25
- Net::HTTP::Get,
26
- parameters,
27
- nil,
28
- headers,
29
- options
30
- )
31
- end
26
+ super(wrest_uri, Net::HTTP::Get, parameters, nil, headers, options)
27
+ end
32
28
 
33
- # Checks equality between two Wrest::Native::Get objects.
34
- # Comparing two Wrest::Native::Get objects with identical values for the following properties would return True.
35
- # uri, parameters, username, password and ssh verify_mode.
36
- def ==(other)
37
- return true if self.equal?(other)
38
- return false unless other.class == self.class
39
- return true if self.uri == other.uri and
40
- self.parameters == other.parameters and
41
- self.username == other.username and
42
- self.password == other.password and
43
- self.verify_mode == other.verify_mode
44
- false
45
- end
29
+ # Checks equality between two Wrest::Native::Get objects.
30
+ # Comparing two Wrest::Native::Get objects with identical values for the following properties would return True.
31
+ # uri, parameters, username, password and ssh verify_mode.
32
+ def ==(other)
33
+ return true if equal?(other)
34
+ return false unless other.class == self.class
35
+ return true if these_fields_are_equal(other)
46
36
 
47
- # Returns a hash value for this Wrest::Native::Get object.
48
- # Objects that returns true when compared using the == operator would return the same hash value also.
49
- def hash
50
- self.uri.hash + self.parameters.hash + self.username.hash + self.password.hash + self.verify_mode.hash + 20110106
51
- end
37
+ false
38
+ end
52
39
 
53
- #:nodoc:
54
- def invoke_with_cache_check
55
- cache_proxy.get
56
- end
40
+ # Returns a hash value for this Wrest::Native::Get object.
41
+ # Objects that returns true when compared using the == operator would return the same hash value also.
42
+ def hash
43
+ [uri, parameters, username, password, verify_mode].hash
44
+ end
57
45
 
58
- alias_method :invoke_without_cache_check, :invoke
59
- alias_method :invoke, :invoke_with_cache_check
46
+ # :nodoc:
47
+ def invoke_with_cache_check
48
+ cache_proxy.get
49
+ end
60
50
 
61
- def build_request_without_cache_store(cache_validation_headers)
62
- new_headers = headers.clone.merge cache_validation_headers
63
- new_options = options.clone.tap { |opts| opts.delete :cache_store; opts[:disable_cache] = true } # do not run this through the caching mechanism.
51
+ alias invoke_without_cache_check invoke
52
+ alias invoke invoke_with_cache_check
64
53
 
65
- new_request = Wrest::Native::Get.new(uri, parameters, new_headers, new_options)
66
- new_request
67
- end
54
+ def build_request_without_cache_store(cache_validation_headers)
55
+ new_headers = headers.clone.merge cache_validation_headers
56
+ new_options = # do not run this through the caching mechanism.
57
+ options.clone.tap do |opts|
58
+ opts.delete :cache_store
59
+ opts[:disable_cache] = true
60
+ end
61
+ Wrest::Native::Get.new(uri, parameters, new_headers, new_options)
62
+ end
68
63
 
69
- def full_uri_string
70
- @uri.to_s + query_params_string
71
- end
64
+ def full_uri_string
65
+ @uri.to_s + query_params_string
66
+ end
72
67
 
73
- private
74
- def query_params_string
75
- (@parameters.any?) ? QUERY_PARAMS_SEPERATOR + @parameters.to_query : EMPTY_QUERY_PARAMS
76
- end
68
+ private
77
69
 
70
+ def these_fields_are_equal(other)
71
+ (uri == other.uri) &&
72
+ (parameters == other.parameters) &&
73
+ (username == other.username) &&
74
+ (password == other.password) &&
75
+ (verify_mode == other.verify_mode)
76
+ end
77
+
78
+ def query_params_string
79
+ @parameters.any? ? QUERY_PARAMS_SEPERATOR + Utils.hash_to_param(@parameters) : EMPTY_QUERY_PARAMS
80
+ end
81
+ end
78
82
  end
79
83
  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,17 +9,19 @@
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
- module Wrest::Native
11
- class Options < Request
12
- def initialize(wrest_uri, options = {})
13
- super(
14
- wrest_uri,
15
- Net::HTTP::Options,
16
- {},
17
- nil,
18
- {},
19
- options
20
- )
12
+ module Wrest
13
+ module Native
14
+ class Options < Request
15
+ def initialize(wrest_uri, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Options,
19
+ {},
20
+ nil,
21
+ {},
22
+ options
23
+ )
24
+ end
21
25
  end
22
26
  end
23
27
  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,17 +9,19 @@
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
- module Wrest::Native
11
- class Patch < Request
12
- def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
- super(
14
- wrest_uri,
15
- Net::HTTP::Patch,
16
- parameters,
17
- body,
18
- headers,
19
- options
20
- )
12
+ module Wrest
13
+ module Native
14
+ class Patch < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Patch,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
21
25
  end
22
26
  end
23
- end
27
+ 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,17 +9,19 @@
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
- module Wrest::Native
11
- class Post < Request
12
- def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
- super(
14
- wrest_uri,
15
- Net::HTTP::Post,
16
- parameters,
17
- body,
18
- headers,
19
- options
20
- )
12
+ module Wrest
13
+ module Native
14
+ class Post < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Post,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
21
25
  end
22
26
  end
23
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 - 2010 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -8,25 +10,27 @@
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
  require 'net/http/post/multipart'
10
12
 
11
- module Wrest::Native
12
- class PostMultipart < Request
13
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
14
- super(
15
- wrest_uri,
16
- Net::HTTP::Post::Multipart,
17
- parameters,
18
- nil,
19
- headers,
20
- options
21
- )
22
- end
23
-
24
- def build_request(request_klass, uri, parameters, headers)
25
- request_klass.new(uri.full_path, parameters, headers)
26
- end
13
+ module Wrest
14
+ module Native
15
+ class PostMultipart < Request
16
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
17
+ super(
18
+ wrest_uri,
19
+ Net::HTTP::Post::Multipart,
20
+ parameters,
21
+ nil,
22
+ headers,
23
+ options
24
+ )
25
+ end
26
+
27
+ def build_request(request_klass, uri, parameters, headers)
28
+ request_klass.new(uri.full_path, parameters, headers)
29
+ end
27
30
 
28
- def do_request
29
- @connection.request(@http_request)
31
+ def do_request
32
+ @connection.request(@http_request)
33
+ end
30
34
  end
31
35
  end
32
36
  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,17 +9,19 @@
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
- module Wrest::Native
11
- class Put < Request
12
- def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
- super(
14
- wrest_uri,
15
- Net::HTTP::Put,
16
- parameters,
17
- body,
18
- headers,
19
- options
20
- )
12
+ module Wrest
13
+ module Native
14
+ class Put < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Put,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
21
25
  end
22
26
  end
23
- end
27
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2009 - 2010 Sidu Ponnappa
2
4
 
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -8,25 +10,27 @@
8
10
  # See the License for the specific language governing permissions and limitations under the License.
9
11
  require 'net/http/post/multipart'
10
12
 
11
- module Wrest::Native
12
- class PutMultipart < Request
13
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
14
- super(
15
- wrest_uri,
16
- Net::HTTP::Put::Multipart,
17
- parameters,
18
- nil,
19
- headers,
20
- options
21
- )
22
- end
23
-
24
- def build_request(request_klass, uri, parameters, headers)
25
- request_klass.new(uri.full_path, parameters, headers)
26
- end
13
+ module Wrest
14
+ module Native
15
+ class PutMultipart < Request
16
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
17
+ super(
18
+ wrest_uri,
19
+ Net::HTTP::Put::Multipart,
20
+ parameters,
21
+ nil,
22
+ headers,
23
+ options
24
+ )
25
+ end
26
+
27
+ def build_request(request_klass, uri, parameters, headers)
28
+ request_klass.new(uri.full_path, parameters, headers)
29
+ end
27
30
 
28
- def do_request
29
- @connection.request(@http_request)
31
+ def do_request
32
+ @connection.request(@http_request)
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -1,25 +1,26 @@
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
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
5
7
  # 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.
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
- module Wrest #:nodoc:
11
- module Native #:nodoc:
12
- # Constructed by Wrest::Response.new if the HTTP response code is 3xx
12
+ module Wrest # :nodoc:
13
+ module Native # :nodoc:
14
+ # Constructed by Wrest::Response.new if the HTTP response code is 3xx
13
15
  # (http://en.wikipedia.org/wiki/300_Multiple_Choices#3xx_Redirection)
14
- #
16
+ #
15
17
  # This class is necessary because Net::HTTP doesn't seem to support
16
18
  # redirection natively.
17
19
  class Redirection < Response
18
-
19
20
  # A get is invoked on the url stored in the response headers
20
21
  # under the key 'location' and the new Response is returned.
21
22
  #
22
- # The follow_redirects_count and follow_redirects_limit options
23
+ # The follow_redirects_count and follow_redirects_limit options
23
24
  # should be present. follow_redirects_count will be incremented by 1.
24
25
  #
25
26
  # This method will raise a Wrest::Exceptions::AutoRedirectLimitExceeded
@@ -35,4 +36,4 @@ module Wrest #:nodoc:
35
36
  end
36
37
  end
37
38
  end
38
- end
39
+ end