Class: GeonamesClient::Service
- Inherits:
-
Object
- Object
- GeonamesClient::Service
- Includes:
- HTTParty
- Defined in:
- lib/geonames_client/service.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
- #bounding_box(options) ⇒ Object
- #check_response_for_error(response) ⇒ Object
- #default_radius ⇒ Object
- #get_nearby_streets(options) ⇒ Object
- #get_nearby_streets_within_radius(options) ⇒ Object
-
#initialize(api_key) ⇒ Service
constructor
A new instance of Service.
- #nearby_streets(options) ⇒ Object
- #service_definition ⇒ Object
Constructor Details
#initialize(api_key) ⇒ Service
Returns a new instance of Service.
17 18 19 |
# File 'lib/geonames_client/service.rb', line 17 def initialize( api_key ) @api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/geonames_client/service.rb', line 15 def api_key @api_key end |
Instance Method Details
#bounding_box(options) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/geonames_client/service.rb', line 89 def bounding_box( ) location = Location.new( [:latitude].to_f, [:longitude].to_f ) location.bounding_box_locations_as_hash( [:radius] || default_radius ) end |
#check_response_for_error(response) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/geonames_client/service.rb', line 54 def check_response_for_error( response ) geonames = response['geonames'] return unless geonames error = geonames['status'] unless error.nil? raise "Geonames returned error: (#{error['value']}) #{error['message']}" end end |
#default_radius ⇒ Object
96 97 98 |
# File 'lib/geonames_client/service.rb', line 96 def default_radius 402 end |
#get_nearby_streets(options) ⇒ Object
25 26 27 28 29 |
# File 'lib/geonames_client/service.rb', line 25 def get_nearby_streets( ) .merge!( :username => api_key ) NearbyStreet.all *nearby_streets( ).sort end |
#get_nearby_streets_within_radius(options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/geonames_client/service.rb', line 31 def get_nearby_streets_within_radius( ) .merge!( :username => api_key ) street_names = (nearby_streets( ) + nearby_streets_northeast( ) + nearby_streets_northwest( ) + nearby_streets_southeast( ) + nearby_streets_southwest( ) ).uniq NearbyStreet.all *street_names.sort end |
#nearby_streets(options) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/geonames_client/service.rb', line 44 def nearby_streets( ) uri = service_definition.full_nearby_streets_uri( ) response = self.class.get( uri ) check_response_for_error( response ) response.parsed_response['geonames']['streetSegment'].map { |s| s['name'] }.uniq.compact end |
#service_definition ⇒ Object
21 22 23 |
# File 'lib/geonames_client/service.rb', line 21 def service_definition @service_definition ||= ServiceDefinition.new end |