stanford_menus 0.0.1

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
+ SHA1:
3
+ metadata.gz: fa63b1f9c6935a16a371fb94fa720cf052f6ad51
4
+ data.tar.gz: 9d74d2560deaea482c49f3163e9a67d497c243a1
5
+ SHA512:
6
+ metadata.gz: 681a01e3b7b31190b7a498cd57a75d3b47efd79d71684289a99136546ce1f3ce88f7e02b267cf21c7b8c945cfe7659e70f4752b4093e863137ce13ceadb324c4
7
+ data.tar.gz: f1d9481339d4756a2695b2d1a15bb344fd210789044bfb0c31e9a2128f5e88c3bbb05b8e09b2be12a9976e038c7c99ef23fad82e356c013fa527ebf593b27195
@@ -0,0 +1,12 @@
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
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
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 stanford_menus.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jessie Keck
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,39 @@
1
+ # StanfordMenus
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/stanford_menus`. 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 'stanford_menus'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install stanford_menus
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 spec` 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]/stanford_menus.
36
+
37
+ ## License
38
+
39
+ 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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stanford_menus"
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,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,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'stanford_menus'
4
+ require 'thor'
5
+
6
+ StanfordMenus::CLI.start(ARGV)
@@ -0,0 +1,18 @@
1
+ require 'stanford_menus/version'
2
+ require 'stanford_menus/menus'
3
+
4
+ module StanfordMenus
5
+ autoload :CLI, 'stanford_menus/cli'
6
+ autoload :StdoutPrinter, 'stanford_menus/stdout_printer'
7
+
8
+ class << self
9
+ attr_writer :renderer, :restaurant_class_map
10
+ def restaurant_class_map
11
+ @restaurant_class_map ||= {}
12
+ end
13
+
14
+ def renderer
15
+ @renderer || StdoutPrinter
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ require 'stanford_menus/menus/gsb'
2
+
3
+ module StanfordMenus
4
+ # stanford_menus --all
5
+ # stanford_menus --for GSB
6
+ # stanford_menus --for GSB --category action
7
+ class CLI < Thor
8
+ desc 'all', 'Print menus for all restaurants (defualt)'
9
+ option :for, type: :string, aliases: :f, banner: 'The key of the a specific restaurant to print a menu for. (Example: GSB)'
10
+ option :category, type: :string, aliases: :c, banner: 'A string to filter the menu on. This is restaurant specific. (Example: "market grill")'
11
+ option :price, type: :string, aliases: :p, banner: 'A number to filter the price on. (Example: 7, or \$5)'
12
+ def all
13
+ menus = if options[:category]
14
+ restaurants.map do |restaurant|
15
+ restaurant.filtered_by_category(options[:category])
16
+ end
17
+ elsif options[:price]
18
+ restaurants.map do |restaurant|
19
+ restaurant.filtered_by_price(options[:price])
20
+ end
21
+ else
22
+ restaurants.map(&:all)
23
+ end
24
+
25
+ StanfordMenus.renderer.new(menus).render
26
+ end
27
+ default_task :all
28
+
29
+ private
30
+
31
+ def restaurants
32
+ if options[:for]
33
+ [StanfordMenus::Menus.for(options[:for])]
34
+ else
35
+ StanfordMenus.restaurant_class_map.keys.map do |restaurant|
36
+ StanfordMenus::Menus.for(restaurant)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ module StanfordMenus
2
+ module Menus
3
+ autoload :GSB, 'stanford_menus/menus/gsb'
4
+
5
+ def self.for(restaurant)
6
+ klass = StanfordMenus.restaurant_class_map[restaurant]
7
+ raise ArgumentError, "\"#{restaurant}\" is not a registered restaurant" unless klass
8
+
9
+ begin
10
+ Object.const_get(klass).new
11
+ rescue NameError
12
+ raise ArgumentError, "The class \"#{klass}\" configured for \"#{restaurant}\" is not an available"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,129 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module StanfordMenus
5
+ module Menus
6
+ class GSB
7
+ URL = 'https://stanfordgsb.cafebonappetit.com/'.freeze
8
+
9
+ def all(&_block)
10
+ (block_given? ? yield(grouped_items) : grouped_items).map do |station, items|
11
+ Struct.new(:station, :items).new(station, items)
12
+ end
13
+ end
14
+
15
+ def filtered_by_category(station_query)
16
+ normalized_query = normalize_station(station_query)
17
+ all do |grouped_items|
18
+ grouped_items.select do |station, _|
19
+ (normalize_station(station) & normalized_query).any? ||
20
+ normalize_station(station).join('').include?(normalized_query.join(''))
21
+ end
22
+ end
23
+ end
24
+
25
+ def filtered_by_price(price_query)
26
+ all do |grouped_items|
27
+ grouped_items.each_with_object({}) do |(station, items), hash|
28
+ next unless (filted_items = items.select { |item| item.costs_less_than?(price_query) }).any?
29
+ hash[station] = filted_items
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def grouped_items
37
+ items.select(&:special).group_by(&:station)
38
+ end
39
+
40
+ def normalize_station(string)
41
+ string
42
+ .downcase
43
+ .gsub(/[[:punct:]]/, ' ')
44
+ .split(/\s/)
45
+ .compact
46
+ .reject(&:empty?)
47
+ end
48
+
49
+ def items
50
+ return to_enum(:items) unless block_given?
51
+ json.values.each do |item_json|
52
+ yield Item.new(item_json)
53
+ end
54
+ end
55
+
56
+ def json
57
+ JSON.parse(json_string_from_response)
58
+ end
59
+
60
+ def response
61
+ @response ||= Faraday.get(URL).body
62
+ end
63
+
64
+ def json_string_from_response
65
+ @json_string_from_response ||= begin
66
+ response[/Bamco\.menu_items = (.*)\n/]
67
+ Regexp.last_match.to_s.gsub(/;$/, '').gsub('Bamco.menu_items = ', '')
68
+ end
69
+ end
70
+
71
+ class Item
72
+ attr_reader :json
73
+ def initialize(json)
74
+ @json = json
75
+ end
76
+
77
+ def label
78
+ json['label']
79
+ end
80
+
81
+ def description
82
+ json['description']
83
+ end
84
+
85
+ def station
86
+ json['station'].gsub(%r{</?strong>}, '').gsub(/^@/, '')
87
+ end
88
+
89
+ def special
90
+ json['special'] == 1
91
+ end
92
+
93
+ def costs_less_than?(price)
94
+ price = price.sub(/^\$/, '').to_f
95
+ price_floats.any? { |item_price| item_price <= price }
96
+ end
97
+
98
+ def price_floats
99
+ prices = !sizes.empty? ? sizes.map(&:price) : [json['price']]
100
+ prices.map do |price|
101
+ price.sub(/^\$/, '').to_f
102
+ end
103
+ end
104
+
105
+ def price
106
+ return json['price'] if sizes.empty?
107
+
108
+ sizes.map do |size|
109
+ "#{size.price} (#{size.size})"
110
+ end.join(' | ')
111
+ end
112
+
113
+ def sizes
114
+ return [] unless json['sizes']
115
+
116
+ json['sizes'].map do |size|
117
+ Struct.new(:size, :price).new(size['size'], size['price'])
118
+ end
119
+ end
120
+
121
+ def to_s
122
+ "#{label} [#{price}]\n\t#{description}\n\n"
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ StanfordMenus.restaurant_class_map['GSB'] = 'StanfordMenus::Menus::GSB'
@@ -0,0 +1,17 @@
1
+ module StanfordMenus
2
+ class StdoutPrinter
3
+ attr_reader :restaurants
4
+ def initialize(restaurants)
5
+ @restaurants = restaurants
6
+ end
7
+
8
+ def render
9
+ restaurants.each do |menus|
10
+ menus.each do |menu|
11
+ puts menu.station
12
+ menu.items.each { |item| puts "\t#{item}" }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module StanfordMenus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "stanford_menus/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stanford_menus"
8
+ spec.version = StanfordMenus::VERSION
9
+ spec.authors = ["Jessie Keck"]
10
+ spec.email = ["jessie.keck@gmail.com"]
11
+
12
+ spec.summary = %q{A CLI for accessing Stanford Food Menu options}
13
+ spec.homepage = "https://github.com/jkeck/stanford_menus"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.bindir = "exe"
21
+ spec.executables = %w[stanford_menus]
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_runtime_dependency "faraday"
29
+ spec.add_runtime_dependency "json"
30
+ spec.add_runtime_dependency "thor"
31
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stanford_menus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jessie Keck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-01 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - jessie.keck@gmail.com
100
+ executables:
101
+ - stanford_menus
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/stanford_menus
115
+ - lib/stanford_menus.rb
116
+ - lib/stanford_menus/cli.rb
117
+ - lib/stanford_menus/menus.rb
118
+ - lib/stanford_menus/menus/gsb.rb
119
+ - lib/stanford_menus/stdout_printer.rb
120
+ - lib/stanford_menus/version.rb
121
+ - stanford_menus.gemspec
122
+ homepage: https://github.com/jkeck/stanford_menus
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.11
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A CLI for accessing Stanford Food Menu options
146
+ test_files: []