distance_finder 1.0.0

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: 8e99628ba43733e1e290acf03a6803bccd4d3281
4
+ data.tar.gz: ee137bff4f70946a6bd41f50717712baadda05fa
5
+ SHA512:
6
+ metadata.gz: 9ae1fb00baf177e3310930e1fe0a545138a10f43a6c933b61dd5866a9dd50d621504d9f413af6d5d2664881731f802534983b593a012a4ad29e5d2f27823a6e7
7
+ data.tar.gz: 192e1251bb777fd6808a177fd50f9f867ed822ff97bafa88a8c7a387bc8749a730903cdc5fc6c03bb665c7c830c3685f3780dc3c70aea5f3dea4944c02a40c80
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in distance_finder.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+ watch(%r{^test/(.+)_test\.rb$}) { 'test' }
24
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 scieslak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # DistanceFinder
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/distance_finder`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'distance_finder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install distance_finder
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/distance_finder.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "distance_finder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'distance_finder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "distance_finder"
8
+ spec.version = DistanceFinder::VERSION
9
+ spec.authors = ["Slawomir Cieslak"]
10
+ spec.email = ["scieslak@scieslak.com"]
11
+
12
+ spec.summary = %q{DistanceFinder gets distance between two provided locations.}
13
+ spec.description = %q{DistanceFinder gets distance between two provided locations useing google maps API}
14
+ spec.homepage = "https://scieslak.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "thor", "0.19.1"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ spec.add_development_dependency "minitest-reporters", "1.1.14"
39
+ spec.add_development_dependency "guard", "2.14"
40
+ spec.add_development_dependency "guard-minitest", "2.4.6"
41
+ end
@@ -0,0 +1,6 @@
1
+ #!/user/bin/env ruby
2
+
3
+ require 'distance_finder'
4
+
5
+ DistanceFinder::Interface.welcome
6
+ DistanceFinder::Interface.calculate
@@ -0,0 +1,68 @@
1
+ module DistanceFinder
2
+ class Calculator
3
+
4
+ attr_reader :distance, :duration, :start_address, :end_address, :display_full_response
5
+
6
+ def initialize(origin, destination)
7
+ @origin = origin.strip().tr(" ", "+")
8
+ @destination = destination.strip().tr(" ", "+")
9
+ @google_object = parse_response
10
+ end
11
+
12
+
13
+ # Returns response
14
+ def status
15
+ @google_object["status"]
16
+ end
17
+
18
+ # Return distance
19
+ def distance
20
+ @google_object["routes"][0]["legs"][0]["distance"]["value"] / 1000 * 0.621371
21
+ end
22
+
23
+ # Return duration
24
+ def duration
25
+ @google_object["routes"][0]["legs"][0]["duration"]["text"]
26
+ end
27
+
28
+ # Return origin name
29
+ def start_address
30
+ @google_object["routes"][0]["legs"][0]["start_address"]
31
+ end
32
+
33
+ # Returns destination name
34
+ def end_address
35
+ @google_object["routes"][0]["legs"][0]["end_address"]
36
+ end
37
+
38
+ # Generate pretty JSON for command line output
39
+ def full_response
40
+ puts JSON.pretty_generate(@google_object)
41
+ end
42
+
43
+ def parse_response
44
+ if get_response.kind_of? Net::HTTPSuccess
45
+ JSON.parse(get_response.body)
46
+ elsif !get_response
47
+ JSON.parse('{"status" : "No internet connection!"}')
48
+ else
49
+ JSON.parse('{"status" : "Google API unaviable!"}')
50
+ end
51
+ end
52
+
53
+ # Checks for internet connection and gets response from Google API
54
+ def get_response
55
+ begin
56
+ TCPSocket.new 'google.com', 80
57
+ Net::HTTP.get_response(URI(build_url))
58
+ rescue SocketError
59
+ return false
60
+ end
61
+ end
62
+
63
+ def build_url
64
+ "https://maps.googleapis.com/maps/api/directions/json?origin=#{@origin}&destination=#{@destination}"
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,112 @@
1
+ module DistanceFinder
2
+ class Interface
3
+
4
+ @@line_length = 60
5
+ @@right_margin = 3
6
+ @@result_ljust = 15
7
+
8
+ # Displays welcome information and help
9
+ def self.welcome
10
+ puts divider("Welcome to DistanceFinder!")
11
+
12
+ text = "Get distance and travel time between almost any two places on the globe.DistanceFinder uses powerful Google API to deliver reliable results fast and without unnecessary baggage."
13
+
14
+ format_text(text).each {|line| puts line.center(@@line_length)}
15
+
16
+ puts "\n"
17
+
18
+ # Display help
19
+ help
20
+ end
21
+
22
+ # Receives input data and returns route details
23
+ def self.calculate
24
+ origin = nil
25
+ destination = nil
26
+
27
+ loop do
28
+
29
+ print margin + "⊗ Insert origin:> "
30
+ origin = gets.chomp
31
+ bye if origin == "quit"
32
+
33
+ print margin + "⊗ Insert destination:> "
34
+ destination = gets.chomp
35
+ bye if destination == "quit"
36
+
37
+
38
+ route = DistanceFinder::Calculator.new(origin, destination)
39
+
40
+ puts divider
41
+
42
+ if route.status == "OK"
43
+ puts format_result("⊗ Origin:", route.start_address)
44
+ puts format_result("⊗ Destination:", route.end_address)
45
+ puts format_result("⇄ Distance:", route.distance.round(1).to_s + "miles")
46
+ puts format_result("🕑 Travel time:", route.duration)
47
+ else
48
+ puts format_result("Sorry, unable to find the route.")
49
+ puts format_result("Status: " + route.status.upcase)
50
+ end
51
+
52
+ puts divider
53
+
54
+ end
55
+
56
+ end
57
+
58
+ #Displays help
59
+ def self.help
60
+ puts divider("Help")
61
+ puts "Possible input formats:"
62
+
63
+ input_formats = [
64
+ "[TOWN]",
65
+ "[STREET] [TOWN]",
66
+ "[HOUSE NUMBER] [STREET] [TOWN]",
67
+ "[HOUSE NUMBER] [STREET] [TOWN] [COUNTRY]",
68
+ "[HOUSE NUMBER] [STREET] [TOWN] [COUNTRY] [POST CODE]",
69
+ "[STREET] [TOWN] [COUNTRY] [POST CODE]",
70
+ "[TOWN] [COUNTRY] [POST CODE]",
71
+ "[COUNTRY] [POST CODE]",
72
+ "[POST CODE]",
73
+ "",
74
+ "`quit` for exit."
75
+ ]
76
+ input_formats.each {|input| puts " ".ljust(3) + input}
77
+ puts divider
78
+ end
79
+
80
+ # Exits application
81
+ def self.bye
82
+ puts divider("Thanks, See you next time, Bye!")
83
+ exit
84
+ end
85
+
86
+ ############# helper methods
87
+
88
+ # Sets margine value
89
+ def self.margin
90
+ "".ljust(@@right_margin)
91
+ end
92
+
93
+ # Returns divider string
94
+ def self.divider(text="***")
95
+ text_length = text.length
96
+ hash_count = (@@line_length - text_length - 2) / 2
97
+ print_hash = '#' * hash_count
98
+ pretty_text = "#{print_hash} #{text} #{print_hash}"
99
+ pretty_text << '#' if pretty_text.length == (@@line_length - 1)
100
+ "\n #{pretty_text.center(@@line_length)}\n\n"
101
+ end
102
+
103
+ # Returns array of lines of set length
104
+ def self.format_text(text)
105
+ text.scan /\S.{1,#{@@line_length}}(?!\S)/
106
+ end
107
+
108
+ def self.format_result(label, value="")
109
+ margin + label.ljust(@@result_ljust) + value
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,3 @@
1
+ module DistanceFinder
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "distance_finder/version"
2
+ require "distance_finder/calculator"
3
+ require "distance_finder/interface"
4
+ require "net/http"
5
+ require "json"
6
+ require 'socket'
7
+
8
+ module DistanceFinder
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: distance_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Slawomir Cieslak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.1
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.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.14
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.14
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: '2.14'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: '2.14'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.4.6
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.4.6
111
+ description: DistanceFinder gets distance between two provided locations useing google
112
+ maps API
113
+ email:
114
+ - scieslak@scieslak.com
115
+ executables:
116
+ - distance_finder
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Guardfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - distance_finder.gemspec
130
+ - exe/distance_finder
131
+ - lib/distance_finder.rb
132
+ - lib/distance_finder/calculator.rb
133
+ - lib/distance_finder/interface.rb
134
+ - lib/distance_finder/version.rb
135
+ homepage: https://scieslak.com
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: DistanceFinder gets distance between two provided locations.
159
+ test_files: []