twofishes 1.0.1 → 2.0.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/twofishes.rb CHANGED
@@ -1,5 +1,9 @@
1
- require "twofishes/version"
2
- require "twofishes/configuration"
3
- require "twofishes/errors"
4
- require "twofishes/result"
5
- require "twofishes/client"
1
+ require 'twofishes/version'
2
+ require 'twofishes/configuration'
3
+ require 'thrift_client'
4
+ require 'thrift/geocoder_types'
5
+ require 'thrift/geocoder'
6
+ require 'twofishes/errors'
7
+ require 'twofishes/result'
8
+ require 'twofishes/underscored'
9
+ require 'twofishes/client'
@@ -1,53 +1,53 @@
1
- require 'httparty'
2
-
3
1
  module Twofishes
4
2
  class Client
5
3
  # @see https://github.com/foursquare/twofishes/blob/master/docs/twofishes_requests.md
6
4
 
7
- include HTTParty
8
- # debug_output $stderr # httparty debugging
9
-
10
5
  # Geocodes a given string.
11
6
  #
12
- # @param [String] query
7
+ # @param [String] location
8
+ # @param [Array] list of additional ResponseIncludes constants
13
9
  # @return [Twofishes::Result]
14
10
  #
15
11
  # @example
16
- # Twofishes::Client.geocode('Zurich, Switzerland')
12
+ # Twofishes::Client.geocode('Zurich, Switzerland')
17
13
  #
18
- def self.geocode(location)
19
- call_api(query: location)
14
+ def self.geocode(location, includes: [])
15
+ handle_response do
16
+ request = GeocodeRequest.new(query: location, responseIncludes: includes)
17
+ thrift_client.geocode(request)
18
+ end
20
19
  end
21
20
 
22
21
  # Reverse geocodes lat/lng.
23
22
  #
24
- # @param [Float] lat
25
- # @param [Float] lng
23
+ # @param [Array] latitude, longitude pair
26
24
  # @return [Twofishes::Result]
27
- #
28
25
  # @example
29
- # Twofishes::Client.reverse_geocode(47.3787733, 8.5273363)
26
+ # Twofishes::Client.reverse_geocode([47.3787733, 8.5273363])
30
27
  #
31
- def self.reverse_geocode(coordinates)
32
- call_api(ll: coordinates.join(','))
33
- end
34
-
35
- def self.call_api(params)
28
+ def self.reverse_geocode(coordinates, includes: [])
36
29
  handle_response do
37
- get(Twofishes.configuration.base_url, query: params, timeout: Twofishes.configuration.timeout)
30
+ point = GeocodePoint.new(lat: coordinates[0], lng: coordinates[1])
31
+ request = GeocodeRequest.new(ll: point, responseIncludes: includes)
32
+ thrift_client.reverseGeocode(request)
38
33
  end
39
34
  end
40
35
 
36
+ def self.thrift_client
37
+ @thrift_client ||= ThriftClient.new(
38
+ Geocoder::Client,
39
+ Twofishes.configuration.address,
40
+ retries: Twofishes.configuration.retries,
41
+ timeout: Twofishes.configuration.timeout
42
+ )
43
+ end
44
+
41
45
  private
42
46
 
43
47
  def self.handle_response
44
- response = yield
45
- if response.code == 200
46
- Result.from_response(response)
47
- else
48
- raise Twofishes::InvalidResponseError, response.to_s.lines.first
49
- end
48
+ Result.from_response(yield)
49
+ rescue => e
50
+ raise Twofishes::InvalidResponseError, e.message
50
51
  end
51
-
52
52
  end
53
53
  end
@@ -1,16 +1,19 @@
1
1
  module Twofishes
2
-
3
2
  class Configuration
4
-
5
3
  # @see https://github.com/thoughtbot/clearance/blob/master/lib/clearance/configuration.rb
6
4
 
