location-one 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,15 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- location-one (0.0.5)
4
+ location-one (0.0.6)
5
5
  geocoder (~> 1.1)
6
+ httpclient
6
7
  json
7
8
 
8
9
  GEM
9
10
  remote: http://rubygems.org/
10
11
  specs:
11
- geocoder (1.1.2)
12
- json (1.7.3)
12
+ geocoder (1.1.3)
13
+ httpclient (2.2.7)
14
+ json (1.7.5)
13
15
 
14
16
  PLATFORMS
15
17
  ruby
@@ -1,6 +1,6 @@
1
1
  require 'geocoder'
2
2
  require 'json'
3
- require 'net/http'
3
+ require 'httpclient'
4
4
 
5
5
  module LocationOne
6
6
  #SUPPORTED_BACKENDS =
@@ -14,47 +14,45 @@ module LocationOne
14
14
  class Client
15
15
  attr_accessor :http, :backend
16
16
 
17
- def initialize(backend,opt_client=nil)
17
+ def initialize(backend, opt_client=nil)
18
18
  @backend = backend
19
- @http = opt_client || Net::HTTP.new(backend[:host], backend[:port])
19
+ @http = opt_client
20
20
  end
21
21
 
22
22
  def change_location(options, opt_data={})
23
-
24
23
  if (options[:latitude] and not options[:longitude]) or
25
24
  (options[:longitude] and not options[:latitude])
26
25
  raise "Both latitude and longitude must be specified if either is."
27
26
  end
28
27
  if (options[:latitude])
29
- change_location_by_coords(options[:latitude], options[:longitude],opt_data)
28
+ change_location_by_coords(options[:latitude], options[:longitude], opt_data)
30
29
  else
31
30
  if not options[:place]
32
31
  raise "Either :place or :latitude and :longitude must be specified."
33
32
  end
34
- change_location_by_place(options[:place],opt_data)
33
+ change_location_by_place(options[:place], opt_data)
35
34
  end
36
35
  end
37
36
 
38
- def change_location_by_coords(lat, lon,opt_data={})
39
- req = Net::HTTP::Post.new(backend[:path])
37
+ def change_location_by_coords(lat, lon, opt_data={})
40
38
 
41
39
  body_data = {:action => :change_location,
42
40
  :latitude => lat,
43
41
  :longitude => lon}.merge(opt_data)
44
42
 
45
- req.body = body_data.to_json
46
43
 
47
- res = @http.request(req)
48
44
 
49
- begin
50
- @http.finish if @http.started?
51
- rescue
45
+ body = make_http_request(
46
+ :uri => URI.parse("http://#{@backend[:host]}:#{@backend[:port]}#{@backend[:path]}"),
47
+ :method => :post,
48
+ :body => body_data.to_json
49
+ )
52
50
 
51
+
52
+ unless body
53
+ raise "Set location change failed, for #{lat}, #{lon} (#{body})."
53
54
  end
54
- if res.code !='200'
55
- raise "Response error code #{res.code}, for #{lat}, #{lon} (#{res.body})."
56
- end
57
- res.body
55
+ body
58
56
  end
59
57
 
60
58
  def self.location_by_place(place)
@@ -63,9 +61,65 @@ module LocationOne
63
61
  results.first
64
62
  end
65
63
 
66
- def change_location_by_place(place,opt_data={})
64
+ def change_location_by_place(place, opt_data={})
67
65
  best_result = Client.location_by_place(place)
68
- change_location_by_coords(best_result.latitude, best_result.longitude,opt_data)
66
+ change_location_by_coords(best_result.latitude, best_result.longitude, opt_data)
67
+ end
68
+
69
+ def make_http_request(options)
70
+ body = nil
71
+ 3.times do |count|
72
+ begin
73
+ if not @http
74
+ @http = init_request(options)
75
+ end
76
+ if options[:method] == :post
77
+ body = @http.post(options[:uri], options[:body]).body
78
+ else
79
+ body = @http.get(options[:uri], options[:body]).body
80
+ end
81
+ break
82
+ rescue HTTPClient::TimeoutError => e
83
+ if count < 2
84
+ sleep(0.5)
85
+ @http.reset_all
86
+ @http=nil
87
+ STDOUT.write "Retrying.. #{e.class}: (#{e})\n"
88
+ STDOUT.flush
89
+
90
+ else
91
+ puts "Failing... #{e.class}"
92
+ raise e
93
+ end
94
+ rescue Exception => e
95
+ case e
96
+ when Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ETIMEDOUT
97
+ if count < 2
98
+ sleep(0.5)
99
+ @http.reset_all
100
+ @http=nil
101
+ STDOUT.write "Retrying.. #{e.class}: (#{e})\n"
102
+ STDOUT.flush
103
+
104
+ else
105
+ puts "Failing... #{e.class}"
106
+ raise e
107
+ end
108
+ else
109
+ raise e
110
+ end
111
+ end
112
+ end
113
+
114
+ body
115
+ end
116
+
117
+ def init_request(url)
118
+ http = HTTPClient.new
119
+ http.connect_timeout = 15
120
+ http.send_timeout = 15
121
+ http.receive_timeout = 15
122
+ http
69
123
  end
70
124
 
71
125
  end
@@ -1,3 +1,3 @@
1
1
  module LocationOne
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/location-one.gemspec CHANGED
@@ -17,5 +17,6 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.add_dependency( "geocoder","~>1.1")
19
19
  s.add_dependency( "json" )
20
+ s.add_dependency( "httpclient" )
20
21
 
21
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: location-one
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: geocoder
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httpclient
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description: Location Simulation Client for Calabash and Frank backends.
47
63
  email:
48
64
  - karl@lesspainful.com