georama 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +7 -0
- data/README.md +2 -0
- data/georama.gemspec +26 -0
- data/lib/georama.rb +4 -0
- data/lib/georama/parser.rb +36 -0
- data/lib/georama/url.rb +51 -0
- data/lib/georama/version.rb +3 -0
- data/spec/georama/parser_spec.rb +63 -0
- data/spec/georama/url_spec.rb +88 -0
- data/spec/spec_helper.rb +13 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a1325af5593583c33903c73c8215c06d48acc925
|
4
|
+
data.tar.gz: 315425c00c6ab1559677524c9682f450bff8364c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e84530110d0fb0d2f31757e6d59743c29aa52534145eb69d810e7e3be33eed795f8d6f05c5e13875bb12a134e419508fef9fae5588551bb6c48625f3cd00b6de
|
7
|
+
data.tar.gz: e7bb16fb354936d3fbcf419d76e945652e1ee75485560db5ae9e6ba19ca8292fc21a72a591a6fd88cf61830437d0d7b3e6ab156d1172652a74f0e7f32060f24d
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/georama.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'georama/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "georama"
|
8
|
+
spec.version = Georama::VERSION
|
9
|
+
spec.authors = ["Martin Pretorius"]
|
10
|
+
spec.email = ["glasnoster@gmail.com"]
|
11
|
+
spec.summary = %q{A simple google maps url parser}
|
12
|
+
spec.description = %q{Extract information like coordinates from a google maps url}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "semvergen", "~> 1.3"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "guard-rspec", "~> 4.0"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
end
|
data/lib/georama.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Georama
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
def self.is_google_maps_url?(url)
|
5
|
+
raise ArgumentError, "No url specified" if url.nil?
|
6
|
+
parsed = URI.parse(url)
|
7
|
+
is_google = parsed.host == "www.google.com"
|
8
|
+
is_maps = parsed.path.start_with?("/maps/")
|
9
|
+
is_google && is_maps
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.url_type(path)
|
13
|
+
raise ArgumentError, "No path specified" if path.nil?
|
14
|
+
split_path = path.split("/")
|
15
|
+
if split_path[2].start_with?("@")
|
16
|
+
:general
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.get_coordinates(params)
|
21
|
+
split_params = params.split(",")
|
22
|
+
{}.tap do |res|
|
23
|
+
res[:latitude] = split_params[0].gsub("@", "").to_f
|
24
|
+
res[:longitude] = split_params[1].to_f
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_metadata(params)
|
29
|
+
split_params = params.split(",")
|
30
|
+
{}.tap do |res|
|
31
|
+
res[:zoom] = split_params[-1][/\d+\.?\d*+?(?=z)/].to_f
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/georama/url.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Georama
|
2
|
+
|
3
|
+
class Url
|
4
|
+
|
5
|
+
def initialize(url)
|
6
|
+
raise ArgumentError, "Expected a valid maps url, got nil" if url.nil?
|
7
|
+
raise ArgumentError, "Not a valid maps url" unless Georama::Parser.is_google_maps_url?(url)
|
8
|
+
@url_string = url
|
9
|
+
@parsed_url = URI.parse(url)
|
10
|
+
end
|
11
|
+
|
12
|
+
def type
|
13
|
+
@type ||= Georama::Parser.url_type(@parsed_url.path)
|
14
|
+
end
|
15
|
+
|
16
|
+
def coordinates
|
17
|
+
@coordinates ||= Georama::Parser.get_coordinates(coordinates_component)
|
18
|
+
end
|
19
|
+
|
20
|
+
def latitude
|
21
|
+
coordinates[:latitude]
|
22
|
+
end
|
23
|
+
|
24
|
+
def longitude
|
25
|
+
coordinates[:longitude]
|
26
|
+
end
|
27
|
+
|
28
|
+
def metadata
|
29
|
+
@metadata ||= Georama::Parser.get_metadata(coordinates_component)
|
30
|
+
end
|
31
|
+
|
32
|
+
def zoom
|
33
|
+
metadata[:zoom]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def coordinates_component
|
39
|
+
@coordinates_component ||= case type
|
40
|
+
when :general
|
41
|
+
path_components[1]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def path_components
|
46
|
+
@path_components ||= @parsed_url.path.split("/")[1..-1]
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Georama::Parser do
|
4
|
+
|
5
|
+
describe :url_type do
|
6
|
+
|
7
|
+
context "with an no path" do
|
8
|
+
it "raises an error" do
|
9
|
+
expect { Georama::Parser.url_type(nil) }.to raise_error ArgumentError, "No path specified"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
describe :is_google_maps_url? do
|
16
|
+
context "with no url" do
|
17
|
+
it "raises an error" do
|
18
|
+
expect { Georama::Parser.is_google_maps_url?(nil) }.to raise_error ArgumentError, "No url specified"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "with an invalid url" do
|
23
|
+
it "returns false" do
|
24
|
+
expect(Georama::Parser.is_google_maps_url?("www.fake.com/foo/bar")).to be_falsey
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with a valid url" do
|
29
|
+
it "returns true" do
|
30
|
+
expect(Georama::Parser.is_google_maps_url?("https://www.google.com/maps/@-33.9218305,18.4296954,15z?hl=en")).to be_truthy
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe :get_coordinates do
|
36
|
+
context "with a valid coordinates component" do
|
37
|
+
let(:coordinates_component) { "@-33.9218305,18.4296954,15z?hl=en" }
|
38
|
+
let(:expected_coordinates) do
|
39
|
+
{
|
40
|
+
latitude: -33.9218305,
|
41
|
+
longitude: 18.4296954
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns a coordinates hash" do
|
46
|
+
expect(Georama::Parser.get_coordinates(coordinates_component)).to eq(expected_coordinates)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe :metadata do
|
52
|
+
context "with a valid coordinates component" do
|
53
|
+
let(:coordinates_component) { "@-33.9218305,18.4296954,15z?hl=en" }
|
54
|
+
let(:expected_zoom) { 15 }
|
55
|
+
|
56
|
+
it "returns the correct zoom" do
|
57
|
+
expect(Georama::Parser.get_metadata(coordinates_component)[:zoom]).to eq(expected_zoom)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Georama::Url do
|
4
|
+
|
5
|
+
let(:valid_general_url) { "https://www.google.com/maps/@-33.9218305,18.4296954,15z?hl=en" }
|
6
|
+
|
7
|
+
describe :initialize do
|
8
|
+
|
9
|
+
context "with an invalid url" do
|
10
|
+
let(:url) { "foo" }
|
11
|
+
|
12
|
+
it "raises an error" do
|
13
|
+
expect { Georama::Url.new(url) }.to raise_error ArgumentError, "Not a valid maps url"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with a nil url" do
|
18
|
+
it "raises an error" do
|
19
|
+
expect { Georama::Url.new(nil) }.to raise_error ArgumentError, "Expected a valid maps url, got nil"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "with a valid url" do
|
24
|
+
it "does not raise an error" do
|
25
|
+
expect { Georama::Url.new(valid_general_url) }.to_not raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe :type do
|
32
|
+
let(:result) { Georama::Url.new(valid_general_url) }
|
33
|
+
|
34
|
+
context "with a general url" do
|
35
|
+
|
36
|
+
it "returns the correct type" do
|
37
|
+
expect(result.type).to eq(:general)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe :coordinates do
|
44
|
+
|
45
|
+
context "with a general url" do
|
46
|
+
let(:result) { Georama::Url.new(valid_general_url) }
|
47
|
+
let(:expected_coordinates) do
|
48
|
+
{
|
49
|
+
latitude: -33.9218305,
|
50
|
+
longitude: 18.4296954
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
it "extracts the coordinates" do
|
55
|
+
expect(result.coordinates).to eq(expected_coordinates)
|
56
|
+
end
|
57
|
+
|
58
|
+
context "accessing the coordinates individually" do
|
59
|
+
it "it returns the correct latitude" do
|
60
|
+
expect(result.latitude).to eq(-33.9218305)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "it returns the correct longitude" do
|
64
|
+
expect(result.longitude).to eq(18.4296954)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe :metadata do
|
72
|
+
context "with a general url" do
|
73
|
+
let(:result) { Georama::Url.new(valid_general_url) }
|
74
|
+
it "extracts the zoom" do
|
75
|
+
expect(result.metadata[:zoom]).to eq(15)
|
76
|
+
end
|
77
|
+
|
78
|
+
context "accessing the metadata components individually" do
|
79
|
+
it "returns the zoom" do
|
80
|
+
expect(result.zoom).to eq(15)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'georama'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
|
5
|
+
config.expect_with :rspec do |expectations|
|
6
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
7
|
+
end
|
8
|
+
|
9
|
+
config.mock_with :rspec do |mocks|
|
10
|
+
mocks.verify_partial_doubles = true
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: georama
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Pretorius
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: semvergen
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
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
|
+
description: Extract information like coordinates from a google maps url
|
84
|
+
email:
|
85
|
+
- glasnoster@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- CHANGELOG.md
|
91
|
+
- Gemfile
|
92
|
+
- README.md
|
93
|
+
- georama.gemspec
|
94
|
+
- lib/georama.rb
|
95
|
+
- lib/georama/parser.rb
|
96
|
+
- lib/georama/url.rb
|
97
|
+
- lib/georama/version.rb
|
98
|
+
- spec/georama/parser_spec.rb
|
99
|
+
- spec/georama/url_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.4.5.1
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: A simple google maps url parser
|
125
|
+
test_files:
|
126
|
+
- spec/georama/parser_spec.rb
|
127
|
+
- spec/georama/url_spec.rb
|
128
|
+
- spec/spec_helper.rb
|