resterl 0.1.0 → 0.1.3

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
- SHA1:
3
- metadata.gz: ba291d5cad21d29e786dffb4c29f6f95af7ac42a
4
- data.tar.gz: 849817253c65f4c23edde761e654d7f3ad70144c
2
+ SHA256:
3
+ metadata.gz: b9ade8f52c4e1a0ff6e52069668fa41a3f0e573ea8ab0016115d7fd849ca1684
4
+ data.tar.gz: 07722b8b4c023e328dfc4ba313f23fdef5790df4a0f168d77d1fb82cc489c8e6
5
5
  SHA512:
6
- metadata.gz: a51fef712af70ca0cd8d3c47ca5ce522461d8de13d902c157b39fb434b30b1a7053ca965fc8b772c0af3d18d23f42fd39eaa177143146e8f82a87c1309d6bc36
7
- data.tar.gz: 15bfd502fc79be97912caae50d42c9899b4ef5d42836b13bf7813e13555b8e6e77603abbc927cd4b5aeb478da039927ca8942442d9d720a6f3e50a4e9c561284
6
+ metadata.gz: 1411bfc88816e1e9e2fc551e6a119da9960a57ae2d443a39738399e4bedcd15fa21a3b59bfa825a06395a45c0602d161a3a9f45bec6f11ca19a3b66c8bfc2b37
7
+ data.tar.gz: 62d40715c602a4e425e6bc91b6c531c94b4b9dd45ad8a8ed1c15365a2c343549a5af2120ca4c52129a5eb8034e4b0411847986b721286b69a33ee2e6556caf61
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resterl
4
+ module Caches
5
+ class CacheKeyGenerator
6
+ def self.generate(url, params, headers)
7
+ normalized = [url, params, headers].map do |part|
8
+ part.is_a?(Hash) ? part.sort : part
9
+ end.each_with_object(+'') { |e, acc| acc << e.inspect }
10
+
11
+ Digest::SHA1.hexdigest(normalized)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -97,8 +97,8 @@ module Resterl
97
97
  end
98
98
  end
99
99
 
100
- def data_to_cache_key *args
101
- args.hash
100
+ def data_to_cache_key url, params, headers
101
+ Caches::CacheKeyGenerator.generate(url, params, headers)
102
102
  end
103
103
 
104
104
  def get_response(request, old_response)
@@ -1,4 +1,5 @@
1
1
  require 'active_support/core_ext/object/blank'
2
+ require 'active_support/core_ext/hash'
2
3
 
3
4
  module Resterl
4
5
  class GenericRequest
@@ -29,11 +30,7 @@ module Resterl
29
30
  end
30
31
 
31
32
  def query_param_string
32
- @query_param_string ||= begin
33
- @query_params.collect do |key, value|
34
- "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
35
- end.join('&')
36
- end
33
+ @query_param_string ||= @query_params.to_query
37
34
  end
38
35
 
39
36
  def apply_basic_auth request
@@ -1,3 +1,3 @@
1
1
  module Resterl
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/lib/resterl.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'net/http'
2
2
  require 'net/https'
3
- require 'yajl/json_gem'
4
3
  ################################################################################
5
4
  module Resterl
6
5
  module Caches; end
@@ -14,6 +13,7 @@ require 'resterl/caches/redis_cache'
14
13
  require 'resterl/caches/simple_cache'
15
14
  require 'resterl/caches/rails_memcached_cache'
16
15
  require 'resterl/caches/key_prefix_decorator'
16
+ require 'resterl/caches/cache_key_generator'
17
17
  require 'resterl/caches/nil_cache'
18
18
 
19
19
  require 'resterl/client'
data/resterl.gemspec CHANGED
@@ -16,7 +16,6 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_runtime_dependency 'activesupport', '>= 3.2.0'
18
18
  s.add_runtime_dependency 'hashie', '~> 3.5.7'
19
- s.add_runtime_dependency 'yajl-ruby', '~> 1.1'
20
19
  s.add_development_dependency 'rspec', '~> 3.7'
21
20
  s.add_development_dependency 'webmock', '~> 3.4.1'
22
21
  s.add_development_dependency 'timecop', '~> 0.9.1'
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resterl::Caches::CacheKeyGenerator do
4
+ it 'generates a stable digest for the same url' do
5
+ a = described_class.generate('http://example.com', {}, {})
6
+ b = described_class.generate('http://example.com', {}, {})
7
+
8
+ expect(a).to eq b
9
+ end
10
+
11
+ it 'generates a stable digest with the same parameters' do
12
+ a = described_class
13
+ .generate('http://example.com', { 'foo' => 'bar' }, {})
14
+ b = described_class
15
+ .generate('http://example.com', { 'foo' => 'bar' }, {})
16
+
17
+ expect(a).to eq b
18
+ end
19
+
20
+ it 'generates a stable digest with the same headers' do
21
+ a = described_class
22
+ .generate('http://example.com', {}, 'x-test' => '0.5')
23
+ b = described_class
24
+ .generate('http://example.com', {}, 'x-test' => '0.5')
25
+
26
+ expect(a).to eq b
27
+ end
28
+
29
+ it 'generates a stable digest with same url, params and headers' do
30
+ a = described_class
31
+ .generate('http://example.com', { 'xy' => 'xxx' }, 'x-test' => '0.5')
32
+ b = described_class
33
+ .generate('http://example.com', { 'xy' => 'xxx' }, 'x-test' => '0.5')
34
+
35
+ expect(a).to eq b
36
+ end
37
+
38
+ it 'generates different digest for different urls' do
39
+ a = described_class.generate('http://example.net', {}, {})
40
+ b = described_class.generate('http://example.com', {}, {})
41
+
42
+ expect(a).not_to eq b
43
+ end
44
+
45
+ it 'generates different digest with different parameters' do
46
+ a = described_class.generate('http://example.com', { 'blib' => 'blub' }, {})
47
+ b = described_class.generate('http://example.com', { 'blib' => 'oopf' }, {})
48
+ c = described_class.generate('http://example.com', { 'oopf' => 'blip' }, {})
49
+
50
+ expect([a, b, c].uniq.length).to eq 3
51
+ end
52
+
53
+ it 'generates different digest with different headers' do
54
+ a = described_class
55
+ .generate('http://example.com', {}, 'accept' => 'json')
56
+ b = described_class
57
+ .generate('http://example.com', {}, 'accept' => '*/*')
58
+
59
+ expect(a).not_to eq b
60
+ end
61
+
62
+ it 'generates the same digest with url parameters and headers ' \
63
+ 'in diffrent order' do
64
+ a = described_class.generate(
65
+ 'http://example.com',
66
+ { 'a' => 'aaa', 'b' => 'bbb' },
67
+ 'x-aaa' => 'aaa', 'x-bbb' => 'bbb'
68
+ )
69
+ b = described_class .generate(
70
+ 'http://example.com',
71
+ { 'b' => 'bbb', 'a' => 'aaa' },
72
+ 'x-bbb' => 'bbb', 'x-aaa' => 'aaa'
73
+ )
74
+
75
+ expect(a).to eq b
76
+ end
77
+ end
@@ -4,6 +4,8 @@ require 'webmock/rspec'
4
4
  require 'timecop'
