nyc-farmers-markets 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8928dc5449c9466f49d9fc74afd5f6d180e9606b
4
- data.tar.gz: d6636590140994ec2b695eee0bee2805d8757ddf
2
+ SHA256:
3
+ metadata.gz: 2b21916ba6a2b697a879dcde3d0b19d3a64f4f14d8bc502485c4e4ca3df380e6
4
+ data.tar.gz: ac30be3edd8e9e6169d98680d19ee2bf874326b82fa3b38a71606d45cd71d233
5
5
  SHA512:
6
- metadata.gz: e9634cb631551775abbc1cebfd183bcaf52e8e0ec5d579c1934ee57cce6ce6e8a2c63fafaad1467cdb40c83a08a9cacf6c3eae327a7abd6eff8bae393245cab4
7
- data.tar.gz: 935f212c0b6fec9f36152bf471ca5dfe8c0d9923f8ed3cf6b28caa129f599ce2551238a19f06a2c3d2760f684c27ef55e90ec23ea08d0171fff7b0b9979fd482
6
+ metadata.gz: a8f0b354a07a7ed87502a2cbaf00161f8f8b0f137fd34071da9a86ac4591721e99c38d2770a28f98f9f64ad5ff98385a9082f6c9a844bbcd9ddc785d2a70e70b
7
+ data.tar.gz: c0435b1210ab51b3b6290f91f5462efbd1aaf21ddad44baabf04a9c5c0dd2de1ff7b73e5b55d841f98bd1d092160bfa4e1a3640fec340d2bf7adc3d7088320d7
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require_relative '../lib/nyc_farmers_markets'
4
5
 
5
- NYCFarmersMarkets::CLI.new.call
6
+ NYCFarmersMarkets::Cli.call
@@ -1,4 +1,7 @@
1
- module NYCFarmersMarkets
2
- end
1
+ # frozen_string_literal: true
3
2
 
4
3
  require_relative '../config/environment'
4
+
5
+ # The main module
6
+ module NYCFarmersMarkets
7
+ end
@@ -1,93 +1,129 @@
1
- class NYCFarmersMarkets::CLI
1
+ # frozen_string_literal: true
2
2
 
3
- def call
4
- NYCFarmersMarkets::GetMarkets.new.make_markets
5
- vesta = "\u26B6".encode('utf-8')
6
- flower = "\u2698".encode('utf-8')
7
- puts "\n\t #{vesta} Welcome to the Farmers Markets of NYC #{vesta} ".green.underline
8
- print "\t"; 15.times { print "#{flower} ".light_blue }; puts ""
3
+ module NYCFarmersMarkets
4
+ EXIT_CMD = 'exit'
5
+ FLOWER = "\u2698".encode('utf-8')
6
+ FLOWER_ROW = Array.new(15, FLOWER).join(' ')
7
+ HAND = "\u261E".encode('utf-8')
8
+ KEYBOARD = "\u2328".encode('utf-8')
9
+ CMD_LIST = [
10
+ 'exit',
11
+ 'help',
12
+ 'list all',
13
+ 'list boroughs',
14
+ 'list zip codes',
15
+ 'version'
16
+ ].flatten
17
+ VESTA = "\u26B6".encode('utf-8')
18
+ WELCOME_MSG = 'Welcome to the Farmers Markets of NYC'
9
19
 
10
- start
11
- end
20
+ # The commandline interface class
21
+ class Cli
22
+ def self.call
23
+ new.call
24
+ end
12
25
 
13
- def start
14
- loop do
15
- print "\nWhat would you like to do (type help for more info)? "
26
+ def call
27
+ NYCFarmersMarkets::GetMarkets.new.make_markets
28
+ print_welcome
29
+ start
30
+ end
31
+
32
+ private
33
+
34
+ def start
35
+ loop do
36
+ input = prompt_user
37
+ interpret(input)
38
+ break if input == EXIT_CMD
39
+ end
40
+ end
16
41
 
