Class: GeonamesClient::Service

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/geonames_client/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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( options )
  location = Location.new( options[:latitude].to_f,
                           options[:longitude].to_f )

  location.bounding_box_locations_as_hash( options[: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_radiusObject



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( options )
  options.merge!( :username => api_key )

  NearbyStreet.all *nearby_streets( options ).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( options )
  options.merge!( :username => api_key )

  street_names = (nearby_streets( options ) +
                  nearby_streets_northeast( options ) +
                  nearby_streets_northwest( options ) +
                  nearby_streets_southeast( options ) +
                  nearby_streets_southwest( options ) ).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( options )
  uri = service_definition.full_nearby_streets_uri( options )

  response = self.class.get( uri )

  check_response_for_error( response )

  response.parsed_response['geonames']['streetSegment'].map { |s| s['name'] }.uniq.compact
end

#service_definitionObject



21
22
23
# File 'lib/geonames_client/service.rb', line 21

def service_definition
  @service_definition ||= ServiceDefinition.new
end