Class: SimpleGeo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_geo/client.rb

Constant Summary collapse

@@connection =
nil
@@debug =
false

Class Method Summary collapse

Class Method Details

.add_record(record) ⇒ Object

Raises:



24
25
26
27
# File 'lib/simple_geo/client.rb', line 24

def add_record(record)
  raise SimpleGeoError, "Record has no layer"  if record.layer.nil?
  put Endpoint.record(record.layer, record.id), record
end

.add_records(layer, records) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/simple_geo/client.rb', line 40

def add_records(layer, records)
  features = {
    :type => 'FeatureCollection',
    :features => records.collect { |record| record.to_hash }
  }
  post Endpoint.add_records(layer), features
end

.debugObject



20
21
22
# File 'lib/simple_geo/client.rb', line 20

def debug
  @@debug
end

.debug=(debug_flag) ⇒ Object



15
16
17
18
# File 'lib/simple_geo/client.rb', line 15

def debug=(debug_flag)
  @@debug = debug_flag
  @@connection.debug = @@debug  if @@connection
end

.delete(endpoint, data = nil) ⇒ Object



148
149
150
151
# File 'lib/simple_geo/client.rb', line 148

def delete(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  @@connection.delete endpoint, data
end

.delete_record(layer, id) ⇒ Object



29
30
31
# File 'lib/simple_geo/client.rb', line 29

def delete_record(layer, id)
  delete Endpoint.record(layer, id)
end

.get(endpoint, data = nil) ⇒ Object



143
144
145
146
# File 'lib/simple_geo/client.rb', line 143

def get(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  @@connection.get endpoint, data
end

.get_boundary(id) ⇒ Object

this API call seems to always return a 404



133
134
135
136
# File 'lib/simple_geo/client.rb', line 133

def get_boundary(id)
  info = get Endpoint.boundary(id)
  HashUtils.recursively_symbolize_keys(info)
end

.get_contains(lat, lon) ⇒ Object



138
139
140
141
# File 'lib/simple_geo/client.rb', line 138

def get_contains(lat, lon)
  info = get Endpoint.contains(lat, lon)
  HashUtils.recursively_symbolize_keys(info)
end

.get_density(lat, lon, day, hour = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/simple_geo/client.rb', line 112

def get_density(lat, lon, day, hour=nil)
  geojson_hash = get Endpoint.density(lat, lon, day, hour)
  geojson_hash = HashUtils.recursively_symbolize_keys(geojson_hash)
  if hour.nil?
    density_info = []
    geojson_hash[:features].each do |hour_geojson_hash|
      density_info << hour_geojson_hash[:properties].merge(
        {:geometry => hour_geojson_hash[:geometry]})
    end
    density_info
  else
    geojson_hash[:properties].merge({:geometry => geojson_hash[:geometry]})
  end
end

.get_history(layer, id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_geo/client.rb', line 60

def get_history(layer, id)
  history_geojson = get Endpoint.history(layer, id)
  history = []
  history_geojson['geometries'].each do |point|
    history << {
      :created => Time.at(point['created']),
      :lat => point['coordinates'][1],
      :lon => point['coordinates'][0]
    }
  end
  history
end

.get_layer_information(layer) ⇒ Object



106
107
108
109
110
# File 'lib/simple_geo/client.rb', line 106

def get_layer_information(layer)
  layer_info = get Endpoint.layer(layer)
  layer_info.delete('selfLink')
  HashUtils.symbolize_keys(layer_info)
end

.get_nearby_address(lat, lon) ⇒ Object



101
102
103
104
# File 'lib/simple_geo/client.rb', line 101

def get_nearby_address(lat, lon)
  geojson_hash = get Endpoint.nearby_address(lat, lon)
  HashUtils.symbolize_keys geojson_hash['properties']
end

.get_nearby_records(layer, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simple_geo/client.rb', line 73

def get_nearby_records(layer, options)
  if options[:geohash]
    endpoint = Endpoint.nearby_geohash(layer, options.delete(:geohash))
  elsif options[:lat] && options[:lon]
    endpoint = Endpoint.nearby_coordinates(layer,
      options.delete(:lat), options.delete(:lon))
  else
    raise SimpleGeoError, "Either geohash or lat and lon is required"
  end

  options = nil  if options.empty?
  features_hash = get(endpoint, options)
  nearby_records = {
    :next_cursor => features_hash['next_cursor'],
    :records => []
  }
  features_hash['features'].each do |feature_hash|
    record = Record.parse_geojson_hash(feature_hash)
    record.layer = layer
    record_info = {
      :distance => feature_hash['distance'],
      :record => record
    }
    nearby_records[:records] << record_info
  end
  nearby_records
end

.get_overlaps(south, west, north, east, options = nil) ⇒ Object



127
128
129
130
# File 'lib/simple_geo/client.rb', line 127

def get_overlaps(south, west, north, east, options=nil)
  info = get Endpoint.overlaps(south, west, north, east), options
  HashUtils.recursively_symbolize_keys(info)
end

.get_record(layer, id) ⇒ Object



33
34
35
36
37
38
# File 'lib/simple_geo/client.rb', line 33

def get_record(layer, id)
  record_hash = get Endpoint.record(layer, id)
  record = Record.parse_geojson_hash(record_hash)
  record.layer = layer
  record
end

.get_records(layer, ids) ⇒ Object

This request currently generates a 500 error if an unknown id is passed in.



49
50
51
52
53
54
55
56
57
58
# File 'lib/simple_geo/client.rb', line 49

def get_records(layer, ids)
  features_hash = get Endpoint.records(layer, ids)
  records = []
  features_hash['features'].each do |feature_hash|
    record = Record.parse_geojson_hash(feature_hash)
    record.layer = layer
    records << record
  end
  records
end

.post(endpoint, data = nil) ⇒ Object



153
154
155
156
# File 'lib/simple_geo/client.rb', line 153

def post(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  @@connection.post endpoint, data
end

.put(endpoint, data = nil) ⇒ Object



158
159
160
161
# File 'lib/simple_geo/client.rb', line 158

def put(endpoint, data=nil)
  raise NoConnectionEstablished  if @@connection.nil?
  @@connection.put endpoint, data
end

.set_credentials(token, secret) ⇒ Object



10
11
12
13
# File 'lib/simple_geo/client.rb', line 10

def set_credentials(token, secret)
  @@connection = Connection.new(token, secret)
  @@connection.debug = @@debug
end