loquor 0.5.3 → 0.5.4

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGMyMzdhMjI3OThmN2I0NThmZTBiZjNhZmU0ODJjODFlOWU0ZmJjZg==
4
+ OWM1ZjMxMmJhNjYyM2IwNWMyMzllNWNiOWIzNzk5Mjc0MjU1ZTUyZg==
5
5
  data.tar.gz: !binary |-
6
- OTJkMTFhNDBlNzI0NzU1ZjA4OTVmOTRkODEyMWViOWFkNjhlODY2Yw==
6
+ YmNkNzIzMTEzNmY0MmE4YTg5NWVhYjdhZDU4YTAwNGFiNjRiZDZjZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTY0MWVjYjNkZGNhMWYyM2I1M2NhNjM5MDA5MDRiNTBiOGEyM2E0NGFhZjA2
10
- MjFkNTc4ZTE5YzM5ZWYwZTJjNjY1MmZjODI3MGEyYmVjNGU3OGFlZjM1NjBm
11
- MmUyYzBkMTEwZWViNDU5ZTM1M2ZkMzgzYzE1NWQ5YTJiOGMzMzY=
9
+ ZGYxZmZlNzlhZGM4NjViNGNhOTViM2I5Y2E0NzE2MzBjNGYyYTAzZDE2ZDcz
10
+ MzRmNGJjNDYzMTllNmJiNWVhZTk1ZGJiY2U0MTI1MWMwODI5NjMwNTc5NDc4
11
+ NzU0YmMyYjlkZDJlMjcwZjE1MGVlYTE0ZTI3MWFmMWQ0MWUzMWM=
12
12
  data.tar.gz: !binary |-
13
- MTU2NTY5MDNmMzdlMTEyZTFiMzg2N2I5MDE0YzRhZjE0NDNkM2M1NGNkODZi
14
- Nzg0ZjdmNzI5MDg4NzQwMDEwODA1Mzg3MGUyOTQ1OTVhMzdkNjJjNGQxZjM5
15
- M2E0NzZkN2E0MDVlNzE5ZjJhOGYzNjEyZGUzMGIzOTNlMzk4MTY=
13
+ NzJjMWM1ODcwNGI1ZjEyNDc1ZTExYTRlZTI1NDQ4YWFmZmFiNzQ3MTZjOGNl
14
+ MzlhNjQwMWMwNTkwMGI3NjhiMDUwMzk4N2E5MGI4OWYyZTkzOGEzMDNiZjI2
15
+ YmVkYTUyNWVlMzA0NzcyMGI5NGVjNWZhMWQ0MmEwODcyYWMyMDQ=
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.5.4 / 2013-12-13
2
+ * [FEATURE] Add support for basic resource caching
3
+
1
4
  # 0.5.3 / 2013-12-11
2
5
  * [FEATURE] Add support for overridding stubbed responses
3
6
 
data/lib/loquor.rb CHANGED
@@ -22,8 +22,8 @@ module Loquor
22
22
  end
23
23
  end
24
24
 
25
- def self.get(url)
26
- loquor.get(url)
25
+ def self.get(url, *args)
26
+ loquor.get(url, *args)
27
27
  end
28
28
 
29
29
  def self.put(url, payload)
@@ -7,7 +7,11 @@ module Loquor
7
7
  end
8
8
 
9
9
  def execute
10
- obj = Loquor.get("#{klass.path}/#{@id}")
10
+ obj = if (klass.cache)
11
+ Loquor.get("#{klass.path}/#{@id}", cache=klass.cache)
12
+ else
13
+ Loquor.get("#{klass.path}/#{@id}")
14
+ end
11
15
  @klass.new(obj)
12
16
  end
13
17
  end
data/lib/loquor/client.rb CHANGED
@@ -6,8 +6,9 @@ module Loquor
6
6
  @config = Configuration.new
7
7
  end
8
8
 
9
- def get(url)
9
+ def get(url, cache=nil)
10
10
  deps = {config: @config}
11
+ deps[:should_cache] = cache if cache
11
12
  HttpAction::Get.get(url, deps)
12
13
  end
13
14
 
@@ -11,6 +11,7 @@ module Loquor
11
11
  ]
