Class: EhbrsRubyUtils::Airbnb::Parsers::Page
- Inherits:
-
Aranha::Parsers::Html::ItemList
- Object
- Aranha::Parsers::Html::ItemList
- EhbrsRubyUtils::Airbnb::Parsers::Page
show all
- Defined in:
- lib/ehbrs_ruby_utils/airbnb/parsers/page.rb
Defined Under Namespace
Classes: REVIEW_STRUCT, TYPE_ADDRESS_STRUCT
Constant Summary
collapse
- DECLARED_COUNT_XPATH =
'//h1/span[1]/text()'
- ITEMS_XPATH =
'//*[@data-xray-jira-component="Guest: Listing Cards"]/div'
- NEXT_PAGE_HREF_XPATH =
'//a[@aria-label="Próximo"]/@href'
- REVIEW_PARSER =
/\A(\S+)\s*\((\d+)\)\z/.to_parser do |m|
{
review_score: m[1].gsub(',', '.').to_f, review_count: m[2].to_i
}
end
- REVIEW_TEXTS_ZERO =
['Novo', ''].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.testid_xpath(value, *suffixes) ⇒ String
11
12
13
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 11
def testid_xpath(value, *suffixes)
([".//*[@data-testid='#{value}']"] + suffixes).join('/')
end
|
Instance Method Details
#accommodations ⇒ Enumerable<Hash>
46
47
48
49
50
51
52
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 46
def accommodations
items_data.map do |e|
e.merge(parse_review(e.fetch(:review_text)))
.merge(parse_type_address(e.fetch(:type_address)))
.merge(price: build_price(e.fetch(:price1), e.fetch(:price2)))
end
end
|
#build_price(*prices) ⇒ Float
71
72
73
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 71
def build_price(*prices)
prices.reject(&:blank?).min
end
|
#data ⇒ Object
39
40
41
42
43
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 39
def data
%i[accommodations declared_count next_page_href].inject({}) do |a, e|
a.merge({ e => send(e) })
end
end
|
#declared_count ⇒ Integer
55
56
57
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 55
def declared_count
node_parser.integer_optional_value(nokogiri, DECLARED_COUNT_XPATH)
end
|
#items_xpath ⇒ String
60
61
62
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 60
def items_xpath
ITEMS_XPATH
end
|
#next_page_href ⇒ String
65
66
67
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 65
def next_page_href
nokogiri.at_xpath(NEXT_PAGE_HREF_XPATH).if_present(&:text)
end
|
#parse_review(text) ⇒ Hash
#parse_type_address(text) ⇒ Hash
85
86
87
|
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 85
def parse_type_address(text)
TYPE_ADDRESS_STRUCT.new(*text.split('⋅').map(&:strip)).to_h
end
|