Class: Applephile::CraigsList
- Inherits:
-
Object
- Object
- Applephile::CraigsList
- Defined in:
- lib/applephile/craigslist.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#site_url ⇒ Object
Returns the value of attribute site_url.
Instance Method Summary collapse
- #get_state_cities(state_name) ⇒ Object
- #get_states_names ⇒ Object
-
#initialize ⇒ CraigsList
constructor
A new instance of CraigsList.
- #return_city_link(state, city) ⇒ Object
- #scrape_by_city_url(city_url) ⇒ Object
Constructor Details
#initialize ⇒ CraigsList
Returns a new instance of CraigsList.
9 10 11 12 |
# File 'lib/applephile/craigslist.rb', line 9 def initialize() @site_url = "https://www.craigslist.org/about/sites" @doc = Nokogiri::HTML(open(@site_url)) end |
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
7 8 9 |
# File 'lib/applephile/craigslist.rb', line 7 def doc @doc end |
#site_url ⇒ Object
Returns the value of attribute site_url.
7 8 9 |
# File 'lib/applephile/craigslist.rb', line 7 def site_url @site_url end |
Instance Method Details
#get_state_cities(state_name) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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 ⇒ Object
14 15 16 17 |
# 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 |
#return_city_link(state, city) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/applephile/craigslist.rb', line 36 def return_city_link(state, city) #it returns the link of a particular city #1. find state hash #2. find the city #3. get city's link city_url = "" states_cities_links.find do |key, value| if key.to_s == state value.find do |city_hash| if city_hash.has_key?(city) city_url = city_hash[city] end end end end city_url end |
#scrape_by_city_url(city_url) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# 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 |