12
12
 
13
13
  attr_writer *SETTINGS
14
+ attr_accessor :cache
14
15
 
15
16
  def initialize
16
17
  Filum.config do |config|
@@ -3,12 +3,30 @@ module Loquor
3
3
  def initialize(url, deps)
4
4
  @url = url
5
5
  @config = deps[:config]
6
+ @should_cache = deps[:should_cache]
6
7
  end
7
8
 
8
9
  def signed_request
9
10
  @config.logger.info "Signing request."
10
11
  ApiAuth.sign!(request, @config.access_id, @config.secret_key)
11
12
  end
13
+
14
+ def execute
15
+ @config.logger.info "Making HTTP request to: #{full_url}"
16
+ signed_request.execute
17
+ rescue RestClient::ResourceNotFound => e
18
+ @config.logger.error("HTTP 404 when accessing #{full_url}")
19
+ raise
20
+ rescue => e
21
+ @config.logger.error("Exception while executing request: #{e.message} <#{e.class}>")
22
+ raise
23
+ end
24
+
25
+ private
26
+
27
+ def full_url
28
+ "#{@config.endpoint}#{@url}"
29
+ end
12
30
  end
13
31
  end
14
32
 
@@ -9,19 +9,29 @@ module Loquor
9
9
  end
10
10
 
11
11
  def get
12
- @config.logger.info "Making GET request to: #{full_url}"
13
- response = JSON.parse(signed_request.execute)
14
- @config.logger.info "Signed request executed. Response: #{response}"
12
+ @config.logger.info "GET: #{full_url}"
13
+ response = @should_cache ? JSON.parse(execute_against_cache) : JSON.parse(execute)
14
+ @config.logger.info "Response: #{response}"
15
15
  response
16
16
  end
17
+
18
+ def execute_against_cache
19
+ cache = @config.cache
20
+ if cache
21
+ val = cache.get(request.url)
22
+ unless val
23
+ val = execute
24
+ cache.set(request.url, val)
25
+ end
26
+ val
27
+ else
28
+ execute
29
+ end
30
+ end
17
31
 
18
32
  private
19
33
  def request
20
34
  RestClient::Request.new(url: full_url, method: :get)
21
35
  end
22
-
23
- def full_url
24
- "#{@config.endpoint}#{@url}"
25
- end
26
36
  end
27
37
  end
@@ -17,6 +17,14 @@ module Loquor
17
17
  @path
18
18
  end
19
19
 
20
+ def self.cache=(value)
21
+ @value = value
22
+ end
23
+
24
+ def self.cache
25
+ @value
26
+ end
27
+
20
28
  def self.find(id)
21
29
  ApiCall::Show.new(self, id).execute
22
30
  end
@@ -1,3 +1,3 @@
1
1
  module Loquor
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
@@ -3,6 +3,27 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Loquor
4
4
  class ApiCall::ShowTest < Minitest::Test
5
5
 
6
+ class NormalResource < Resource
7
+ self.path = '/normal'
8
+ end
9
+
10
+ class CachedResource < Resource
11
+ self.path = '/cached'
12
+ self.cache = true
13
+ end
14
+
15
+ def test_request_correct_path
16
+ show = ApiCall::Show.new(NormalResource, 42)
17
+ Loquor.expects(:get).with('/normal/42').returns({}.to_json)
18
+ show.execute
19
+ end
20
+
21
+ def test_request_correct_path_for_cachable_resources
22
+ show = ApiCall::Show.new(CachedResource, 42)
23
+ Loquor.expects(:get).with('/cached/42', cache=true).returns({}.to_json)
24
+ show.execute
25
+ end
26
+
6
27
  def test_response_is_a_representation
7
28
  show = ApiCall::Show.new(Resource, 1)
8
29
  Loquor.stubs(get: {foo: 'bar'}.to_json)
data/test/client_test.rb CHANGED
@@ -35,5 +35,15 @@ module Loquor
35
35
  HttpAction::Post.expects(:post).with(url, payload, deps)
36
36
  client.post(url, payload)
37
37
  end
