Class: SgPostcode::LongLatConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/sg_postcode/converters/long_lat_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postcodes, opts = {}) ⇒ LongLatConverter

Returns a new instance of LongLatConverter.



5
6
7
8
# File 'lib/sg_postcode/converters/long_lat_converter.rb', line 5

def initialize(postcodes, opts = {})
  @postcodes = postcodes
  convert_options opts
end

Instance Attribute Details

#postcodesObject (readonly)

Returns the value of attribute postcodes.



3
4
5
# File 'lib/sg_postcode/converters/long_lat_converter.rb', line 3

def postcodes
  @postcodes
end

Instance Method Details

#convertObject

Convert an array of SG Postcode

Examples:

postcodes = ['238432', '247964']
SgPostCode::LongLatConverter.new(postcodes).convert

Returns:

  • an array contains long, lat



18
19
20
21
22
# 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) ⇒ Object

Request info from host for a postcode

@params: postcode number [String]

Returns:

  • hash of info, check response/config.rb to see the info fields



31
32
33
# 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) ⇒ Object

Send request to host, and return the response

SgPostcode::LongLatConverter.send_geo_request(“230000”)

Returns:



45
46
47
48
49
50
# File 'lib/sg_postcode/converters/long_lat_converter.rb', line 45

def send_geo_request(postcode)
  Response.new(
    response(postcode),
    response_type: :json
  )
end