class Applephile::CraigsList

Attributes

doc[RW]
site_url[RW]

Public Class Methods

new() click to toggle source
# File lib/applephile/craigslist.rb, line 9
def initialize()
  @site_url = "https://www.craigslist.org/about/sites"
  @doc = Nokogiri::HTML(open(@site_url))  
end

Public Instance Methods

get_state_cities(state_name) click to toggle source
# File lib/applephile/craigslist.rb, line 19
def get_state_cities(state_name)
  #it returns an array of cities belonging to a particular state
  #1. find the state first.
  #2. then collect the state's city array, return a flatten array so only cities can be used
  #to be displayed in CLI
  state_cities_array = []
  states_cities_links.each do |states, cities_links|
    if states.to_s == state_name 
      #collect only cities
      state_cities_array = cities_links.collect { |city| city.keys }
      break
    end
  end
  state_cities_array.flatten!  

end
get_states_names() click to toggle source
# File lib/applephile/craigslist.rb, line 14
def get_states_names
  #it returns an array of states of the U.S., CLI will use to display them
  doc.css(".colmask").first.css("h4").collect { |st| st.text}  
end
scrape_by_city_url(city_url) click to toggle source
# File lib/applephile/craigslist.rb, line 53
def scrape_by_city_url(city_url)
  items_array_of_hashes = []
  #1. returns an array of hashes for every link (phone found) for a particular scrape criteria
  #2. build site link with searching criteria phones with pic, new, os
  #3. grab all phones that meet this search criteria.
  #4. traverse the collection, and grab each needed attribute one by one.
  #5. build hash, and save it to array.
  #6. return hash
  city_url = city_url + "search/moa?hasPic=1&condition=10&mobile_os=2"
  city_doc = Nokogiri::HTML(open(city_url))
  grab_all_phones = city_doc.css(".result-info")
    grab_all_phones.each do |phones|
       phone_url = phones.css("a")[0]["href"]
       phone_price = phones.css(".result-price").text.split("$")[1]
       phone_description = phones.css("a").text.split("\n")[0]
       items_array_of_hashes.push({:url => phone_url,
                                   :price => phone_price,
                                   :description => phone_description})
    end
    items_array_of_hashes
end

Private Instance Methods