17
- input = gets.chomp.downcase
42
+ def prompt_user
43
+ print "\nWhat would you like to do (type help for more info)? "
44
+ gets.chomp.downcase
45
+ end
18
46
 
47
+ def interpret(input)
19
48
  case input
20
- when "list all"
21
- list_all
22
- when "list boroughs"
23
- #puts market_set.list_boroughs.join(" - ")
24
- list_boroughs
25
- when "bronx", "brooklyn", "manhattan", "queens", "staten island"
49
+ when *CMD_LIST
50
+ send input.gsub(/\s/, '_').to_sym
51
+ when *market_set.boroughs_lowercase
26
52
  print_by_borough(input)
27
- when "list zip codes"
28
- puts market_set.list_zipcodes.join(" - ")
29
- when *market_set.list_zipcodes
30
- print_by_zipcode(input)
31
- when "help"
32
- help
33
- when "exit"
34
- puts "See you next time!"
35
- break
53
+ when *market_set.zip_codes
54
+ print_by_zip_code(input)
36
55
  else
37
- puts "Invalid selection!".red
56
+ puts 'Invalid selection!'.red
38
57
  end
39
58
  end
40
- end
41
59
 
42
- def print_by_borough(borough)
43
- market_set.find_by_borough(borough).each do |m|
44
- puts "\n#{m.name}".green
45
- puts "#{m.street_address}, #{m.borough}, #{m.zipcode}"
46
- puts "#{m.additional_info}" unless m.additional_info == nil
47
- puts "#{m.website}" unless m.website == nil
60
+ def exit
61
+ puts 'See you next time!'
48
62
  end
49
- end
50
63
 
51
- def print_by_zipcode(zipcode)
52
- market_set.find_by_zipcode(zipcode).each do |m|
53
- puts "\n#{m.name}".green
54
- puts "#{m.street_address}, #{m.borough}, #{m.zipcode}"
55
- puts "#{m.additional_info}" unless m.additional_info == nil
56
- puts "#{m.website}" unless m.website == nil
64
+ def print_by_borough(borough)
65
+ market_set.find_by_borough(borough).each do |market|
66
+ print_market_info market
67
+ end
57
68
  end
58
- end
59
69
 
60
- def list_all
61
- market_set.all.each do |m|
62
- puts "\n#{m.name}".green
63
- puts m.street_address == nil ? "-address not available-" : "#{m.street_address}, #{m.borough}, #{m.zipcode}"
64
- puts "#{m.additional_info}" unless m.additional_info == nil
65
- puts "#{m.website}" unless m.website == nil
70
+ def print_by_zip_code(zip_code)
71
+ market_set.find_by_zip_code(zip_code).each do |market|
72
+ print_market_info market
73
+ end
66
74
  end
67
- end
68
75
 
69
- def list_boroughs
70
- puts "\n\tBoroughs and Their Number of Farmers Markets".green
71
- boroughs_with_num_markets = market_set.list_boroughs.collect do |b|
72
- "#{b}(#{market_set.num_markets_in_borough(b)})"
76
+ def print_market_info(market)
77
+ info = "\n#{market.name}\n".green
78
+ info << "#{market.full_address}\n"
79
+ info << "#{market.additional_info}\n" unless market.additional_info.nil?
80
+ info << "#{market.website}\n" unless market.website.nil?
81
+ info << "#{market.open_street_map_link}\n" unless market.open_street_map_link.nil?
82
+ puts info
73
83
  end
74
- puts boroughs_with_num_markets.join(", ")
75
- end
76
84
 
