Class: Hoccer::GeoStoreClient

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

Instance Method Summary collapse

Constructor Details

#initializeGeoStoreClient

Returns a new instance of GeoStoreClient.



8
9
10
11
# File 'lib/geo_store_client.rb', line 8

def initialize
  @server = "geostore.beta.hoccer.com"
  @port   = 80
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/geo_store_client.rb', line 82

def method_missing name, *args
  if %w(get post put delete).include? name.to_s
    request name, *args
  else
    super
  end
end

Instance Method Details

#prepare(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geo_store_client.rb', line 22

def prepare options
  validate options, :longitude, Float
  validate options, :latitude,  Float
  validate options, :params,    Hash

  {
    :environment => {
      :gps => {
        :longitude  => options[:longitude],
        :latitude   => options[:latitude],
        :accuracy   => options[:accuracy] || 5
      }
    },
    :params   => options[:params],
    :lifetime => options[:liftime]
  }
end

#remove(uuid) ⇒ Object



45
46
47
# File 'lib/geo_store_client.rb', line 45

def remove uuid
  delete "/store/#{uuid}"
end

#request(method, path, data = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geo_store_client.rb', line 69

def request method, path, data = {}
  if method == :delete
    Net::HTTP.start(@server, @port) do |http|
      req = Net::HTTP::Delete.new(path)
      response = http.request(req)
    end
  else
    Net::HTTP.start(@server, @port) do |http|
      http.send( "request_#{method}".to_sym, path, data.to_json)
    end
  end
end

#search_within_radius(options) ⇒ Object



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

def search_within_radius options
  validate options, :longitude, Float
  validate options, :latitude,  Float
  validate options, :radius,    Float

  options[:accuracy] = options.delete(:radius)

  post "/query", { :gps => options }
end

#search_within_region(options) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/geo_store_client.rb', line 59

def search_within_region options
  validate options, :region,    Array

  unless options[:region].all? {|x| x.is_a? Array}
    raise ArgumentError, "Region must be of format [[lon1,lat1], [lon2,lat2]]"
  end

  post "/query", { :box => options[:region] }
end

#store(options) ⇒ Object



40
41
42
43
# File 'lib/geo_store_client.rb', line 40

def store options
  data = prepare( options )
  post "/store", data
end

#validate(options, key, class_name) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/geo_store_client.rb', line 13

def validate options, key, class_name
  unless options[key] && options[key].is_a?( class_name )
    raise(
      ArgumentError,
      "#{key.capitalize} is missing or not a #{class_name.to_s}"
    )
  end
end