holden-hostelify 0.2.0 → 0.2.1
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.
- data/Manifest +1 -1
- data/Rakefile +1 -1
- data/hostelify.gemspec +4 -4
- data/lib/hostel.rb +0 -1
- data/lib/hostel/hostel.rb +52 -2
- data/lib/hostel/hostelbookers.rb +25 -9
- data/lib/hostel/hostelworld.rb +15 -13
- data/spec/hb_find_hostels.spec +1 -1
- data/spec/hw_find_by_hostel.spec +1 -1
- data/spec/hw_find_hostels.spec +6 -6
- metadata +5 -6
- data/lib/hostel/hostel_available.rb +0 -11
data/Manifest
CHANGED
data/Rakefile
CHANGED
data/hostelify.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{hostelify}
|
5
|
-
s.version = "0.2.
|
5
|
+
s.version = "0.2.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Holden Thomas"]
|
9
|
-
s.date = %q{2009-09-
|
9
|
+
s.date = %q{2009-09-10}
|
10
10
|
s.description = %q{Simple Hostel Webscrapper.}
|
11
11
|
s.email = %q{holden.thomas@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["README.rdoc", "lib/hostel.rb", "lib/hostel/gomio.rb", "lib/hostel/hostel.rb", "lib/hostel/
|
13
|
-
s.files = ["Manifest", "README.rdoc", "Rakefile", "
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/hostel.rb", "lib/hostel/gomio.rb", "lib/hostel/hostel.rb", "lib/hostel/hostelbookers.rb", "lib/hostel/hostelworld.rb", "lib/test.rb"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "hostelify.gemspec", "lib/hostel.rb", "lib/hostel/gomio.rb", "lib/hostel/hostel.rb", "lib/hostel/hostelbookers.rb", "lib/hostel/hostelworld.rb", "lib/test.rb", "spec/_helper.rb", "spec/hb_find_by_hostel.spec", "spec/hb_find_hostels.spec", "spec/hw_find_by_hostel.spec", "spec/hw_find_hostels.spec"]
|
14
14
|
s.has_rdoc = true
|
15
15
|
s.homepage = %q{http://github.com/holden/hostelify}
|
16
16
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Hostelify", "--main", "README.rdoc"]
|
data/lib/hostel.rb
CHANGED
data/lib/hostel/hostel.rb
CHANGED
@@ -1,11 +1,61 @@
|
|
1
1
|
class Hostel
|
2
|
-
attr_accessor :hostel_id, :name, :address, :description, :facilities, :ratings, :directions, :geo, :images, :video, :availability
|
2
|
+
attr_accessor :hostel_id, :name, :address, :description, :facilities, :ratings, :directions, :geo, :images, :video, :availability
|
3
|
+
attr_accessor :rating, :dorm, :single, :unavailable
|
3
4
|
|
4
5
|
def initialize(options = {})
|
5
6
|
options.each {
|
6
7
|
|k,v|
|
7
8
|
self.send( "#{k.to_s}=".intern, v)
|
8
9
|
}
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class HostelCollection < Array
|
15
|
+
# This collection does everything an Array does, plus
|
16
|
+
# you can add utility methods like names.
|
17
|
+
|
18
|
+
def ids
|
19
|
+
collect do |i|
|
20
|
+
i.hostel_id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def names
|
25
|
+
collect do |i|
|
26
|
+
i.name
|
9
27
|
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def descs
|
31
|
+
collect do |i|
|
32
|
+
i.description
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class HostelAvailable
|
39
|
+
attr_accessor :name, :price, :spots, :bookdate
|
40
|
+
|
41
|
+
def initialize(name, price, spots, bookdate)
|
42
|
+
@name = name
|
43
|
+
@price = price
|
44
|
+
@spots = spots
|
45
|
+
@bookdate = bookdate
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
module Retryable
|
51
|
+
extend self
|
52
|
+
|
53
|
+
def try times = 1, options = {}, &block
|
54
|
+
val = yield
|
55
|
+
rescue options[:on] || Exception
|
56
|
+
retry if (times -= 1) > 0
|
57
|
+
else
|
58
|
+
val
|
59
|
+
end
|
60
|
+
end
|
10
61
|
|
11
|
-
end
|
data/lib/hostel/hostelbookers.rb
CHANGED
@@ -18,11 +18,14 @@ class Hostelbookers
|
|
18
18
|
date = Date.strptime(options[:date])
|
19
19
|
data = setSearch(url,options[:date],options[:no_days])
|
20
20
|
else
|
21
|
-
|
21
|
+
Retryable.try 3 do
|
22
|
+
data = Hpricot(open(url))
|
23
|
+
end
|
22
24
|
end
|
23
25
|
|
24
26
|
data = data.search("//div[@id='propertyResultsList']")
|
25
|
-
|
27
|
+
#@results = []
|
28
|
+
@results = HostelCollection.new
|
26
29
|
#coder = HTMLEntities.new
|
27
30
|
(data/"tr.propertyRow").each do |row|
|
28
31
|
name = row.at("a.propertyTitle").inner_text
|
@@ -34,7 +37,8 @@ class Hostelbookers
|
|
34
37
|
single = row.at("td.private/text()")
|
35
38
|
hb_id = url.match(/[\d]{2,5}.$/).to_s.to_i
|
36
39
|
|
37
|
-
|
40
|
+
#@results << Hostel.new(:hostel_id => hb_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single)
|
41
|
+
@results << Hostel.new(:hostel_id => hb_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single)
|
38
42
|
end
|
39
43
|
return @results
|
40
44
|
end
|
@@ -51,7 +55,9 @@ class Hostelbookers
|
|
51
55
|
options = @default_options.merge(options)
|
52
56
|
data = setSearch_id(url,options[:date],options[:no_days])
|
53
57
|
else
|
54
|
-
|
58
|
+
Retryable.try 3 do
|
59
|
+
data = Hpricot(open(url))
|
60
|
+
end
|
55
61
|
end
|
56
62
|
|
57
63
|
hostel.hostel_id = id
|
@@ -119,7 +125,7 @@ class Hostelbookers
|
|
119
125
|
hostel.availability = @availables
|
120
126
|
end
|
121
127
|
|
122
|
-
hostel
|
128
|
+
return hostel
|
123
129
|
end
|
124
130
|
|
125
131
|
def self.setSearch(url,date,no_days)
|
@@ -129,12 +135,18 @@ class Hostelbookers
|
|
129
135
|
form = page.form_with(:name => 'searchForm') # => WWW::Mechanize::Form
|
130
136
|
form.field_with(:name => 'intnights').options[no_days.to_i-1].select
|
131
137
|
form.dtearrival = date #d/m/y
|
132
|
-
|
138
|
+
|
139
|
+
Retryable.try 3 do
|
140
|
+
page = agent.submit(form)
|
141
|
+
end
|
133
142
|
|
134
143
|
#to dollars!
|
135
144
|
form = page.forms[0]
|
136
145
|
form.field_with(:name => 'strSelectedCurrencyCode').options[5].select
|
137
|
-
|
146
|
+
|
147
|
+
Retryable.try 3 do
|
148
|
+
page = agent.submit(form)
|
149
|
+
end
|
138
150
|
|
139
151
|
data = page.search('//div[@id="content"]')
|
140
152
|
|
@@ -148,14 +160,18 @@ class Hostelbookers
|
|
148
160
|
form = page.form_with(:name => 'frmCheckAvailBook') # => WWW::Mechanize::Form
|
149
161
|
form.field_with(:name => 'intNights').options[no_days.to_i-1].select
|
150
162
|
form.dteArrival = date #d/m/y
|
151
|
-
page = agent.submit(form)
|
152
163
|
|
164
|
+
Retryable.try 3 do
|
165
|
+
page = agent.submit(form)
|
166
|
+
end
|
153
167
|
#change currency to dollars
|
154
168
|
form = page.forms[1]
|
155
169
|
#puts form.name
|
156
170
|
form.field_with(:name => 'strSelectedCurrencyCode').options[5].select
|
157
|
-
page = agent.submit(form)
|
158
171
|
|
172
|
+
Retryable.try 3 do
|
173
|
+
page = agent.submit(form)
|
174
|
+
end
|
159
175
|
data = page.search('//div[@id="content"]')
|
160
176
|
|
161
177
|
return data
|
data/lib/hostel/hostelworld.rb
CHANGED
@@ -14,7 +14,9 @@ class Hostelworld
|
|
14
14
|
def self.parse_html(url)
|
15
15
|
f = open(url)
|
16
16
|
f.rewind
|
17
|
-
|
17
|
+
Retryable.try 3 do
|
18
|
+
data = Hpricot(Iconv.conv('utf-8', f.charset, f.readlines.join("\n")))
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
22
|
def self.find_hostel_by_id(options)
|
@@ -105,7 +107,7 @@ class Hostelworld
|
|
105
107
|
end
|
106
108
|
|
107
109
|
data = data.search("//div[@id='content']")
|
108
|
-
@results =
|
110
|
+
@results = HostelCollection.new
|
109
111
|
|
110
112
|
(data/"div.hostelListing").each do |row|
|
111
113
|
name = row.at("h3").inner_text
|
@@ -116,22 +118,16 @@ class Hostelworld
|
|
116
118
|
type = row.at("div.hostelListingImage/span").inner_text
|
117
119
|
hostel_id = url.match(/[\d]*$/).to_s
|
118
120
|
|
119
|
-
#@main_values = { :hostel_id => hostel_id, :name => name, :desc => desc, :type => type, :rating => rating }
|
120
|
-
#@extra = {}
|
121
|
-
|
122
121
|
if options[:date]
|
123
122
|
#price_USD = row.at("span.blueBeds").inner_text #need to fix float
|
124
123
|
dorm = (row.at("p.hostelListingRate/span.blueBeds/text()")).to_s.gsub(/[A-Z$]*/,'')
|
125
124
|
single = row.at("p.hostelListingPrivateRate/span.blueBeds/text()").to_s.gsub(/[A-Z$]*/,'')
|
126
125
|
available = row/"ul.hostelListingDates/li.noAvail/text()"
|
127
126
|
available = available.to_a.join(',').split(',')
|
128
|
-
|
129
|
-
#@extra = { :dorm => dorm, :single => single, :unavailable => available }
|
130
|
-
@results << Hostel.new(:hostel_id => hostel_id, :name => name, :description => desc, :ratings => rating, :price => dorm, :availability => available)
|
127
|
+
@results << Hostel.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single, :unavailable => available)
|
131
128
|
else
|
132
|
-
@results << Hostel.new(:hostel_id => hostel_id, :name => name, :description => desc, :
|
129
|
+
@results << Hostel.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => rating)
|
133
130
|
end
|
134
|
-
#@results << @main_values.merge(@extra)
|
135
131
|
end
|
136
132
|
return @results
|
137
133
|
end
|
@@ -150,8 +146,11 @@ class Hostelworld
|
|
150
146
|
|
151
147
|
#the form name
|
152
148
|
form = page.forms.first # => WWW::Mechanize::Form
|
153
|
-
|
154
|
-
|
149
|
+
|
150
|
+
Retryable.try 3 do
|
151
|
+
page = agent.submit(form)
|
152
|
+
end
|
153
|
+
|
155
154
|
#form must be submitted twice because the people writing hostelworld are retards
|
156
155
|
form = page.forms.first # => WWW::Mechanize::Form
|
157
156
|
form.field_with(:name => 'selMonth').options[month-1].select
|
@@ -161,7 +160,10 @@ class Hostelworld
|
|
161
160
|
form.field_with(:name => 'Persons').options[no_ppl.to_i-1].select
|
162
161
|
form.field_with(:name => 'Currency').options[4].select #US Currency
|
163
162
|
|
164
|
-
|
163
|
+
Retryable.try 3 do
|
164
|
+
page = agent.submit(form)
|
165
|
+
end
|
166
|
+
|
165
167
|
data = page.search("//div[@id='content']")
|
166
168
|
|
167
169
|
return data
|
data/spec/hb_find_hostels.spec
CHANGED
data/spec/hw_find_by_hostel.spec
CHANGED
@@ -61,7 +61,7 @@ end
|
|
61
61
|
|
62
62
|
describe "with dates to get availabilty and verify output!" do
|
63
63
|
before(:all) do
|
64
|
-
@h = Hostelworld.find_hostel_by_id(:id => 20763, :date => (Date.today+
|
64
|
+
@h = Hostelworld.find_hostel_by_id(:id => 20763, :date => (Date.today+20).to_s)
|
65
65
|
end
|
66
66
|
|
67
67
|
it "get first availability and check it merit" do
|
data/spec/hw_find_hostels.spec
CHANGED
@@ -16,11 +16,11 @@ describe "finds list of hostels" do
|
|
16
16
|
|
17
17
|
|
18
18
|
it "rating should be high for first choices" do
|
19
|
-
@h.first.
|
19
|
+
@h.first.rating.to_i.should be > 50
|
20
20
|
end
|
21
21
|
|
22
22
|
it "desc should have a certain length <" do
|
23
|
-
@h.first.description.length.should be >
|
23
|
+
@h.first.description.length.should be > 80
|
24
24
|
end
|
25
25
|
|
26
26
|
it "has a hostel number" do
|
@@ -36,7 +36,7 @@ describe "find hostels with dates" do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "rating should be high for first choices" do
|
39
|
-
@h.first.
|
39
|
+
@h.first.rating.to_i.should be > 50
|
40
40
|
end
|
41
41
|
|
42
42
|
it "desc should have a certain length <" do
|
@@ -48,15 +48,15 @@ describe "find hostels with dates" do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
it "has dorm rooms for greater than $5" do
|
51
|
-
@h.first.
|
51
|
+
@h.first.dorm.to_i.should be > 5
|
52
52
|
end
|
53
53
|
|
54
54
|
it "has available rooms!" do
|
55
|
-
@h.first.
|
55
|
+
@h.first.unavailable.first.should be nil
|
56
56
|
end
|
57
57
|
|
58
58
|
it "has unavailable rooms!" do
|
59
|
-
@h.last.
|
59
|
+
@h.last.unavailable.first.should_not be nil
|
60
60
|
end
|
61
61
|
|
62
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: holden-hostelify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Holden Thomas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-10 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,7 +24,6 @@ extra_rdoc_files:
|
|
24
24
|
- lib/hostel.rb
|
25
25
|
- lib/hostel/gomio.rb
|
26
26
|
- lib/hostel/hostel.rb
|
27
|
-
- lib/hostel/hostel_available.rb
|
28
27
|
- lib/hostel/hostelbookers.rb
|
29
28
|
- lib/hostel/hostelworld.rb
|
30
29
|
- lib/test.rb
|
@@ -32,10 +31,10 @@ files:
|
|
32
31
|
- Manifest
|
33
32
|
- README.rdoc
|
34
33
|
- Rakefile
|
34
|
+
- hostelify.gemspec
|
35
35
|
- lib/hostel.rb
|
36
36
|
- lib/hostel/gomio.rb
|
37
37
|
- lib/hostel/hostel.rb
|
38
|
-
- lib/hostel/hostel_available.rb
|
39
38
|
- lib/hostel/hostelbookers.rb
|
40
39
|
- lib/hostel/hostelworld.rb
|
41
40
|
- lib/test.rb
|
@@ -44,9 +43,9 @@ files:
|
|
44
43
|
- spec/hb_find_hostels.spec
|
45
44
|
- spec/hw_find_by_hostel.spec
|
46
45
|
- spec/hw_find_hostels.spec
|
47
|
-
- hostelify.gemspec
|
48
46
|
has_rdoc: true
|
49
47
|
homepage: http://github.com/holden/hostelify
|
48
|
+
licenses:
|
50
49
|
post_install_message:
|
51
50
|
rdoc_options:
|
52
51
|
- --line-numbers
|
@@ -72,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
71
|
requirements: []
|
73
72
|
|
74
73
|
rubyforge_project: hostelify
|
75
|
-
rubygems_version: 1.
|
74
|
+
rubygems_version: 1.3.5
|
76
75
|
signing_key:
|
77
76
|
specification_version: 2
|
78
77
|
summary: Simple Hostel Webscrapper.
|