77
- def help
78
- hand = "\u261E".encode('utf-8')
79
- keyboard = "\u2328".encode('utf-8')
80
- puts "\n #{keyboard} These are the available commands #{keyboard}".light_blue.underline
81
- puts "#{hand}list all\t-See a list of all Farmers Markets"
82
- puts "#{hand}list boroughs -See a list of boroughs with Farmers Markets"
83
- puts "#{hand}[borough name] -See all markets in this borough"
84
- puts "#{hand}list zip codes -See a list of zip codes"
85
- puts "#{hand}[zip code]\t-See all markets in this zip code"
86
- puts "#{hand}help\t\t-See this helpful list of commands!"
87
- puts "#{hand}exit\t\t-Say good-bye"
88
- end
85
+ def list_all
86
+ market_set.all.each do |market|
87
+ print_market_info market
88
+ end
89
+ end
90
+
91
+ def list_boroughs
92
+ puts "\n\tBoroughs and Their Number of Farmers Markets".green
93
+ boroughs_with_num_markets = market_set.boroughs.collect do |b|
94
+ "#{b}(#{market_set.num_markets_in_borough(b)})"
95
+ end
96
+ puts boroughs_with_num_markets.join(', ')
97
+ end
98
+
99
+ def list_zip_codes
100
+ puts market_set.zip_codes.join(' - ')
101
+ end
102
+
103
+ def help
104
+ headline = "\n #{KEYBOARD} These are the available commands #{KEYBOARD}"
105
+ puts headline.light_blue.underline
106
+ puts "#{HAND} list all\t - See a list of all Farmers Markets"
107
+ puts "#{HAND} list boroughs - See a list of boroughs with Farmers Markets"
108
+ puts "#{HAND} [borough name] - See all markets in this borough"
109
+ puts "#{HAND} list zip codes - See a list of zip codes"
110
+ puts "#{HAND} [zip code]\t - See all markets in this zip code"
111
+ puts "#{HAND} help\t\t - See this helpful list of commands!"
112
+ puts "#{HAND} version\t - See the currently running version!"
113
+ puts "#{HAND} exit\t\t - Say good-bye"
114
+ end
115
+
116
+ def market_set
117
+ @market_set ||= NYCFarmersMarkets::Market
118
+ end
119
+
120
+ def print_welcome
121
+ puts "\n\t #{VESTA} #{WELCOME_MSG} #{VESTA} ".green.underline
122
+ puts "\t#{FLOWER_ROW.light_blue}"
123
+ end
89
124
 
90
- def market_set
91
- NYCFarmersMarkets::Market
125
+ def version
126
+ puts NYCFarmersMarkets::VERSION
127
+ end
92
128
  end
93
129
  end
@@ -1,15 +1,22 @@
1
- class NYCFarmersMarkets::GetMarkets
2
- URL = "https://data.cityofnewyork.us/resource/cw3p-q2v6.json"
1
+ # frozen_string_literal: true
3
2
 
4
- def get_markets
5
- content = open(URL).read
6
- markets = JSON.parse(content)
7
- end
3
+ module NYCFarmersMarkets
4
+ # Retrieves the from the NYC Open Data API
5
+ class GetMarkets
6
+ # https://data.cityofnewyork.us/dataset/DOHMH-Farmers-Markets-and-Food-Boxes/8vwk-6iz2
7
+ URL = 'https://data.cityofnewyork.us/resource/94pk-v63f.json'
8
+
9
+ def make_markets
10
+ retrieved_markets.each do |market_hash|
11
+ NYCFarmersMarkets::Market.create_from_hash(market_hash)
12
+ end
13
+ NYCFarmersMarkets::Market.all
14
+ end
15
+
16
+ private
8
17
 
9
- def make_markets
10
- get_markets.each do |market_hash|
11
- NYCFarmersMarkets::Market.create_from_hash(market_hash)
18
+ def retrieved_markets
19
+ JSON.parse open(URL).read
12
20
  end
13
- NYCFarmersMarkets::Market.all
14
21
  end
15
22
  end
