class SgPostcode::LongLatConverter

Attributes

postcodes[R]

Public Class Methods

new(postcodes, opts = {}) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 5
def initialize(postcodes, opts = {})
  @postcodes = postcodes
  convert_options opts
end

Public Instance Methods

convert() click to toggle source

Convert an array of SG Postcode

@return an array contains long, lat

@example

postcodes = ['238432', '247964']
SgPostCode::LongLatConverter.new(postcodes).convert
# File lib/sg_postcode/converters/long_lat_converter.rb, line 18
def convert
  postcodes
    .uniq
    .map { |postcode| density_of(postcode, place_info(postcode)) }
end
place_info(postcode) click to toggle source

Request info from host for a postcode

@return hash of info, check

response/config.rb to see the info fields

@params: postcode number [String]

# File lib/sg_postcode/converters/long_lat_converter.rb, line 31
def place_info(postcode)
  send_geo_request(postcode).data
end
send_geo_request(postcode) click to toggle source

Send request to host, and return the response

@return

@params

postcode

@example SgPostcode::LongLatConverter.send_geo_request(“230000”)

# File lib/sg_postcode/converters/long_lat_converter.rb, line 45
def send_geo_request(postcode)
  Response.new(
    response(postcode),
    response_type: :json
  )
end

Private Instance Methods

class_name(host) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 64
def class_name(host)
  return nil unless Module.constants.include? host
  host.to_sym
end
convert_options(opts) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 58
def convert_options(opts)
  @host = class_name(opts[:host]) || :Google
  @response_type = opts[:response_type] || :json
  @cache = opts[:cache].nil? ? true : opts[:cache]
end
density_count(postcode) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 77
def density_count(postcode)
  postcodes.count postcode
end
density_info(postcode) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 73
def density_info(postcode)
  density_count(postcode) > 1 ? { density: density_count(postcode) } : {}
end
density_of(postcode, place_info_result) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 69
def density_of(postcode, place_info_result)
  place_info_result.merge density_info(postcode)
end
response(postcode) click to toggle source
# File lib/sg_postcode/converters/long_lat_converter.rb, line 54
def response(postcode)
  Proxy.new(@host, postcode, cache: @cache).request
end