Module: Barnie::Helpers

Included in:
Response
Defined in:
lib/barnie/helpers.rb

Instance Method Summary collapse

Instance Method Details

#extract_authors(html) ⇒ Object



47
48
49
50
51
# File 'lib/barnie/helpers.rb', line 47

def extract_authors(html)
  authors = html.search('.contrib a')
  authors = html.search('.ebookcontrib a') unless authors.any?
  authors.map { |author| sanitize_string(author.text) }
end

#extract_binding(string) ⇒ Object



53
54
55
# File 'lib/barnie/helpers.rb', line 53

def extract_binding(string)
  sanitize_string(string) unless string !~ /\S/
end

#extract_isbn(string) ⇒ Object



28
29
30
# File 'lib/barnie/helpers.rb', line 28

def extract_isbn(string)
  string.scan(/e\/\d{11,13}\//).first.gsub(/\D/, '').rjust(13, '0') unless string !~ /\S/
end


43
44
45
# File 'lib/barnie/helpers.rb', line 43

def extract_link(title)
  title.first.attributes['href'].value
end

#extract_price(string) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/barnie/helpers.rb', line 9

def extract_price(string)
  if string !~ /\S/
    0
  else
    string.gsub!(/\D/, '').to_i
  end
end

#extract_ships_in(string) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/barnie/helpers.rb', line 32

def extract_ships_in(string)
  return nil if string !~ /\S/
  string = sanitize_string(string)

  if string.include?('hours')
    string.scan(/\d{2}/).last.to_i
  elsif string.include?('days')
    string.scan(/\d{1}/).last.to_i * 24
  end
end

#extract_title(title) ⇒ Object



24
25
26
# File 'lib/barnie/helpers.rb', line 24

def extract_title(title)
  sanitize_string(title.text) if title.respond_to?("text")
end

#sanitize_string(string) ⇒ Object



3
4
5
6
7
# File 'lib/barnie/helpers.rb', line 3

def sanitize_string(string)
  string.gsub!(/^"|"$/, '')
  string.gsub!(/\\r|\\n/, '')
  string.strip
end

#title(html) ⇒ Object



17
18
19
20
21
22
# File 'lib/barnie/helpers.rb', line 17

def title(html)
  title = html.search('.title a')
  title = html.search('.ebooktitle a') unless title.any?

  title
end