5
5
 
6
6
  describe Resterl::Client do
7
+ subject(:client) { Resterl::Client.new }
8
+
7
9
  describe 'minimum cache lifetime' do
8
10
  before do
9
11
  Timecop.freeze(Time.parse('2015-01-01 01:00'))
@@ -14,7 +16,6 @@ describe Resterl::Client do
14
16
  headers: { cache_control: 'max-age=0, private, must-revalidate' }
15
17
  ).then.to_raise('2 requests')
16
18
  end
17
- let(:client) { Resterl::Client.new }
18
19
 
19
20
  it 'is used in "max-age=0" case' do
20
21
  response = client.get 'http://www.example.com/', {}, {}
@@ -31,4 +32,12 @@ describe Resterl::Client do
31
32
  end.not_to raise_error
32
33
  end
33
34
  end
35
+
36
+ it 'encodes url params' do
37
+ stub_request(
38
+ :get, "http://www.example.com/?text=Ein%20gro%C3%9Fer%20Spa%C3%9F!"
39
+ ).to_return(status: 200, body: '', headers: {})
40
+
41
+ client.get 'http://www.example.com/', { text: 'Ein großer Spaß!' }, {}
42
+ end
34
43
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resterl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dütsch
8
8
  - Stefan Hoffmann
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-05-22 00:00:00.000000000 Z
12
+ date: 2022-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -39,20 +39,6 @@ dependencies:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: 3.5.7
42
- - !ruby/object:Gem::Dependency
43
- name: yajl-ruby
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '1.1'
49
- type: :runtime
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - "~>"
54
- - !ruby/object:Gem::Version
55
- version: '1.1'
56
42
  - !ruby/object:Gem::Dependency
57
43
  name: rspec
58
44
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +109,7 @@ dependencies:
123
109
  - - ">="
124
110
  - !ruby/object:Gem::Version
125
111
  version: '0'
126
- description:
112
+ description:
127
113
  email:
128
114
  - florian.duetsch@nix-wie-weg.de
129
115
  - stefan.hoffmann@nix-wie-weg.de
@@ -134,6 +120,7 @@ files:
134
120
  - ".document"
135
121
  - ".gitignore"
136
122
  - ".rubocop.yml"
123
+ - ".ruby-version"
137
124
  - Gemfile
138
125
  - LICENSE
139
126
  - README.rdoc
@@ -141,6 +128,7 @@ files:
141
128
  - lib/resterl.rb
142
129
  - lib/resterl/base_object.rb
143
130
  - lib/resterl/caches/cache_interface.rb
131
+ - lib/resterl/caches/cache_key_generator.rb
144
132
  - lib/resterl/caches/key_prefix_decorator.rb
145
133
  - lib/resterl/caches/nil_cache.rb
146
134
  - lib/resterl/caches/rails_memcached_cache.rb
@@ -157,12 +145,13 @@ files:
157
145
  - lib/resterl/version.rb
158
146
  - resterl.gemspec
159
147
  - spec/lib/resterl/base_object_spec.rb
148
+ - spec/lib/resterl/caches/cache_key_generator_spec.rb
160
149
  - spec/lib/resterl/client_spec.rb
161
150
  - spec/spec_helper.rb
162
151
  homepage: https://github.com/Nix-wie-weg/resterl
163
152
  licenses: []
164
153
  metadata: {}
165
- post_install_message:
154
+ post_install_message:
166
155
  rdoc_options: []
167
156
  require_paths:
168
157
  - lib
@@ -177,12 +166,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
166
  - !ruby/object:Gem::Version
178
167
  version: '0'
179
168
  requirements: []
180
- rubyforge_project:
181
- rubygems_version: 2.4.5.1
182
- signing_key:
169
+ rubygems_version: 3.1.6
170
+ signing_key:
183
171
  specification_version: 4
184
172
  summary: Rudimentary HTTP client with focus on caching
185
173
  test_files:
186
174
  - spec/lib/resterl/base_object_spec.rb
175
+ - spec/lib/resterl/caches/cache_key_generator_spec.rb
187
176
  - spec/lib/resterl/client_spec.rb
188
177
  - spec/spec_helper.rb