@@ -1,78 +1,102 @@
1
- class NYCFarmersMarkets::Market
2
- attr_accessor :name, :additional_info, :street_address, :borough, :state, :zipcode, :latitude, :longitude, :website
3
-
4
- @@all = []
5
-
6
- def initialize(name: nil, additional_info: nil, street_address: nil, borough: nil, state: "NY", zipcode: nil, website: nil)
7
- @name = name
8
- @additional_info = additional_info
9
- @street_address = street_address
10
- @borough = borough
11
- @state = state
12
- @zipcode = zipcode
13
- end
14
-
15
- def self.all
16
- @@all
17
- end
1
+ # frozen_string_literal: true
18
2
 
19
- def save
20
- @@all << self
21
- end
3
+ module NYCFarmersMarkets
4
+ # Organizes the actual market data
5
+ class Market
6
+ attr_reader :additional_info, :additional_info_raw, :borough, :name, :state,
7
+ :street_address, :latitude, :longitude, :website, :zip_code
22
8
 
23
- def self.create_from_hash(h)
24
- market = self.new(
25
- name: h["facilityname"],
26
- additional_info: h["facilityaddinfo"],
27
- street_address: h["facilitystreetname"],
28
- borough: h["facilitycity"],
29
- state: h["facilitystate"],
30
- zipcode: h["facilityzipcode"]
31
- )
32
-
33
- market.set_website_from_additional_info
34
- market.remove_html
35
- market.save
36
- market
37
- end
9
+ @all = []
38
10
 
39
- def set_website_from_additional_info
40
- if @additional_info != nil
41
- url_from_info = @additional_info.match(/http:(\w|\/|\.|\&|\=|\?|\_)*</)
42
- @website = url_from_info[0].chomp("<").chomp(".") unless url_from_info == nil
11
+ def initialize(attributes = {})
12
+ attributes.each do |attribute, value|
13
+ instance_variable_set "@#{attribute}", value
14
+ end
15
+ @state ||= 'NY'
43
16
  end
44
- end
45
17
 
46
- def remove_html
47
- if @additional_info != nil && @additional_info.include?("<")
48
- x = @additional_info.index("<")
49
- y = @additional_info.length - 1
50
- @additional_info.slice!(x..y)
18
+ def cleanup
19
+ set_website_from_additional_info_raw
20
+ set_additional_info
21
+ self
51
22
  end
52
23
 
53
- end
24
+ def full_address
25
+ return '- address not available -' if street_address.nil?
26
+ "#{street_address}, #{borough}, #{zip_code}"
27
+ end
54
28
 
55
- def self.find_by_borough(b)
56
- self.all.select do |market|
57
- market if market.borough != nil && market.borough.downcase == b.downcase
29
+ def open_street_map_link
30
+ return if latitude.to_s.empty? || longitude.to_s.empty?
31
+ 'https://www.openstreetmap.org/' \
32
+ "?mlat=#{latitude}&mlon=#{longitude}#map=11/#{latitude}/#{longitude}"
58
33
  end
59
- end
60
34
 
61
- def self.find_by_zipcode(z)
62
- self.all.select do |market|
63
- market if market.zipcode == z
35
+ class << self
36
+ attr_accessor :all
37
+
38
+
39
+ def create(attributes)
40
+ market = new attributes
41
+ market.cleanup
42
+ all << market
43
+ market
44
+ end
45
+
46
+ def create_from_hash(h)
47
+ create(
48
+ name: h['facilityname'],
49
+ additional_info_raw: h['additionalinfo'],
50
+ street_address: h['address'],
51
+ borough: h['borough'],
52
+ zip_code: h['zipcode'],
53
+ latitude: h['latitude'],
54
+ longitude: h['longitude'],
55
+ website: h['website']
56
+ )
57
+ end
58
+
59
+ def find_by_borough(b)
60
+ all.select { |m| b.casecmp(m.borough.to_s).zero? }
61
+ end
62
+
63
+ def find_by_zip_code(z)
64
+ all.select { |market| market.zip_code == z }
65
+ end
66
+
67
+ def boroughs
68
+ @boroughs ||= all.collect(&:borough).compact.uniq.sort
69
+ end
70
+
71
+ def boroughs_lowercase
72
+ @boroughs_lowercase ||= boroughs.map(&:downcase)
73
+ end
74
+
75
+ def zip_codes
76
+ @zip_codes ||= all.collect(&:zip_code).compact.uniq.sort
77
+ end
78
+
79
+ def num_markets_in_borough(b)
80
+ find_by_borough(b).count
81
+ end
82
+
83
+ def destroy_all!
84
+ @all = []
85
+ end
64
86
  end
