im_hungry 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 843c399eae4d7284a0116eb3cbfcc676565ffc97f472602513bf3538245ca45b
4
+ data.tar.gz: 4686e0cc3d58524fec2aa36bf197fe2d9c672499e9547c8af5be13c54add8920
5
+ SHA512:
6
+ metadata.gz: 4adcbf4bec9a33c60499e1b7c88deaacbbf6d7949cac84fdfab4cfe543d410ceed1e83bf3a650398ef2ab4f8fd514e69b7187c907c860b6f08335b376772daf3
7
+ data.tar.gz: ba9c40cc795955cc1f9ac6ead5ceb3c3d9c312a85f6c259b5c28c70896753da51979706e6fc97d4fd76380ba56c7f00b91312e12f2eca6a1bbe6665da93f50e5
Binary file
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in im_hungry.gemspec
6
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ im_hungry (0.1.0)
5
+ tzinfo (= 1.2.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.8.0)
13
+ rspec-core (~> 3.8.0)
14
+ rspec-expectations (~> 3.8.0)
15
+ rspec-mocks (~> 3.8.0)
16
+ rspec-core (3.8.0)
17
+ rspec-support (~> 3.8.0)
18
+ rspec-expectations (3.8.3)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-mocks (3.8.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-support (3.8.0)
25
+ thread_safe (0.3.6)
26
+ tzinfo (1.2.5)
27
+ thread_safe (~> 0.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.17)
34
+ im_hungry!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019
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.
@@ -0,0 +1,57 @@
1
+ # ImHungry
2
+
3
+ ![Image of CLI output](./assets/preview.png)
4
+
5
+ ## If this was a web application...
6
+
7
+ I think speed and availability are the most important features to improve on for a web application. I noticed all of the start and end times are always on the hour. We can use this to our advantage by pre-filtering and pre-sorting the food trucks for every hour of the week.
8
+
9
+ I would fetch the data as a daily / hourly background worker and store the response in a database. Once the fetch worker is successful and complete, another worker would bucket the food trucks into an in-memory key/value data store (redis or memcache). The keys of this cache would be the `day_of_week,hour_of_day`. In O(1) time, we could retrieve a sorted list at any hour. The bucket cache will consume worst-case additional 8.4MB of memory [0]. This will make reads super quick since we will not need to filter and sort. Assuming N trucks with K trucks in the current hour, the complexity costs of filtering and sorting is O(N + Klog(K)).
10
+
11
+ This approach will have high availability since it will not be dependent on external services since the web requests will always retrieve the data from our own servers.
12
+
13
+ [0] - 24 hours * 7 days * 50 bytes * 1000 trucks = ~8.4 megabytes if we have 1000 trucks open 24/7.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'im_hungry'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install im_hungry
30
+
31
+ ## Usage
32
+
33
+ `$ im_hungry`
34
+
35
+ Example output:
36
+
37
+ ```
38
+ $ im_hungry
39
+
40
+ Here is what is open:
41
+
42
+ Page 1 of 2
43
+ Bay Area Dots, LLC at 900 BEACH ST
44
+ Bonito Poke at 409 ILLINOIS ST
45
+ Casey's Pizza, LLC at 1 POST ST
46
+ Expresso Subito, LLC. at 150 CALIFORNIA ST
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ 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).
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
Binary file
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'im_hungry'
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(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'im_hungry'
5
+
6
+ ImHungry::CLI.start
@@ -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,13 @@
1
+ require 'im_hungry/version'
2
+ require 'im_hungry/cli'
3
+ require 'im_hungry/fetch_json'
4
+ require 'im_hungry/tz_helper'
5
+ require 'im_hungry/food_truck'
6
+ require 'im_hungry/paginate'
7
+ require 'tzinfo'
8
+ require 'readline'
9
+
10
+ module ImHungry
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,30 @@
1
+ module ImHungry
2
+ class CLI
3
+ NUM_PER_PAGE = 10
4
+ URL = 'http://data.sfgov.org/resource/bbb8-hzi6.json'.freeze
5
+
6
+ def self.start
7
+ food_trucks = fetch_im_hungry
8
+ filter_food_trucks!(food_trucks)
9
+ sort_food_trucks!(food_trucks)
10
+ print(food_trucks)
11
+ exit
12
+ end
13
+
14
+ def self.fetch_im_hungry
15
+ ImHungry::FetchJson.fetch(URL, ImHungry::FoodTruck)
16
+ end
17
+
18
+ def self.filter_food_trucks!(food_trucks)
19
+ food_trucks.select!(&:currently_open?)
20
+ end
21
+
22
+ def self.sort_food_trucks!(food_trucks)
23
+ food_trucks.sort_by!(&:applicant)
24
+ end
25
+
26
+ def self.print(food_trucks)
27
+ ImHungry::Paginate.new(food_trucks, NUM_PER_PAGE).print
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module ImHungry
5
+ class FetchJson
6
+ def self.fetch(url, model)
7
+ uri = URI.parse(url)
8
+ req = Net::HTTP::Get.new(uri)
9
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
10
+ http.request(req)
11
+ end
12
+ raise "HTTP code #{res.code} - Failure fetching json" if res.code != '200'
13
+ self.to_model(res.body, model)
14
+ rescue SocketError
15
+ puts 'Network connectivity issue'
16
+ exit
17
+ rescue Errno::ECONNREFUSED => e
18
+ puts 'The server is down.'
19
+ puts e.message
20
+ exit
21
+ rescue Timeout::Error => e
22
+ puts 'Timeout error occurred.'
23
+ puts e.message
24
+ exit
25
+ rescue Errno::EINVAL, Errno::ECONNRESET, EOFError,
26
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
27
+ Net::ProtocolError => e
28
+ puts 'Error occurred.'
29
+ puts e.message
30
+ exit
31
+ end
32
+
33
+ def self.to_model(body, model)
34
+ JSON.parse(body).map do |food_truck_json|
35
+ model.new(food_truck_json)
36
+ end
37
+ rescue JSON::ParserError => e
38
+ puts 'Invalid JSON'
39
+ puts e.message
40
+ exit
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,52 @@
1
+ module ImHungry
2
+ class FoodTruck
3
+ include ImHungry::TzHelper
4
+ TZ = 'US/Pacific'
5
+
6
+ attr_accessor :start24, :end24, :dayorder, :applicant, :location
7
+
8
+ def initialize(food_truck)
9
+ self.start24 = food_truck["start24"]
10
+ self.end24 = food_truck["end24"]
11
+ self.dayorder = food_truck["dayorder"]
12
+ self.applicant = food_truck["applicant"]
13
+ self.location = food_truck["location"]
14
+ end
15
+
16
+ def wday
17
+ dayorder&.to_i
18
+ end
19
+
20
+ def start_time
21
+ parse_time(start24)
22
+ end
23
+
24
+ def end_time
25
+ parse_time(end24)
26
+ end
27
+
28
+ def is_current_day_of_week?
29
+ wday == current_time.wday
30
+ end
31
+
32
+ def has_started?
33
+ start_time <= current_time
34
+ end
35
+
36
+ def has_ended?
37
+ end_time > current_time
38
+ end
39
+
40
+ def currently_open?
41
+ is_current_day_of_week? && has_started? && has_ended?
42
+ end
43
+
44
+ def time_zone
45
+ TZ
46
+ end
47
+
48
+ def to_s
49
+ "#{applicant} at #{location}"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ module ImHungry
2
+ class Paginate
3
+ attr_accessor :array, :per_page
4
+
5
+ def initialize(array, per_page)
6
+ self.array = array
7
+ self.per_page = per_page
8
+ end
9
+
10
+ def pages
11
+ @pages ||= array.each_slice(per_page).to_a
12
+ end
13
+
14
+ def page_lines(page, i)
15
+ [
16
+ '',
17
+ "Page #{i + 1} of #{pages.length}",
18
+ page.map(&:to_s)
19
+ ]
20
+ end
21
+
22
+ def print_page(page, i)
23
+ puts page_lines(page, i)
24
+ end
25
+
26
+ def print
27
+ puts
28
+ puts 'Here is what is open:'
29
+ pages.each_with_index do |page, i|
30
+ print_page(page, i)
31
+
32
+ last_page = i == pages.length - 1
33
+ Readline.readline('Next page?', true) unless last_page
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module ImHungry
2
+ module TzHelper
3
+ def parse_time(time)
4
+ Time.parse("#{time} #{tz_offset}")
5
+ end
6
+
7
+ def tz_offset
8
+ timezone = TZInfo::Timezone.get(time_zone)
9
+ offset_in_hours = timezone.current_period.utc_total_offset_rational.numerator
10
+ format('%+.2d:00', offset_in_hours)
11
+ end
12
+
13
+ def current_time
14
+ Time.now.getlocal(tz_offset)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ImHungry
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,43 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'im_hungry/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'im_hungry'
7
+ spec.version = ImHungry::VERSION
8
+ spec.authors = ['ANON']
9
+ spec.email = ['ANON@ANON.io']
10
+
11
+ spec.summary = 'Puts out the food trucks that are currently open in SF'
12
+ spec.description = 'Uses SF open data to find food trucks that are currently open.'
13
+ spec.homepage = 'https://github.com'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
20
+
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = 'https://github.com/'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = 'bin'
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_development_dependency 'bundler', '~> 1.17'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.executables = ['im_hungry']
41
+
42
+ spec.add_dependency 'tzinfo', '1.2.5'
43
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: im_hungry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ANON
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
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: tzinfo
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.5
69
+ description: Uses SF open data to find food trucks that are currently open.
70
+ email:
71
+ - ANON@ANON.io
72
+ executables:
73
+ - im_hungry
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".DS_Store"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - assets/preview.png
87
+ - bin/console
88
+ - bin/im_hungry
89
+ - bin/setup
90
+ - lib/im_hungry.rb
91
+ - lib/im_hungry/cli.rb
92
+ - lib/im_hungry/fetch_json.rb
93
+ - lib/im_hungry/food_truck.rb
94
+ - lib/im_hungry/paginate.rb
95
+ - lib/im_hungry/tz_helper.rb
96
+ - lib/im_hungry/version.rb
97
+ - sf_food_trucks.gemspec
98
+ homepage: https://github.com
99
+ licenses:
100
+ - MIT
101
+ metadata:
102
+ allowed_push_host: https://rubygems.org/
103
+ homepage_uri: https://github.com
104
+ source_code_uri: https://github.com/
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
+ rubygems_version: 3.0.3
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Puts out the food trucks that are currently open in SF
124
+ test_files: []