38
+
39
+
40
+ def test_get_calls_gets_with_cache_flag
41
+ url = "foobar"
42
+
43
+ client = Client.new
44
+ deps = {config: client.config, should_cache: true}
45
+ HttpAction::Get.expects(:get).with(url, deps)
46
+ client.get(url, cache=true)
47
+ end
38
48
  end
39
49
  end
@@ -1,10 +1,11 @@
1
1
  module Loquor
2
- class HttpAction::Test < Minitest::Test
2
+ class HttpAction::Test < Minitest::Test
3
3
  def setup
4
4
  super
5
5
  @access_id = "123"
6
6
  @secret_key = "Foobar132"
7
7
  @endpoint = "http://www.thefoobar.com"
8
+ @cache = mock()
8
9
  end
9
10
 
10
11
  def deps
@@ -16,7 +17,15 @@ module Loquor
16
17
  config.stubs(access_id: @access_id)
17
18
  config.stubs(secret_key: @secret_key)
18
19
  config.stubs(endpoint: @endpoint)
20
+ config.stubs(cache: @cache)
19
21
  {config: config}
20
22
  end
21
- end
23
+
24
+ def test_execute_signs_and_executes
25
+ json = "{}"
26
+ action = HttpAction.new("", deps)
27
+ action.expects(signed_request: mock(execute: json))
28
+ assert_equal json, action.send(:execute)
29
+ end
30
+ end
22
31
  end
@@ -39,5 +39,44 @@ module Loquor
39
39
  gets.send(:signed_request)
40
40
  end
41
41
 
42
+ def test_get_cached_resource
43
+ output = {'foo' => 'bar'}
44
+ json = output.to_json
45
+
46
+ gets = HttpAction::Get.new("", deps.merge({should_cache: true}))
47
+ gets.expects(execute_against_cache: json)
48
+ assert_equal output, gets.get
49
+ end
50
+
51
+ def test_cache_hit
52
+ url = "/resource"
53
+ full_url = "#{@endpoint}#{url}"
54
+ cached_value = "{}"
55
+ @cache.expects(:get).with(full_url).returns(cached_value)
56
+ gets = HttpAction::Get.new(url, deps.merge({should_cache: true}))
57
+ assert_equal cached_value, gets.send(:execute_against_cache)
58
+ end
59
+
60
+ def test_execute_http_call_on_cache_miss
61
+ url = "/resource"
62
+ full_url = "#{@endpoint}#{url}"
63
+ json_response = "{}"
64
+
65
+ @cache.expects(:get).with(full_url).returns(nil)
66
+ @cache.expects(:set).with(full_url, json_response)
67
+
68
+ gets = HttpAction::Get.new(url, deps.merge({should_cache: true}))
69
+ gets.expects(execute: json_response)
70
+ assert_equal json_response, gets.send(:execute_against_cache)
71
+ end
72
+
73
+ def test_does_not_cache_when_no_cache_configured
74
+ json_response = "{}"
75
+
76
+ @cache = nil
77
+ gets = HttpAction::Get.new("", deps.merge({should_cache: true}))
78
+ gets.expects(execute: json_response)
79
+ gets.send(:execute_against_cache)
80
+ end
42
81
  end
43
82
  end
data/test/loquor_test.rb CHANGED
@@ -7,6 +7,12 @@ module Loquor
7
7
  Client.any_instance.expects(:get).with(url)
8
8
  Loquor.get(url)
9
9
  end
10
+
11
+ def test_get_calls_get_passing_on_args
12
+ url = "foo"
13
+ Client.any_instance.expects(:get).with(url, cached=true)
14
+ Loquor.get(url, cached=true)
15
+ end
10
16
 
11
17
  def test_put_calls_put
12
18
  url = "foo"
@@ -43,5 +43,13 @@ module Loquor
43
43
  Loquor.expects(:put).with("/foobar/#{id}", payload: payload)
44
44
  Foobar.update(id, payload)
45
45
  end
46
+
47
+ def test_can_read_path
48
+ assert_equal "/foobar", Foobar.path
49
+ end
50
+
51
+ def test_cache_flag_false_by_default
52
+ refute Foobar.cache
53
+ end
46
54
  end
47
55
  end
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: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-11 00:00:00.000000000 Z
11
+ date: 2013-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: filum