sg_postcode 1.1.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +29 -0
- data/lib/sg_postcode.rb +22 -0
- data/lib/sg_postcode/array.rb +30 -0
- data/lib/sg_postcode/converters/long_lat_converter.rb +87 -0
- data/lib/sg_postcode/response/config.rb +34 -0
- data/lib/sg_postcode/response/json_output.rb +48 -0
- data/lib/sg_postcode/response/response.rb +22 -0
- data/lib/sg_postcode/response/response_builder.rb +21 -0
- data/lib/sg_postcode/services/google.rb +22 -0
- data/lib/sg_postcode/version.rb +3 -0
- data/lib/tasks/sg_postcode_tasks.rake +5 -0
- data/test/converters/test_long_lat_converter.rb +17 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/homes_controller.rb +8 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/homes/index.html.erb +141 -0
- data/test/dummy/app/views/layouts/application.html.erb +23 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +57 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +5046 -0
- data/test/dummy/log/test.log +225 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/-vQYSUkE9lGmrmiORXoPfBwAf3hR234Yok5jnBQrzyo.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/9DGvqRf86Gyp3vd0KXPT1Xhx9MyFgIZywKHd-SN_vj0.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/CiSgr1SAu2SQdLy0phRQpULdqgyMSS2NtQpuHlxzB6s.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/RCSidKcxbxGQunodZFTx_an2UpxtX0c4CgWUAifF1Zc.cache +3 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/RzQHQwY4ozzo0ESckKkxMAZBV2a9PFG4f27Ao0lIZOk.cache +2 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/VMj2FphcEriKcPhX84DUfL4KLpP8lwwOX3YiUlddU8Q.cache +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/WXAJAhHe2YtSSQHEqqlrP41iupUol2lRZHo9aSDtqDk.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/_LRrankhfE2ypIWNdzZlGeJp_CnDa9Limi8PMPenLGs.cache +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/kSxhTk2OTbIhyjFKfQKiwi9N89JVI2KMi2-t4I2nkKo.cache +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/kgKFQq9K2CKWDviK9J2kse2ZTlIFyP_yFiSBv5HAZWY.cache +3 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/l5QHQQcUNuQQC_6QrqHceFwt6ukt54_erDN70ansVL4.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/lz1MBX9tRsdihuh-tESZsyE93OVaYcpwQQTOD4WIEG8.cache +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/meCcv5Dm5dZTAH3JEtCm80y9z6fLXLVcTaKDik82Vbs.cache +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/uiSc7dBC0BJJjflpgOIsEPJlC1BL3UAtHZD4yDYa9Zo.cache +2 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/xDi0XH-TKuUZ70g472LXoPtaOpeNK-iGRkR4_A1Nsqw.cache +1 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/zHq3zhOzmWMYy7_JkcW1UXBNAtYl_r4kz_Ax8mdeLOI.cache +1 -0
- data/test/dummy/tmp/pids/server.pid +1 -0
- data/test/response/test_json_output.rb +15 -0
- data/test/response/test_response_builder.rb +72 -0
- data/test/sample_response.json +49 -0
- data/test/test_helper.rb +24 -0
- data/test/test_sg_postcode.rb +51 -0
- metadata +211 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3241ed35ffe07e0b40a10204bf2a7952131a5648
|
4
|
+
data.tar.gz: 3677670b8801efb86907e252b3923e4979872fe1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42613712019d6d930c31cbbbf6dd5ebecd6c4b812dd2a11d032e1618ddbf2a5685c19ebf38cff753bb4c71e87ddf1851d26ad6c9f745abe7416c5b2c3f6424dd
|
7
|
+
data.tar.gz: b51d668090072588f13e2d6ea0102f17e18116fe8c5a5dd52ad44f188a8a206ecb1a2f726a9fc1b3007868d234269cbf75fb455013017de5daa9f76f2da817b9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Duy Khoa
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SgPostcode'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/test_*.rb'
|
25
|
+
t.verbose = false
|
26
|
+
t.options = '--pride'
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
data/lib/sg_postcode.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
end
|
3
|
+
|
4
|
+
# Add these path to $LOAD_PATH
|
5
|
+
#
|
6
|
+
# so later, instead of writing
|
7
|
+
# require 'converters/json_ouput'
|
8
|
+
# we can use
|
9
|
+
# require 'json_ouput'
|
10
|
+
# cause the converters directory already in $LOAD_PATH
|
11
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "sg_postcode"))
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "sg_postcode", "converters"))
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "sg_postcode", "services"))
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "sg_postcode", "response"))
|
15
|
+
|
16
|
+
require "response"
|
17
|
+
require "response_builder"
|
18
|
+
require "json_output"
|
19
|
+
require "config"
|
20
|
+
require "array"
|
21
|
+
require "google"
|
22
|
+
require "long_lat_converter"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
class Array
|
3
|
+
# Init a new array object
|
4
|
+
#
|
5
|
+
# @params
|
6
|
+
# - postcodes: array[String] of postcode
|
7
|
+
# - opts: hash of option
|
8
|
+
# + response_type: default is :json, check the response folder
|
9
|
+
# + host: :Google is default, check services folder
|
10
|
+
#
|
11
|
+
def initialize(postcodes, opts = {})
|
12
|
+
@postcodes = postcodes
|
13
|
+
@opts = opts
|
14
|
+
end
|
15
|
+
|
16
|
+
# Convert to a long lat array
|
17
|
+
#
|
18
|
+
# @return [{ long: '123456', lat: '123456' }, ...]
|
19
|
+
#
|
20
|
+
# @params
|
21
|
+
# opts: list of options from #initializer
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# SgPostcode::Array.new(postcodes).convert
|
25
|
+
#
|
26
|
+
def convert
|
27
|
+
LongLatConverter.new(@postcodes, @opts).convert
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
class LongLatConverter
|
3
|
+
attr_reader :postcodes
|
4
|
+
|
5
|
+
def initialize(postcodes, opts = {})
|
6
|
+
@postcodes = postcodes
|
7
|
+
convert_options opts
|
8
|
+
end
|
9
|
+
|
10
|
+
# Convert an array of SG Postcode
|
11
|
+
#
|
12
|
+
# @return an array contains long, lat
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# postcodes = ['238432', '247964']
|
16
|
+
# SgPostCode::LongLatConverter.new(postcodes).convert
|
17
|
+
#
|
18
|
+
def convert
|
19
|
+
postcodes
|
20
|
+
.uniq(&:itself)
|
21
|
+
.map { |postcode| density_of(postcode, place_info(postcode)) }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Request info from host for a postcode
|
25
|
+
#
|
26
|
+
# @return hash of info, check
|
27
|
+
# response/config.rb to see the info fields
|
28
|
+
#
|
29
|
+
# @params: postcode number [String]
|
30
|
+
#
|
31
|
+
def place_info(postcode)
|
32
|
+
send_geo_request(postcode).data
|
33
|
+
end
|
34
|
+
|
35
|
+
# Send request to host, and return the response
|
36
|
+
#
|
37
|
+
# @return
|
38
|
+
#
|
39
|
+
# @params
|
40
|
+
# postcode
|
41
|
+
#
|
42
|
+
# @example
|
43
|
+
# SgPostcode::LongLatConverter.send_geo_request("230000")
|
44
|
+
#
|
45
|
+
def send_geo_request(postcode)
|
46
|
+
return nil if Module.const_defined? @host
|
47
|
+
|
48
|
+
Response.new(
|
49
|
+
response(postcode),
|
50
|
+
response_type: :json
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def response(postcode)
|
57
|
+
case @host
|
58
|
+
when :Google
|
59
|
+
Google.new(postcode).request
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_options(opts)
|
66
|
+
@host = class_name(opts[:host]) || :Google
|
67
|
+
@response_type = opts[:response_type] || :json
|
68
|
+
end
|
69
|
+
|
70
|
+
def class_name(host)
|
71
|
+
return nil unless Module.constants.include? host
|
72
|
+
host.to_sym
|
73
|
+
end
|
74
|
+
|
75
|
+
def density_of(postcode, place_info_result)
|
76
|
+
place_info_result.merge density_info(postcode)
|
77
|
+
end
|
78
|
+
|
79
|
+
def density_info(postcode)
|
80
|
+
density_count(postcode) > 1 ? { density: density_count(postcode) } : {}
|
81
|
+
end
|
82
|
+
|
83
|
+
def density_count(postcode)
|
84
|
+
postcodes.count postcode
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
module ResponseBuilder
|
3
|
+
class Config
|
4
|
+
class << self
|
5
|
+
# Get all response fields (key_paths)
|
6
|
+
def fields
|
7
|
+
@@fields ||= default_fields
|
8
|
+
end
|
9
|
+
|
10
|
+
# add a custom key_path
|
11
|
+
def add_key_path(key_name, *path)
|
12
|
+
#TODO
|
13
|
+
end
|
14
|
+
|
15
|
+
# remove a key_path
|
16
|
+
#
|
17
|
+
# @return nothing if keypath doesn't exist
|
18
|
+
#
|
19
|
+
def remove_key_path(key_name)
|
20
|
+
#TODO
|
21
|
+
end
|
22
|
+
|
23
|
+
# default fields (key_paths)
|
24
|
+
def default_fields
|
25
|
+
{
|
26
|
+
format_address: ['formatted_address'],
|
27
|
+
lat: ['geometry', 'location', 'lat'],
|
28
|
+
long: ['geometry', 'location', 'lng'],
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
module ResponseBuilder
|
3
|
+
class JsonOutput < Builder
|
4
|
+
# Implement #data from Builder
|
5
|
+
#
|
6
|
+
# Combine all info fields in Config class to the result
|
7
|
+
def data
|
8
|
+
result = {}
|
9
|
+
|
10
|
+
Config.fields.each do |key, value|
|
11
|
+
result[key] = digg value
|
12
|
+
end
|
13
|
+
|
14
|
+
result
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get value from a key path
|
18
|
+
#
|
19
|
+
# @params: key_path - array[String|integer]
|
20
|
+
#
|
21
|
+
# @return value of a keypath
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# @raw_data =
|
25
|
+
# 'results' => [
|
26
|
+
# {
|
27
|
+
# 'address' => [
|
28
|
+
# {
|
29
|
+
# 'short_name' => '1'
|
30
|
+
# }
|
31
|
+
# ]
|
32
|
+
# }
|
33
|
+
# ]
|
34
|
+
#
|
35
|
+
# so the key_path will be
|
36
|
+
# keypath = ['address', 0, 'short_name']
|
37
|
+
#
|
38
|
+
# response = SgPostcode::ResponseBuilder::JsonOutput.new(@raw_data)
|
39
|
+
# response.data
|
40
|
+
#
|
41
|
+
def digg(key_path)
|
42
|
+
key_path.inject(@raw_data) do |result, key|
|
43
|
+
(result.respond_to?(:[]) && (result[key] || {})) || break
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module SgPostcode
|
4
|
+
class Response
|
5
|
+
def initialize(raw_data, response_type: :json)
|
6
|
+
@raw_data = JSON.parse(raw_data)
|
7
|
+
@response_type = response_type
|
8
|
+
end
|
9
|
+
|
10
|
+
# build the data hash
|
11
|
+
# may use Builder object to contruct the output data
|
12
|
+
#
|
13
|
+
def data
|
14
|
+
case @response_type
|
15
|
+
when :json
|
16
|
+
ResponseBuilder::JsonOutput.new(@raw_data).data
|
17
|
+
else
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SgPostcode
|
2
|
+
module ResponseBuilder
|
3
|
+
class Builder
|
4
|
+
attr_reader :raw_data
|
5
|
+
# Initialize a new builder
|
6
|
+
#
|
7
|
+
# All subclass of Builder will inherit this method
|
8
|
+
#
|
9
|
+
# raw_data must have the 'results', follow is an array
|
10
|
+
def initialize(raw_data)
|
11
|
+
# FIXME: check if the data is invalid
|
12
|
+
@raw_data = raw_data['results'].first rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# Implement this method in the subclass instead
|
16
|
+
def data
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module SgPostcode
|
4
|
+
class Google
|
5
|
+
# Request URL
|
6
|
+
REQUEST_SERVICE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'
|
7
|
+
|
8
|
+
def initialize(postcode)
|
9
|
+
@postcode = postcode
|
10
|
+
end
|
11
|
+
|
12
|
+
# Send Request with the postcode included as a params
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# sender = SgPostcode:Google.new('238432')
|
16
|
+
# sender.request
|
17
|
+
#
|
18
|
+
def request
|
19
|
+
RestClient.get REQUEST_SERVICE_URL, { params: { address: @postcode } }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestLongLatConverter < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@converter = SgPostcode::LongLatConverter.new(nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_send_request
|
9
|
+
@converter.place_info(nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_send_geo_request
|
13
|
+
result = @converter.send_geo_request("23000")
|
14
|
+
|
15
|
+
refute_equal nil, result
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|