7
- attr_accessor :base_url, :timeout
5
+ attr_accessor :host, :port, :timeout, :retries
8
6
 
9
7
  def initialize
10
- @base_url = 'http://localhost:8081'
8
+ @host = '127.0.0.1'
9
+ @port = 8080
11
10
  @timeout = 3
11
+ @retries = 2
12
12
  end
13
13
 
14
+ def address
15
+ "#{host}:#{port}"
16
+ end
14
17
  end
15
18
 
16
19
  def self.configuration
@@ -24,5 +27,4 @@ module Twofishes
24
27
  def self.reset_configuration
25
28
  @configuration = nil
26
29
  end
27
-
28
30
  end
@@ -1,6 +1,4 @@
1
1
  module Twofishes
2
-
3
2
  class InvalidResponseError < StandardError
4
3
  end
5
-
6
4
  end
@@ -1,56 +1,32 @@
1
- require 'rash'
2
-
1
+ require 'delegate'
3
2
  module Twofishes
4
-
5
- class Result
6
-
7
- def initialize(hash)
8
- @data = Hashie::Rash.new(hash)
9
- end
10
-
11
- def self.from_response(hash)
12
- if hash['interpretations']
13
- hash['interpretations'].map { |interpretation| self.new(interpretation) }
14
- else
15
- []
16
- end
17
- end
18
-
19
- def name
20
- feature.name
21
- end
22
-
23
- def display_name
24
- feature.display_name
25
- end
26
-
27
- def country_code
28
- feature.cc
29
- end
30
-
31
- def lat
32
- feature.geometry.center.lat
33
- end
34
-
35
- def lng
36
- feature.geometry.center.lng
3
+ class Result < SimpleDelegator
4
+ def self.from_response(response)
5
+ response.interpretations.map { |interpretation|
6
+ new(interpretation)
7
+ }
37
8
  end
9
+ end
10
+ end
38
11
 
39
- def coordinates
40
- [lat, lng]
41
- end
12
+ require 'forwardable'
13
+ class GeocodeInterpretation
14
+ extend Forwardable
15
+ def_delegators :feature, *GeocodeFeature::FIELDS.map { |_, v| v[:name] }
42
16
 
43
- def parents
44
- @data.parents.map { |parent| Twofishes::Result.new(parent) }
45
- end
17
+ def country_code
18
+ cc
19
+ end
46
20
 
47
- def method_missing(method_sym, *arguments, &block)
48
- @data.send(method_sym)
49
- end
21
+ def lat
22
+ geometry.center.lat
23
+ end
50
24
 
51
- def respond_to?(method_sym, include_private=false)
52
- @data.respond_to?(method_sym) ? true : super
53
- end
25
+ def lng
26
+ geometry.center.lng
27
+ end
54
28
 
29
+ def coordinates
30
+ [lat, lng]
55
31
  end
56
- end
32
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Underscored
4
+ def self.included(base)
5
+ base::FIELDS.map { |_, v|
6
+ define_method(v[:name].underscore) { send(v[:name]) }
7
+ }
8
+ end
9
+ end
10
+
11
+ class GeocodeInterpretation
12
+ include Underscored
13
+ end
14
+
15
+ class GeocodeFeature
16
+ include Underscored
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Twofishes
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0-rc.1"
3
3
  end
metadata CHANGED
@@ -1,57 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twofishes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0.pre.rc.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Felder
8
+ - Miha Rekar
9
+ - Bartek Bułat
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
13
+ date: 2015-02-19 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
- name: httparty
16
+ name: activesupport
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
19
  - - '>='
18
20
  - !ruby/object:Gem::Version
19
- version: 0.12.0
21
+ version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
23
25
  requirements:
24
26
  - - '>='
25
27
  - !ruby/object:Gem::Version
26
- version: 0.12.0
28
+ version: '0'
27
29
  - !ruby/object:Gem::Dependency
28
- name: rash
30
+ name: thrift_client
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
33
  - - '>='
