loquor 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 699e2b00b4ee1162574675896d215c952652901d
4
- data.tar.gz: 89dc1db3265fd242b06562ace2eb27f8e2cb4af3
3
+ metadata.gz: 632149903ddd90cf5dc6edaede05ac490317090c
4
+ data.tar.gz: a523cf47111b39b1086a8f378316bac2fbf78a6d
5
5
  SHA512:
6
- metadata.gz: 8911399fba9e05bdce37fa853a55f93a743c2ed31ab7741f93c292179ffe8da2d4c43dc95944e82311d85716d0dba46a16688033588cfb420fc1c64645010a3a
7
- data.tar.gz: 02668ec5bdc32239fc620dca4160dfc6eafcc47c8eb372edab5ea769f45289a743e749f14d00cf038fd445b85a768159c432c86a4b7270e8e14923e06a0434dd
6
+ metadata.gz: ddd8547f47aab0d704b909488c4a59cf569ad7b9bccefb8fb95f313b2aeb7adb1b6b774fe6153c523cecf81d5fdace2dfa7a873f260623ceac00658fced90192
7
+ data.tar.gz: 43e207ddc2425f44db2a8bc6138b67bff3853b7d6155c61a343d3ab8a4ab41fd22fa59fe87511abcbdade03a5d60a8e1b4658d195903438072fa5265e2bdafba
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 1.5.0 / 2014-02-27
2
+ * [FEATURE] Add retry support for 404s
3
+
1
4
  # 1.4.0 / 2014-02-22
2
5
  * [FEATURE] Use Filum v2.
3
6
 
@@ -7,12 +7,22 @@ module Loquor
7
7
  end
8
8
 
9
9
  def execute
10
- obj = if (klass.cache)
11
- Loquor.get("#{klass.path}/#{@id}", cache=klass.cache)
12
- else
13
- Loquor.get("#{klass.path}/#{@id}")
10
+ begin
11
+ get_data
12
+ rescue RestClient::ResourceNotFound
13
+ if Loquor.config.retry_404s
14
+ sleep(1)
15
+ get_data
16
+ else
17
+ raise
18
+ end
14
19
  end
15
- @klass.new(obj)
20
+ end
21
+
22
+ private
23
+ def get_data
24
+ options = {cache: klass.cache}
25
+ klass.new Loquor.get("#{klass.path}/#{@id}", options)
16
26
  end
17
27
  end
18
28
  end
data/lib/loquor/client.rb CHANGED
@@ -6,9 +6,9 @@ module Loquor
6
6
  @config = Configuration.new
7
7
  end
8
8
 
9
- def get(url, cache=nil)
9
+ def get(url, options = {})
10
10
  deps = {config: @config}
11
- deps[:should_cache] = cache if cache
11
+ deps[:should_cache] = options[:cache] if options[:cache]
12
12
  HttpAction::Get.get(url, deps)
13
13
  end
14
14
 
@@ -7,7 +7,7 @@ module Loquor
7
7
  class Configuration
8
8
 
9
9
  SETTINGS = [
10
- :logger, :access_id, :secret_key, :endpoint, :substitute_values
10
+ :logger, :access_id, :secret_key, :endpoint, :substitute_values, :retry_404s
11
11
  ]
12
12
 
13
13
  attr_writer *SETTINGS
@@ -16,6 +16,7 @@ module Loquor
16
16
  def initialize
17
17
  self.logger = Filum.logger
18
18
  self.substitute_values = {}
19
+ self.retry_404s = false
19
20
  end
20
21
 
21
22
  SETTINGS.each do |setting|
@@ -27,8 +28,9 @@ module Loquor
27
28
  private
28
29
 
29
30
  def get_or_raise(setting)
30
- instance_variable_get("@#{setting.to_s}") ||
31
- raise(LoquorConfigurationError.new("Configuration for #{setting} is not set"))
31
+ val = instance_variable_get("@#{setting.to_s}")
32
+ raise(LoquorConfigurationError.new("Configuration for #{setting} is not set")) if val.nil?
33
+ val
32
34
  end
33
35
  end
34
36
  end
@@ -1,3 +1,3 @@
1
1
  module Loquor
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -9,21 +9,41 @@ module Loquor
9
9
 
10
10
  class CachedResource < Resource
11
11
  self.path = '/cached'
12
- self.cache = true
12
+ self.cache = true
13
13
  end
14
14
 
15
15
  def test_request_correct_path
16
16
  show = ApiCall::Show.new(NormalResource, 42)
17
- Loquor.expects(:get).with('/normal/42').returns({}.to_json)
17
+ Loquor.expects(:get).with('/normal/42', {cache: nil}).returns({}.to_json)
18
18
  show.execute
19
19
  end
20
-
20
+
21
21
  def test_request_correct_path_for_cachable_resources
22
22
  show = ApiCall::Show.new(CachedResource, 42)
