lonely_planet_tours 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 677cc5a7fdc5727128a5ff48e646da82ca1093ac
4
+ data.tar.gz: dc1b8c204fd73ebe75535688acfe4c6e67676e14
5
+ SHA512:
6
+ metadata.gz: b20b00e93b82abf3b256adc8db5c56a51fd0210693b57b7e1f74a3a8f5269a697beb62d0cefed6b02654c18c65fff3d20d4bf8d0a71671705798c20d78584802
7
+ data.tar.gz: f9da365db0e0be8e142eb8cfcf8661b2f88881a40d43fc92eeedaa3e07140c3e0f11ffb578d5031f348f02d3c2d5efa860645a5b2d913571e6e81814e5b7c45b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - "2.2.3"
5
+ - "2.1.0"
6
+ - "1.9.3"
7
+ - jruby-head
8
+ - jruby-19mode
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: jruby-head
12
+ branches:
13
+ only:
14
+ - master
15
+ # uncomment this line if your project needs to run something other than `rake`:
16
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Gems required
4
+ gem 'oga'
5
+ gem 'json'
6
+ gem 'minitest'
7
+ gem 'webmock'
8
+ gem 'vcr'
9
+
10
+ group :setup do
11
+ gem 'rake'
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2015 ZhongMeiZhou https://github.com/ZhongMeiZhou
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+
2
+
3
+ # lonely_planet_tours [![Gem Version](https://badge.fury.io/rb/taiwan_tours.svg)](https://badge.fury.io/rb/taiwan_tours) [![Build Status](https://travis-ci.org/ZhongMeiZhou/scraper_project.svg)](https://travis-ci.org/ZhongMeiZhou/scraper_project)
4
+
5
+ The web scraper service we built provides listings of tour packages from Lonelyplanet based on a specified country. We have bundled this service into the lonely_planet_tours gem for you to use.
6
+
7
+ Note that we respect Lonelyplanet's 'robots.txt' file.
8
+
9
+
10
+ ## Usage
11
+
12
+ Install at the command line:
13
+
14
+ ```sh
15
+ $ gem install lonely_planet_tours
16
+ ```
17
+
18
+ or include it in your 'Gemfile' as:
19
+
20
+ ```ruby
21
+ gem lonely_planet_tours
22
+ ```
23
+
24
+ ## Try it yourself!
25
+ Run it from the command line as:
26
+
27
+ ```sh
28
+ $ lonely_planet_tours
29
+ ```
30
+
31
+ or include it in your code:
32
+
33
+ ```ruby
34
+ require 'lonely_planet_tours'
35
+ taiwan = LonelyPlanetScrape::LonelyPlanetTours.new(country)
36
+ tours = taiwan.tours
37
+ ```
38
+
39
+ ## Want to make improvements?
40
+
41
+ 1. Fork it ( https://github.com/ZhongMeiZhou/scraper_project )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => [:spec]
4
+
5
+ desc 'Run specs'
6
+ Rake::TestTask.new(name=:spec) do |t|
7
+ t.pattern = 'spec/*_spec.rb'
8
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json'
3
+ require '../lib/lonely_planet/lonelyplanet_scrap.rb'
4
+
5
+ begin
6
+ fail ArgumentError, "usage: tours [country]\n" if ARGV.count == 0
7
+ country = ARGV[0]
8
+
9
+ taiwan_tours = LonelyPlanetScrape::LonelyPlanetTours.new(country)
10
+ tour_arr = JSON.parse(taiwan_tours.tours)
11
+ puts "According to LonelyPlanet, these are the best tour packages in #{country}:"
12
+
13
+ tour_arr.each do |hash|
14
+ puts "- #{hash['title']} for $#{hash['price']}"
15
+ end
16
+
17
+ rescue => e
18
+ puts "Error occured - see details: #{e}"
19
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,50 @@
1
+ require 'oga'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ # Module defines LonelyPlanetTours class which handles scraping of lonelyplanet Taiwan tours page
6
+ module LonelyPlanetScrape
7
+ class LonelyPlanetTours
8
+ LONELYPLANET_URL = 'http://www.lonelyplanet.com'
9
+ TOUR_RELATIVE_DIR = 'tours'
10
+
11
+ TOUR_XPATH_CARD = "//article[contains(@class,'card')]"
12
+ CARD_IMGLINK_XPATH = ".//img[contains(@class,'card__figure__img')]/@src"
13
+ CARD_TITLE_XPATH = './/h1'
14
+ CARD_CONTENT_XPATH = ".//div[contains(@class,'card__content__desc')]//p"
15
+ CARD_LINK_XPATH = ".//div[contains(@class,'card__mask')]//a"
16
+ CARD_LOCATION_XPATH = ".//div[contains(@class,'card__footer__locale')]"
17
+ CARD_PRICE_CURRENCY_XPATH = ".//span[contains(@class,'js-currency')]"
18
+ CARD_PRICE_AMOUNT_XPATH = ".//span[contains(@class,'js-price')]"
19
+
20
+ def initialize(country)
21
+ parse_html(country)
22
+ end
23
+
24
+ def tours
25
+ @tours ||= extract_tours
26
+ end
27
+
28
+ private
29
+
30
+ def parse_html(country)
31
+ url = "#{LONELYPLANET_URL}/#{country}/#{TOUR_RELATIVE_DIR}"
32
+ @document = Oga.parse_html(open(url))
33
+ end
34
+
35
+ def extract_tours
36
+ result = []
37
+ @document.xpath(TOUR_XPATH_CARD).map do |card|
38
+ element = {}
39
+ element['img'] = card.xpath(CARD_IMGLINK_XPATH).text
40
+ element['title'] = card.xpath(CARD_TITLE_XPATH).text.strip
41
+ element['content'] = card.xpath(CARD_CONTENT_XPATH).text.strip
42
+ element['location'] = card.xpath(CARD_LOCATION_XPATH).text
43
+ element['price_currency'] = card.xpath(CARD_PRICE_CURRENCY_XPATH).text
44
+ element['price'] = card.xpath(CARD_PRICE_AMOUNT_XPATH).text
45
+ result << element
46
+ end
47
+ result.to_json
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # Versioning
2
+ module LonelyPlanetScrape
3
+ VERSION = '1.0.0'
4
+ DATE = '2015-10-21'
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'lonely_planet/lonelyplanet_scrap.rb'
2
+ require 'lonely_planet/version.rb'
@@ -0,0 +1,52 @@
1
+ require 'oga'
2
+ require 'open-uri'
3
+ require 'json'
4
+ require './lib/lonely_planet/lonelyplanet_scrap'
5
+ # Test class practice for handling errors and checking for HTML structure changes
6
+ # can be used to test functionality before implementing it in Scraper Class
7
+ class LonelyPlanetToursTest < LonelyPlanetScrape::LonelyPlanetTours
8
+ # override super class initialize function
9
+ def initialize(country)
10
+ test_parse_html(country)
11
+ end
12
+
13
+ def test_tour
14
+ @tours ||= test_html_extraction
15
+ end
16
+
17
+ private
18
+
19
+ # test connection to external site
20
+ def test_parse_html(country)
21
+ parse_html(country)
22
+ rescue OpenURI::HTTPError => e
23
+ puts "HTTP request error: #{e}"
24
+ end
25
+
26
+ # test scraping service and also test for changes in structure of elements being traversed
27
+ def test_html_extraction
28
+ result = []
29
+ fail 'OOPS, root article may have been changed or removed' if @document.xpath(TOUR_XPATH_CARD).text.empty?
30
+ fail 'Title h1 tag may have been changed or removed' if @document.xpath(CARD_TITLE_XPATH).text.empty?
31
+ fail 'Price span tag may have been changed or removed' if @document.xpath(CARD_PRICE_AMOUNT_XPATH).text.empty?
32
+ fail 'Content div tag may have been changed or removed' if @document.xpath(CARD_CONTENT_XPATH).text.empty?
33
+
34
+ @document.xpath(TOUR_XPATH_CARD).map do |card|
35
+ element = {}
36
+ element['img'] = card.xpath(CARD_IMGLINK_XPATH).text
37
+ element['title'] = card.xpath(CARD_TITLE_XPATH).text.strip
38
+ element['content'] = card.xpath(CARD_CONTENT_XPATH).text.strip
39
+ element['location'] = card.xpath(CARD_LOCATION_XPATH).text
40
+ element['price_currency'] = card.xpath(CARD_PRICE_CURRENCY_XPATH).text
41
+ element['price'] = card.xpath(CARD_PRICE_AMOUNT_XPATH).text
42
+ result << element
43
+ end
44
+ result.to_json
45
+ rescue StandardError => e
46
+ puts e.message
47
+ puts e.backtrace.inspect
48
+ end
49
+ end
50
+
51
+ test_run = LonelyPlanetToursTest.new('belize')
52
+ puts test_run.test_tour
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'lonely_planet/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'lonely_planet_tours'
6
+ s.version = LonelyPlanetScrape::VERSION
7
+ s.date = LonelyPlanetScrape::DATE
8
+ s.executables << 'lonely_planet_tours'
9
+ s.summary = 'The lonelyplanet web scraper service traverses details of tour packages based on specified country'
10
+ s.description = 'Get listings of tour packages from Lonelyplanet'
11
+ s.authors = ['Bayardo Salgado', 'Cesar Ordoñez','Eduardo Salazar','Nicole Weatherburne']
12
+ s.email = ['bayardo_salgado@yahoo.com','cesar','esalazar922@gmail.com','nikkiweat@gmail.com']
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files spec/*`.split("\n")
15
+ s.homepage = 'https://github.com/ZhongMeiZhou/scraper_project'
16
+ s.license = 'MIT'
17
+
18
+ s.add_development_dependency 'minitest'
19
+ s.add_development_dependency 'vcr'
20
+ s.add_development_dependency 'webmock'
21
+ s.add_runtime_dependency 'oga'
22
+ s.add_runtime_dependency 'json'
23
+ end