diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index 5ccae10227fcd99931c49b924adad5213b1e5c7b..b80fb4f2f8cd8a7e39d1738bec1f024aa9909831 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -43,6 +43,7 @@ module Geocoder :pickpoint, :here, :baidu, + :tencent, :geocodio, :smarty_streets, :postcode_anywhere_uk, diff --git a/lib/geocoder/lookups/tencent.rb b/lib/geocoder/lookups/tencent.rb new file mode 100644 index 0000000000000000000000000000000000000000..2dade518726bbaf6a19e5d8bcd0bee17fb85c878 --- /dev/null +++ b/lib/geocoder/lookups/tencent.rb @@ -0,0 +1,56 @@ +require 'geocoder/lookups/base' +require "geocoder/results/tencent" + +module Geocoder::Lookup + class Tencent < Base + + def name + "Tencent" + end + + def required_api_key_parts + ["key"] + end + + def supported_protocols + [:https] + end + + private # --------------------------------------------------------------- + + def base_query_url(query) + "#{protocol}://apis.map.qq.com/ws/geocoder/v1/?" + end + + def content_key + 'result' + end + + def results(query, reverse = false) + return [] unless doc = fetch_data(query) + case doc['status'] + when 0 + return [doc[content_key]] + when 311 + raise_error(Geocoder::RequestDenied, "request denied") || + Geocoder.log(:warn, "#{name} Geocoding API error: request denied.") + when 310, 306 + raise_error(Geocoder::InvalidRequest, "invalid request.") || + Geocoder.log(:warn, "#{name} Geocoding API error: invalid request.") + when 311 + raise_error(Geocoder::InvalidApiKey, "invalid api key") || + Geocoder.log(:warn, "#{name} Geocoding API error: invalid api key.") + end + return [] + end + + def query_url_params(query) + { + (query.reverse_geocode? ? :location : :address) => query.sanitized_text, + :key => configuration.api_key, + :output => "json" + }.merge(super) + end + + end +end diff --git a/lib/geocoder/results/tencent.rb b/lib/geocoder/results/tencent.rb new file mode 100644 index 0000000000000000000000000000000000000000..d546bad6a78d95e16df93dffdcf5709619c0cdcd --- /dev/null +++ b/lib/geocoder/results/tencent.rb @@ -0,0 +1,73 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class Tencent < Base + + def coordinates + ['lat', 'lng'].map{ |i| @data['location'][i] } + end + + def address + "#{province}#{city}#{district}#{street}#{street_number}" + + #@data['title'] or @data['address'] + end + + # The Tencent reverse reverse geocoding API has the field named + # 'address_component' compared to 'address_components' in the + # regular geocoding API. + + def province + @data['address_components'] and (@data['address_components']['province']) or + (@data['address_component'] and @data['address_component']['province']) or + "" + end + + alias_method :state, :province + + def city + @data['address_components'] and (@data['address_components']['city']) or + (@data['address_component'] and @data['address_component']['city']) or + "" + end + + def district + @data['address_components'] and (@data['address_components']['district']) or + (@data['address_component'] and @data['address_component']['district']) or + "" + end + + def street + @data['address_components'] and (@data['address_components']['street']) or + (@data['address_component'] and @data['address_component']['street']) or + "" + end + + def street_number + @data['address_components'] and (@data['address_components']['street_number']) or + (@data['address_component'] and @data['address_component']['street_number']) or + "" + end + + def address_components + @data['address_components'] or @data['address_component'] + end + + def state_code + "" + end + + def postal_code + "" + end + + def country + "China" + end + + def country_code + "CN" + end + + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index d6243a919ea11ef14733539fc0c1a3148aafff04..a760407b5a805e2936180e7ad938d9b66a1cfcaf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -318,6 +318,14 @@ module Geocoder end end + require 'geocoder/lookups/tencent' + class Tencent + private + def default_fixture_filename + "tencent_shanghai_pearl_tower" + end + end + require 'geocoder/lookups/geocodio' class Geocodio private