opencage-geocoder 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/opencage.rb +2 -0
- data/lib/opencage/geocoder.rb +2 -2
- data/lib/opencage/geocoder/location.rb +65 -0
- data/lib/opencage/version.rb +3 -0
- metadata +11 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb44eb7c577cea9176bf884e83a94160c8068f12
|
4
|
+
data.tar.gz: 3918ebe495c93729197ccad7d6eee5f8451a74c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b838c50be09b64555f86e365ed03337e3ec2a1017af020f0fd4dc6877daeb765dbb8e60b1ae42255b356e4bc8ea7f61114c931561974d1fe61525b95fbafee4
|
7
|
+
data.tar.gz: d9d5e5d5ae02e553880a98829bec8048f5ce7eb77b6a86338b2accdd62d66c9ca5e2d2b653078a6c0df0cc5526d44717b49a5abd9b683c6c385ec0f51ad8a41f
|
data/lib/opencage.rb
ADDED
data/lib/opencage/geocoder.rb
CHANGED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module OpenCage
|
5
|
+
class Geocoder
|
6
|
+
class Location
|
7
|
+
attr_reader :geo, :name
|
8
|
+
|
9
|
+
def initialize(geo, options={})
|
10
|
+
@geo = geo
|
11
|
+
@lat = options[:lat]
|
12
|
+
@lng = options[:lng]
|
13
|
+
@name = options[:name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def lat
|
17
|
+
@lat ||= results.first['geometry']['lat'].to_f
|
18
|
+
end
|
19
|
+
|
20
|
+
def lng
|
21
|
+
@lng ||= results.first['geometry']['lng'].to_f
|
22
|
+
end
|
23
|
+
|
24
|
+
def name
|
25
|
+
@name ||= results.first['formatted']
|
26
|
+
end
|
27
|
+
|
28
|
+
def coordinates
|
29
|
+
[ lat, lng ]
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def results
|
35
|
+
@results ||= Array(fetch).tap do |results|
|
36
|
+
raise GeocodingError.new('location not found') if results.empty?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def fetch
|
41
|
+
JSON.parse(open(url).read)['results']
|
42
|
+
rescue OpenURI::HTTPError => error
|
43
|
+
raise GeocodingError.new(error_message(error))
|
44
|
+
end
|
45
|
+
|
46
|
+
def url
|
47
|
+
uri = URI.parse(geo.url)
|
48
|
+
uri.query = [uri.query, "q=#{query}"].join('&')
|
49
|
+
uri
|
50
|
+
end
|
51
|
+
|
52
|
+
def query
|
53
|
+
if @lat && @lng && !@name
|
54
|
+
"#{lat},#{lng}"
|
55
|
+
elsif @name
|
56
|
+
URI::encode(@name)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def error_message(error)
|
61
|
+
(String(error) =~ /\A403/) ? 'invalid API key' : error
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,46 +1,47 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opencage-geocoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Samuel Scully
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-10-27 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
|
-
description: A client for the OpenCage Data geocoder API
|
13
|
+
description: A client for the OpenCage Data geocoder API - http://geocoder.opencagedata.com/
|
15
14
|
email: dev@lokku.com
|
16
15
|
executables: []
|
17
16
|
extensions: []
|
18
17
|
extra_rdoc_files: []
|
19
18
|
files:
|
19
|
+
- lib/opencage.rb
|
20
20
|
- lib/opencage/geocoder.rb
|
21
|
+
- lib/opencage/geocoder/location.rb
|
22
|
+
- lib/opencage/version.rb
|
21
23
|
homepage: https://github.com/lokku/ruby-opencage-geocoder
|
22
24
|
licenses:
|
23
25
|
- MIT
|
26
|
+
metadata: {}
|
24
27
|
post_install_message:
|
25
28
|
rdoc_options: []
|
26
29
|
require_paths:
|
27
30
|
- lib
|
28
31
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
-
none: false
|
30
32
|
requirements:
|
31
|
-
- -
|
33
|
+
- - ">="
|
32
34
|
- !ruby/object:Gem::Version
|
33
35
|
version: '0'
|
34
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
37
|
requirements:
|
37
|
-
- -
|
38
|
+
- - ">="
|
38
39
|
- !ruby/object:Gem::Version
|
39
40
|
version: '0'
|
40
41
|
requirements: []
|
41
42
|
rubyforge_project:
|
42
|
-
rubygems_version:
|
43
|
+
rubygems_version: 2.4.5.1
|
43
44
|
signing_key:
|
44
|
-
specification_version:
|
45
|
+
specification_version: 4
|
45
46
|
summary: A client for the OpenCage Data geocoder API
|
46
47
|
test_files: []
|