loquor 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +3 -0
- data/lib/loquor.rb +2 -2
- data/lib/loquor/api_calls/show.rb +5 -1
- data/lib/loquor/client.rb +2 -1
- data/lib/loquor/configuration.rb +1 -0
- data/lib/loquor/http_action.rb +18 -0
- data/lib/loquor/http_actions/get.rb +17 -7
- data/lib/loquor/resource.rb +8 -0
- data/lib/loquor/version.rb +1 -1
- data/test/api_calls/show_test.rb +21 -0
- data/test/client_test.rb +10 -0
- data/test/http_action_test.rb +11 -2
- data/test/http_actions/get_test.rb +39 -0
- data/test/loquor_test.rb +6 -0
- data/test/resource_test.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OWM1ZjMxMmJhNjYyM2IwNWMyMzllNWNiOWIzNzk5Mjc0MjU1ZTUyZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmNkNzIzMTEzNmY0MmE4YTg5NWVhYjdhZDU4YTAwNGFiNjRiZDZjZQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGYxZmZlNzlhZGM4NjViNGNhOTViM2I5Y2E0NzE2MzBjNGYyYTAzZDE2ZDcz
|
10
|
+
MzRmNGJjNDYzMTllNmJiNWVhZTk1ZGJiY2U0MTI1MWMwODI5NjMwNTc5NDc4
|
11
|
+
NzU0YmMyYjlkZDJlMjcwZjE1MGVlYTE0ZTI3MWFmMWQ0MWUzMWM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzJjMWM1ODcwNGI1ZjEyNDc1ZTExYTRlZTI1NDQ4YWFmZmFiNzQ3MTZjOGNl
|
14
|
+
MzlhNjQwMWMwNTkwMGI3NjhiMDUwMzk4N2E5MGI4OWYyZTkzOGEzMDNiZjI2
|
15
|
+
YmVkYTUyNWVlMzA0NzcyMGI5NGVjNWZhMWQ0MmEwODcyYWMyMDQ=
|
data/CHANGELOG.md
CHANGED
data/lib/loquor.rb
CHANGED
data/lib/loquor/client.rb
CHANGED
data/lib/loquor/configuration.rb
CHANGED
data/lib/loquor/http_action.rb
CHANGED
@@ -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 "
|
13
|
-
response = JSON.parse(
|
14
|
-
@config.logger.info "
|
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
|
data/lib/loquor/resource.rb
CHANGED
data/lib/loquor/version.rb
CHANGED
data/test/api_calls/show_test.rb
CHANGED
@@ -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
|
data/test/http_action_test.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
module Loquor
|
2
|
-
|
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
|
-
|
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"
|
data/test/resource_test.rb
CHANGED
@@ -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.
|
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
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: filum
|