sg_postcode 1.2.4 → 1.2.5
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 +4 -4
- data/lib/sg_postcode/converters/long_lat_converter.rb +2 -5
- data/lib/sg_postcode/response/config.rb +2 -2
- data/lib/sg_postcode/services/cache_adapter.rb +26 -0
- data/lib/sg_postcode/services/proxy.rb +6 -2
- data/lib/sg_postcode/version.rb +1 -1
- data/test/response/test_config.rb +29 -0
- data/test/services/test_proxy.rb +11 -0
- data/test/test_sg_postcode.rb +14 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16852417b548612f8e74f646ec0378b9d1c8d517
|
4
|
+
data.tar.gz: e48309bd857ebcc8af595ca9f53121380ca5fbac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82301d3a01f5933f75f593ec15a33a083ef811f9e47f4bee4b7b355aa2016255250dfa0f8461323fe660f5d270fa6896c873261cd06fccc55735283d0b481eb7
|
7
|
+
data.tar.gz: 624b8918f53cc4b3eeb25a87e8a47d65510a1e841c9994a4f4774cdde4e131fdb6941d2bd879c99fab3d350f3168500bbf0ffc133399254fb4d9a8284ca7d6ba
|
@@ -43,9 +43,6 @@ module SgPostcode
|
|
43
43
|
# SgPostcode::LongLatConverter.send_geo_request("230000")
|
44
44
|
#
|
45
45
|
def send_geo_request(postcode)
|
46
|
-
# FIXME: not sure
|
47
|
-
return nil if Module.const_defined? @host
|
48
|
-
|
49
46
|
Response.new(
|
50
47
|
response(postcode),
|
51
48
|
response_type: :json
|
@@ -55,16 +52,16 @@ module SgPostcode
|
|
55
52
|
private
|
56
53
|
|
57
54
|
def response(postcode)
|
58
|
-
Proxy.new(@host, postcode, cache:
|
55
|
+
Proxy.new(@host, postcode, cache: @cache).request
|
59
56
|
end
|
60
57
|
|
61
58
|
def convert_options(opts)
|
62
59
|
@host = class_name(opts[:host]) || :Google
|
63
60
|
@response_type = opts[:response_type] || :json
|
61
|
+
@cache = opts[:cache].nil? ? true : opts[:cache]
|
64
62
|
end
|
65
63
|
|
66
64
|
def class_name(host)
|
67
|
-
# FIXME: not sure
|
68
65
|
return nil unless Module.constants.include? host
|
69
66
|
host.to_sym
|
70
67
|
end
|
@@ -9,7 +9,7 @@ module SgPostcode
|
|
9
9
|
|
10
10
|
# add a custom key_path
|
11
11
|
def add_key_path(key_name, *path)
|
12
|
-
|
12
|
+
fields.merge!({ key_name => path })
|
13
13
|
end
|
14
14
|
|
15
15
|
# remove a key_path
|
@@ -17,7 +17,7 @@ module SgPostcode
|
|
17
17
|
# @return nothing if keypath doesn't exist
|
18
18
|
#
|
19
19
|
def remove_key_path(key_name)
|
20
|
-
|
20
|
+
fields.delete(key_name)
|
21
21
|
end
|
22
22
|
|
23
23
|
# default fields (key_paths)
|
@@ -2,24 +2,50 @@ module SgPostcode
|
|
2
2
|
class CacheAdapter
|
3
3
|
@@hash_name = "SgPostcodeCaching"
|
4
4
|
|
5
|
+
# Init a new CacheAdapter object
|
6
|
+
#
|
7
|
+
# @dependencies: redis
|
8
|
+
#
|
9
|
+
# Redis.new will get REDIS_URL as host
|
10
|
+
# you can customize it by set value for
|
11
|
+
# `ENV["REDIS_URL"]`
|
12
|
+
#
|
13
|
+
# @params
|
14
|
+
# @cache
|
15
|
+
# @key in this context is a postcode
|
16
|
+
#
|
5
17
|
def initialize(key)
|
6
18
|
@cache = Redis.new
|
7
19
|
@key = key
|
8
20
|
end
|
9
21
|
|
22
|
+
# Redefine @@hash_name (key_name in Redis hash store)
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# SgPostcode::CacheAdapter.hash_name = "test_redis_store"
|
26
|
+
#
|
10
27
|
def self.hash_name=(hash_name)
|
11
28
|
@@hash_name = hash_name
|
12
29
|
end
|
13
30
|
|
31
|
+
# Fetch request data from cache
|
32
|
+
#
|
33
|
+
# TODO: will split #value_of method to sub-class
|
34
|
+
# cause Adapter shouldn't call Redis directly
|
35
|
+
#
|
14
36
|
def fetch
|
15
37
|
value_of @key
|
16
38
|
end
|
17
39
|
|
40
|
+
# Store new postcode and request_data to Redis
|
41
|
+
#
|
18
42
|
def store(key, value)
|
19
43
|
@cache.hset(@@hash_name, key, value)
|
20
44
|
value
|
21
45
|
end
|
22
46
|
|
47
|
+
# Get Value of a postcode from Redis Hash Store
|
48
|
+
#
|
23
49
|
def value_of(key)
|
24
50
|
@cache.hget(@@hash_name, key)
|
25
51
|
end
|
@@ -14,9 +14,13 @@ module SgPostcode
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def request
|
17
|
-
return nil unless service
|
17
|
+
return nil unless service
|
18
18
|
|
19
|
-
|
19
|
+
if cache_adapter
|
20
|
+
cache_adapter.fetch || cache_adapter.store(@postcode, service.request)
|
21
|
+
else
|
22
|
+
service.request
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
end
|
data/lib/sg_postcode/version.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestConfig < Minitest::Test
|
4
|
+
def setup
|
5
|
+
SgPostcode::ResponseBuilder::Config.class_variable_set :@@fields, SgPostcode::ResponseBuilder::Config.default_fields
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_default_fields
|
9
|
+
assert_equal 3, SgPostcode::ResponseBuilder::Config.fields.keys.count
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_key
|
13
|
+
SgPostcode::ResponseBuilder::Config.add_key_path(:test, 'first', 'second')
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_add_key_path
|
17
|
+
add_key
|
18
|
+
assert_equal 4, SgPostcode::ResponseBuilder::Config.fields.keys.count
|
19
|
+
assert_operator SgPostcode::ResponseBuilder::Config.fields.keys, :include?, :test
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_remove_key_path
|
23
|
+
add_key
|
24
|
+
|
25
|
+
SgPostcode::ResponseBuilder::Config.remove_key_path :test
|
26
|
+
|
27
|
+
assert_equal false, SgPostcode::ResponseBuilder::Config.fields.keys.include?(:test)
|
28
|
+
end
|
29
|
+
end
|
data/test/services/test_proxy.rb
CHANGED
@@ -54,4 +54,15 @@ class TestProxy < Minitest::Test
|
|
54
54
|
assert_equal true, proxy.request
|
55
55
|
end
|
56
56
|
end
|
57
|
+
|
58
|
+
def test_non_cached
|
59
|
+
nocache_proxy = SgPostcode::Proxy.new(:Google, "238432", cache: false)
|
60
|
+
|
61
|
+
mock = Minitest::Mock.new
|
62
|
+
mock.expect :request, "Send Request"
|
63
|
+
|
64
|
+
nocache_proxy.stub :service, mock do
|
65
|
+
assert_equal "Send Request", nocache_proxy.request
|
66
|
+
end
|
67
|
+
end
|
57
68
|
end
|
data/test/test_sg_postcode.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestSgPostcode < Minitest::Test
|
4
|
+
def setup
|
5
|
+
Redis.new.flushall
|
6
|
+
end
|
7
|
+
|
4
8
|
def test_init_new_sg_postcode_array
|
5
9
|
postcode_array = SgPostcode::Array.new([])
|
6
10
|
assert_kind_of SgPostcode::Array, postcode_array
|
@@ -62,4 +66,14 @@ class TestSgPostcode < Minitest::Test
|
|
62
66
|
convert = SgPostcode::Array.new(['238438'], host: :Google)
|
63
67
|
assert_kind_of Array, convert.convert
|
64
68
|
end
|
69
|
+
|
70
|
+
def test_non_cached
|
71
|
+
converter = SgPostcode::Array.new(['238432'], cache: false)
|
72
|
+
converter.convert
|
73
|
+
|
74
|
+
redis = Redis.new
|
75
|
+
store = SgPostcode::CacheAdapter.class_variable_get :@@hash_name
|
76
|
+
|
77
|
+
assert_equal 0, redis.hkeys(store).length
|
78
|
+
end
|
65
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sg_postcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Duy Khoa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- lib/sg_postcode/version.rb
|
47
47
|
- lib/tasks/sg_postcode_tasks.rake
|
48
48
|
- test/converters/test_long_lat_converter.rb
|
49
|
+
- test/response/test_config.rb
|
49
50
|
- test/response/test_json_output.rb
|
50
51
|
- test/response/test_response_builder.rb
|
51
52
|
- test/sample_response.json
|
@@ -79,6 +80,7 @@ specification_version: 4
|
|
79
80
|
summary: Convert SG Postcode to long lat array
|
80
81
|
test_files:
|
81
82
|
- test/converters/test_long_lat_converter.rb
|
83
|
+
- test/response/test_config.rb
|
82
84
|
- test/response/test_json_output.rb
|
83
85
|
- test/response/test_response_builder.rb
|
84
86
|
- test/sample_response.json
|