twofishes 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff205c55a7da5aebe1b1f1c1674a5eeb52dfb596
4
+ data.tar.gz: 21b4f4688a41eebbb0c0f4d101ddd6a69a4aaa1b
5
+ SHA512:
6
+ metadata.gz: 93ecbf4f575a1103a09d37d68b4688f57fa259da7c22136fe53f711a7883a789e5ee72592c6b93c987177c7ef84980774245204fde9fa294fb6aae65cc8ed836
7
+ data.tar.gz: 2bc70e57bb6d77811703de931dbc5e10e564173076293bd368d8d5a5508796f5fcd1b81e3d423e250eb2952a70477501eddaf554b77ea298ca29708b4872b7fa
@@ -0,0 +1,53 @@
1
+ require 'httparty'
2
+
3
+ module Twofishes
4
+ class Client
5
+ # @see https://github.com/foursquare/twofishes/blob/master/docs/twofishes_requests.md
6
+
7
+ include HTTParty
8
+ # debug_output $stderr # httparty debugging
9
+
10
+ # Geocodes a given string.
11
+ #
12
+ # @param [String] query
13
+ # @return [Twofishes::Result]
14
+ #
15
+ # @example
16
+ # Twofishes::Client.geocode('Zurich, Switzerland')
17
+ #
18
+ def self.geocode(location)
19
+ call_api(query: location)
20
+ end
21
+
22
+ # Reverse geocodes lat/lng.
23
+ #
24
+ # @param [Float] lat
25
+ # @param [Float] lng
26
+ # @return [Twofishes::Result]
27
+ #
28
+ # @example
29
+ # Twofishes::Client.reverse_geocode(47.3787733, 8.5273363)
30
+ #
31
+ def self.reverse_geocode(coordinates)
32
+ call_api(ll: coordinates.join(','))
33
+ end
34
+
35
+ def self.call_api(params)
36
+ handle_response do
37
+ get(Twofishes.configuration.base_url, query: params)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ 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
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ module Twofishes
2
+
3
+ class Configuration
4
+
5
+ # @see https://github.com/thoughtbot/clearance/blob/master/lib/clearance/configuration.rb
6
+
7
+ attr_accessor :base_url
8
+
9
+ def initialize
10
+ @base_url = 'http://localhost:8081'
11
+ end
12
+
13
+ end
14
+
15
+ def self.configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield configuration
21
+ end
22
+
23
+ def self.reset_configuration
24
+ @configuration = nil
25
+ end
26
+
27
+ end
@@ -0,0 +1,6 @@
1
+ module Twofishes
2
+
3
+ class InvalidResponseError < StandardError
4
+ end
5
+
6
+ end
@@ -0,0 +1,56 @@
1
+ require 'rash'
2
+
3
+ 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
37
+ end
38
+
39
+ def coordinates
40
+ [lat, lng]
41
+ end
42
+
43
+ def parents
44
+ @data.parents.map { |parent| Twofishes::Result.new(parent) }
45
+ end
46
+
47
+ def method_missing(method_sym, *arguments, &block)
48
+ @data.send(method_sym)
49
+ end
50
+
51
+ def respond_to?(method_sym, include_private=false)
52
+ @data.respond_to?(method_sym) ? true : super
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Twofishes
2
+ VERSION = "1.0.0.rc1"
3
+ end
data/lib/twofishes.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "twofishes/version"
2
+ require "twofishes/configuration"
3
+ require "twofishes/errors"
4
+ require "twofishes/result"
5
+ require "twofishes/client"
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twofishes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Christian Felder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.12.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.12.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rash
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeweb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codeclimate-test-reporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Client for foursquare's Twofishes API, a sparse reverse geocoder.
140
+ email:
141
+ - ema@rh-productions.ch
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - lib/twofishes/client.rb
147
+ - lib/twofishes/configuration.rb
148
+ - lib/twofishes/errors.rb
149
+ - lib/twofishes/result.rb
150
+ - lib/twofishes/version.rb
151
+ - lib/twofishes.rb
152
+ homepage: https://github.com/masone/twofishes-ruby
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - '>'
168
+ - !ruby/object:Gem::Version
169
+ version: 1.3.1
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.0.3
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Client for foursquare's Twofishes API, a sparse reverse geocoder.
176
+ test_files: []
177
+ has_rdoc: