Class: NYCFarmersMarkets::Market

Inherits:
Object
  • Object
show all
Defined in:
lib/nyc_farmers_markets/market.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, additional_info: nil, street_address: nil, borough: nil, state: "NY", zipcode: nil, website: nil) ⇒ Market

Returns a new instance of Market.



6
7
8
9
10
11
12
13
# File 'lib/nyc_farmers_markets/market.rb', line 6

def initialize(name: nil, additional_info: nil, street_address: nil, borough: nil, state: "NY", zipcode: nil, website: nil)
  @name = name
  @additional_info = additional_info
  @street_address = street_address
  @borough = borough
  @state = state
  @zipcode = zipcode
end

Instance Attribute Details

#additional_infoObject

Returns the value of attribute additional_info.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def additional_info
  @additional_info
end

#boroughObject

Returns the value of attribute borough.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def borough
  @borough
end

#latitudeObject

Returns the value of attribute latitude.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def name
  @name
end

#stateObject

Returns the value of attribute state.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def state
  @state
end

#street_addressObject

Returns the value of attribute street_address.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def street_address
  @street_address
end

#websiteObject

Returns the value of attribute website.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def website
  @website
end

#zipcodeObject

Returns the value of attribute zipcode.



2
3
4
# File 'lib/nyc_farmers_markets/market.rb', line 2

def zipcode
  @zipcode
end

Class Method Details

.allObject



15
16
17
# File 'lib/nyc_farmers_markets/market.rb', line 15

def self.all
  @@all
end

.create_from_hash(h) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nyc_farmers_markets/market.rb', line 23

def self.create_from_hash(h)
  market = self.new(
    name: h["facilityname"],
    additional_info: h["facilityaddinfo"],
    street_address: h["facilitystreetname"],
    borough: h["facilitycity"],
    state: h["facilitystate"],
    zipcode: h["facilityzipcode"]
  )

  market.set_website_from_additional_info
  market.remove_html
  market.save
  market
end

.find_by_borough(b) ⇒ Object



55
56
57
58
59
# File 'lib/nyc_farmers_markets/market.rb', line 55

def self.find_by_borough(b)
  self.all.select do |market|
    market if market.borough != nil && market.borough.downcase == b.downcase
  end
end

.find_by_zipcode(z) ⇒ Object



61
62
63
64
65
# File 'lib/nyc_farmers_markets/market.rb', line 61

def self.find_by_zipcode(z)
  self.all.select do |market|
    market if market.zipcode == z
  end
end

.list_boroughsObject



67
68
69
# File 'lib/nyc_farmers_markets/market.rb', line 67

def self.list_boroughs
  self.all.collect { |market| market.borough }.compact.uniq.sort
end

.list_zipcodesObject



71
72
73
# File 'lib/nyc_farmers_markets/market.rb', line 71

def self.list_zipcodes
  self.all.collect { |market| market.zipcode }.compact.uniq.sort
end

.num_markets_in_borough(b) ⇒ Object



75
76
77
# File 'lib/nyc_farmers_markets/market.rb', line 75

def self.num_markets_in_borough(b)
  self.find_by_borough(b).count
end

Instance Method Details

#remove_htmlObject



46
47
48
49
50
51
52
53
# File 'lib/nyc_farmers_markets/market.rb', line 46

def remove_html
  if @additional_info != nil && @additional_info.include?("<")
    x = @additional_info.index("<")
    y = @additional_info.length - 1
    @additional_info.slice!(x..y)
  end

end

#saveObject



19
20
21
# File 'lib/nyc_farmers_markets/market.rb', line 19

def save
  @@all << self
end

#set_website_from_additional_infoObject



39
40
41
42
43
44
# File 'lib/nyc_farmers_markets/market.rb', line 39

def set_website_from_additional_info
  if @additional_info != nil
    url_from_info = @additional_info.match(/http:(\w|\/|\.|\&|\=|\?|\_)*</)
    @website = url_from_info[0].chomp("<").chomp(".") unless url_from_info == nil
  end
end