32
34
  - !ruby/object:Gem::Version
33
- version: 0.4.0
35
+ version: '0'
34
36
  type: :runtime
35
37
  prerelease: false
36
38
  version_requirements: !ruby/object:Gem::Requirement
37
39
  requirements:
38
40
  - - '>='
39
41
  - !ruby/object:Gem::Version
40
- version: 0.4.0
42
+ version: '0'
41
43
  - !ruby/object:Gem::Dependency
42
44
  name: bundler
43
45
  requirement: !ruby/object:Gem::Requirement
44
46
  requirements:
45
- - - ~>
47
+ - - '>='
46
48
  - !ruby/object:Gem::Version
47
- version: '1.5'
49
+ version: '0'
48
50
  type: :development
49
51
  prerelease: false
50
52
  version_requirements: !ruby/object:Gem::Requirement
51
53
  requirements:
52
- - - ~>
54
+ - - '>='
53
55
  - !ruby/object:Gem::Version
54
- version: '1.5'
56
+ version: '0'
55
57
  - !ruby/object:Gem::Dependency
56
58
  name: rake
57
59
  requirement: !ruby/object:Gem::Requirement
@@ -81,7 +83,7 @@ dependencies:
81
83
  - !ruby/object:Gem::Version
82
84
  version: '0'
83
85
  - !ruby/object:Gem::Dependency
84
- name: fakeweb
86
+ name: mocha
85
87
  requirement: !ruby/object:Gem::Requirement
86
88
  requirements:
87
89
  - - '>='
@@ -95,7 +97,7 @@ dependencies:
95
97
  - !ruby/object:Gem::Version
96
98
  version: '0'
97
99
  - !ruby/object:Gem::Dependency
98
- name: mocha
100
+ name: yard
99
101
  requirement: !ruby/object:Gem::Requirement
100
102
  requirements:
101
103
  - - '>='
@@ -109,7 +111,7 @@ dependencies:
109
111
  - !ruby/object:Gem::Version
110
112
  version: '0'
111
113
  - !ruby/object:Gem::Dependency
112
- name: yard
114
+ name: codeclimate-test-reporter
113
115
  requirement: !ruby/object:Gem::Requirement
114
116
  requirements:
115
117
  - - '>='
@@ -123,7 +125,7 @@ dependencies:
123
125
  - !ruby/object:Gem::Version
124
126
  version: '0'
125
127
  - !ruby/object:Gem::Dependency
126
- name: codeclimate-test-reporter
128
+ name: pry
127
129
  requirement: !ruby/object:Gem::Requirement
128
130
  requirements:
129
131
  - - '>='
@@ -143,11 +145,14 @@ executables: []
143
145
  extensions: []
144
146
  extra_rdoc_files: []
145
147
  files:
148
+ - lib/thrift/geocoder.rb
149
+ - lib/thrift/geocoder_types.rb
146
150
  - lib/twofishes.rb
147
151
  - lib/twofishes/client.rb
148
152
  - lib/twofishes/configuration.rb
149
153
  - lib/twofishes/errors.rb
150
154
  - lib/twofishes/result.rb
155
+ - lib/twofishes/underscored.rb
151
156
  - lib/twofishes/version.rb
152
157
  homepage: https://github.com/masone/twofishes-ruby
153
158
  licenses:
@@ -164,12 +169,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
169
  version: '2.0'
165
170
  required_rubygems_version: !ruby/object:Gem::Requirement
166
171
  requirements:
167
- - - '>='
172
+ - - '>'
168
173
  - !ruby/object:Gem::Version
169
- version: '0'
174
+ version: 1.3.1
170
175
  requirements: []
171
176
  rubyforge_project:
172
- rubygems_version: 2.4.3
177
+ rubygems_version: 2.4.5
173
178
  signing_key:
174
179
  specification_version: 4
175
180
  summary: Client for foursquare's Twofishes API, a sparse reverse geocoder.