Class: EhbrsRubyUtils::Airbnb::Parsers::Page

Inherits:
Aranha::Parsers::Html::ItemList
  • Object
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

Parameters:

  • value (String)
  • suffixes (Enumerable<String>)

Returns:

  • (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

#accommodationsEnumerable<Hash>

Returns:

  • (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

Parameters:

  • prices (Enumerable<Float, nil>)

Returns:

  • (Float)


71
72
73
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 71

def build_price(*prices)
  prices.reject(&:blank?).min
end

#dataObject



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_countInteger

Returns:

  • (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_xpathString

Returns:

  • (String)


60
61
62
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 60

def items_xpath
  ITEMS_XPATH
end

#next_page_hrefString

Returns:

  • (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

Parameters:

  • text (String)

Returns:

  • (Hash)


77
78
79
80
81
# File 'lib/ehbrs_ruby_utils/airbnb/parsers/page.rb', line 77

def parse_review(text)
  return REVIEW_STRUCT.new(0, 0).to_h if REVIEW_TEXTS_ZERO.include?(text)

  REVIEW_PARSER.parse!(text).to_h
end

#parse_type_address(text) ⇒ Hash

Parameters:

  • text (String)

Returns:

  • (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