65
- end
66
87
 
67
- def self.list_boroughs
68
- self.all.collect { |market| market.borough }.compact.uniq.sort
69
- end
88
+ private
70
89
 
71
- def self.list_zipcodes
72
- self.all.collect { |market| market.zipcode }.compact.uniq.sort
73
- end
90
+ def set_website_from_additional_info_raw
91
+ return if additional_info_raw.to_s.empty?
92
+ url = additional_info_raw.match %r{http(:|\w|\/|\.|\&|\=|\?|\_)*<}
93
+ return if url.nil?
94
+ @website = url[0].chomp('<').chomp('.')
95
+ end
74
96
 
75
- def self.num_markets_in_borough(b)
76
- self.find_by_borough(b).count
97
+ def set_additional_info
98
+ return if additional_info_raw.to_s.empty?
99
+ @additional_info ||= additional_info_raw.match(/\A[^<]*/).to_s
100
+ end
77
101
  end
78
102
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NYCFarmersMarkets
4
+ VERSION = '2.0.0'
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyc-farmers-markets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin McCormack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2018-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -16,83 +16,111 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.7'
19
+ version: '0.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.7'
26
+ version: '0.8'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.8'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.8'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: open-uri-cached
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.0.5
47
+ version: '0.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.0.5
54
+ version: '0.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: pry
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '3.5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '3.5'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.47'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.47'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ">="
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '3.4'
89
+ version: '1.10'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ">="
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
81
109
  - !ruby/object:Gem::Version
82
- version: '3.4'
110
+ version: '3.5'
83
111
  description: A Ruby gem for the NYC Farmers Markets API with a CLI
84
- email: wittyman37@yahoo.com
112
+ email: HarlemSquirrel@gmail.com
85
113
  executables:
86
114
  - nyc-farmers-markets
87
115
  extensions: []
88
116
  extra_rdoc_files: []
89
117
  files:
90
118
  - bin/nyc-farmers-markets
91
- - config/environment.rb
92
119
  - lib/nyc_farmers_markets.rb
93
120
  - lib/nyc_farmers_markets/cli.rb
94
121
  - lib/nyc_farmers_markets/get_markets.rb
95
122
  - lib/nyc_farmers_markets/market.rb
123
+ - lib/nyc_farmers_markets/version.rb
96
124
  homepage: https://github.com/HarlemSquirrel/nyc-farmers-markets-cli-gem
97
125
  licenses:
98
126
  - MIT
@@ -112,8 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
140
  - !ruby/object:Gem::Version
113
141
  version: '0'
114
142
  requirements: []
115
- rubyforge_project:
116
- rubygems_version: 2.4.5.1
143
+ rubygems_version: 3.0.1
117
144
  signing_key:
118
145
  specification_version: 4
119
146
  summary: Wrapper and CLI the NYC Farmers Markets API
@@ -1,10 +0,0 @@
1
- require 'colorize'
2
- require 'json'
3
- #require 'open-uri'
4
- require 'open-uri/cached'
5
- require 'pry'
6
-
7
-
8
- require_relative '../lib/nyc_farmers_markets/cli'
9
- require_relative '../lib/nyc_farmers_markets/get_markets'
10
- require_relative '../lib/nyc_farmers_markets/market'