23
- Loquor.expects(:get).with('/cached/42', cache=true).returns({}.to_json)
23
+ Loquor.expects(:get).with('/cached/42', {cache: true}).returns({}.to_json)
24
24
  show.execute
25
25
  end
26
26
 
27
+ def test_should_retry_for_404_if_configured
28
+ Loquor.config.retry_404s = true
29
+ Loquor.expects(:get).twice.raises(RestClient::ResourceNotFound)
30
+
31
+ show = ApiCall::Show.new(CachedResource, 42)
32
+ assert_raises(RestClient::ResourceNotFound) do
33
+ show.execute
34
+ end
35
+ end
36
+
37
+ def test_should_not_retry_for_404_if_configured
38
+ Loquor.config.retry_404s = false
39
+ Loquor.expects(:get).once.raises(RestClient::ResourceNotFound)
40
+
41
+ show = ApiCall::Show.new(CachedResource, 42)
42
+ assert_raises(RestClient::ResourceNotFound) do
43
+ show.execute
44
+ end
45
+ end
46
+
27
47
  def test_response_is_a_representation
28
48
  show = ApiCall::Show.new(Resource, 1)
29
49
  Loquor.stubs(get: {foo: 'bar'}.to_json)
data/test/client_test.rb CHANGED
@@ -35,7 +35,7 @@ module Loquor
35
35
  HttpAction::Post.expects(:post).with(url, payload, deps)
36
36
  client.post(url, payload)
37
37
  end
38
-
38
+
39
39
 
40
40
  def test_get_calls_gets_with_cache_flag
41
41
  url = "foobar"
@@ -43,7 +43,7 @@ module Loquor
43
43
  client = Client.new
44
44
  deps = {config: client.config, should_cache: true}
45
45
  HttpAction::Get.expects(:get).with(url, deps)
46
- client.get(url, cache=true)
46
+ client.get(url, cache: true)
47
47
  end
48
48
  end
49
49
  end
@@ -37,6 +37,22 @@ module Loquor
37
37
  assert_equal endpoint, Loquor.config.endpoint
38
38
  end
39
39
 
40
+ def test_retry_404s_should_be_false_by_default
41
+ assert_equal false, Loquor.config.retry_404s
42
+ end
43
+
44
+ def test_retry_404s_should_set_to_false
45
+ retry_404s = false
46
+ Loquor.config.retry_404s = retry_404s
47
+ assert_equal retry_404s, Loquor.config.retry_404s
48
+ end
49
+
50
+ def test_retry_404s_should_set_to_false
51
+ retry_404s = true
52
+ Loquor.config.retry_404s = retry_404s
53
+ assert_equal retry_404s, Loquor.config.retry_404s
54
+ end
55
+
40
56
  def test_substitute_values
41
57
  substitute_values = {foo: 'bar'}
42
58
  Loquor.config.substitute_values = substitute_values
@@ -47,7 +47,7 @@ module Loquor
47
47
  gets.expects(execute_against_cache: json)
48
48
  assert_equal output, gets.get
49
49
  end
50
-
50
+
51
51
  def test_cache_hit
52
52
  url = "/resource"
53
53
  full_url = "#{@endpoint}#{url}"
@@ -58,10 +58,10 @@ module Loquor
58
58
  end
59
59
 
60
60
  def test_execute_http_call_on_cache_miss
61
- url = "/resource"
61
+ url = "/resource"
62
62
  full_url = "#{@endpoint}#{url}"
63
63
  json_response = "{}"
64
-
64
+
65
65
  @cache.expects(:get).with(full_url).returns(nil)
66
66
  @cache.expects(:set).with(full_url, json_response)
67
67
 
@@ -72,7 +72,7 @@ module Loquor
72
72
 
73
73
  def test_does_not_cache_when_no_cache_configured
74
74
  json_response = "{}"
75
-
75
+
76
76
  @cache = nil
77
77
  gets = HttpAction::Get.new("", deps.merge({should_cache: true}))
78
78
  gets.expects(execute: json_response)
@@ -23,7 +23,7 @@ module Loquor
23
23
 
24
24
  def test_find_should_get_correct_path_with_simple_path
25
25
  id = 8
26
- Loquor.expects(:get).with("/foobar/#{id}")
26
+ Loquor.expects(:get).with("/foobar/#{id}", {cache: nil})
27
27
  Foobar.find(id)
28
28
  end
29
29
 
data/test/test_helper.rb CHANGED
@@ -12,6 +12,7 @@ require "loquor"
12
12
  class Minitest::Test
13
13
  def setup
14
14
  Filum.setup("./log/test.log")
15
+ Loquor.instance_variable_set :@config, nil
15
16
  Loquor.config do |config|
16
17
  config.access_id = "Sermo"
17
18
  config.secret_key = "foobar"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loquor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-23 00:00:00.000000000 Z
11
+ date: 2014-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: filum