Class: LocationOne::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/location-one/core.rb

Overview

SUPPORTED_BACKENDS =

{
    :calabashios => {:path => "/uia"},
    :calabashandroid => {:path => "/uia"},
    :frank => {:path => "/tbd"},

}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, opt_client = nil) ⇒ Client



17
18
19
20
# File 'lib/location-one/core.rb', line 17

def initialize(backend, opt_client=nil)
  @backend = backend
  @http = opt_client
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



15
16
17
# File 'lib/location-one/core.rb', line 15

def backend
  @backend
end

#httpObject

Returns the value of attribute http.



15
16
17
# File 'lib/location-one/core.rb', line 15

def http
  @http
end

Class Method Details

.location_by_place(place) ⇒ Object



58
59
60
61
62
# File 'lib/location-one/core.rb', line 58

def self.location_by_place(place)
  results = Geocoder.search(place)
  raise "Got no results for #{place}" if results.empty?
  results.first
end

Instance Method Details

#change_location(options, opt_data = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/location-one/core.rb', line 22

def change_location(options, opt_data={})
  if (options[:latitude] and not options[:longitude]) or
      (options[:longitude] and not options[:latitude])
    raise "Both latitude and longitude must be specified if either is."
  end
  if (options[:latitude])
    change_location_by_coords(options[:latitude], options[:longitude], opt_data)
  else
    if not options[:place]
      raise "Either :place or :latitude and :longitude must be specified."
    end
    change_location_by_place(options[:place], opt_data)
  end
end

#change_location_by_coords(lat, lon, opt_data = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/location-one/core.rb', line 37

def change_location_by_coords(lat, lon, opt_data={})

  body_data = {:action => :change_location,
               :latitude => lat,
               :longitude => lon}.merge(opt_data)



  body = make_http_request(
      :uri => URI.parse("http://#{@backend[:host]}:#{@backend[:port]}#{@backend[:path]}"),
      :method => :post,
      :body => body_data.to_json
  )


  unless body
    raise "Set location change failed, for #{lat}, #{lon} (#{body})."
  end
  body
end

#change_location_by_place(place, opt_data = {}) ⇒ Object



64
65
66
67
# File 'lib/location-one/core.rb', line 64

def change_location_by_place(place, opt_data={})
  best_result = Client.location_by_place(place)
  change_location_by_coords(best_result.latitude, best_result.longitude, opt_data)
end

#init_request(url) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/location-one/core.rb', line 117

def init_request(url)
  http = HTTPClient.new
  http.connect_timeout = 15
  http.send_timeout = 15
  http.receive_timeout = 15
  http
end

#make_http_request(options) ⇒ Object



69
70
71
72
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/location-one/core.rb', line 69

def make_http_request(options)
  body = nil
  3.times do |count|
    begin
      if not @http
        @http = init_request(options)
      end
      if options[:method] == :post
        body = @http.post(options[:uri], options[:body]).body
      else
        body = @http.get(options[:uri], options[:body]).body
      end
      break
    rescue HTTPClient::TimeoutError => e
      if count < 2
        sleep(0.5)
        @http.reset_all
        @http=nil
        STDOUT.write "Retrying.. #{e.class}: (#{e})\n"
        STDOUT.flush

      else
        puts "Failing... #{e.class}"
        raise e
      end
    rescue Exception => e
      case e
        when Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ETIMEDOUT
          if count < 2
            sleep(0.5)
            @http.reset_all
            @http=nil
            STDOUT.write "Retrying.. #{e.class}: (#{e})\n"
            STDOUT.flush

          else
            puts "Failing... #{e.class}"
            raise e
          end
        else
          raise e
      end
    end
  end

  body
end