Sha256: 1e4428464916fd89393ef0d7c8c1dda1cb0ffa96dd4e2ab987756bae87dd8ac5

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

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

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

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

      title
    end

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

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

    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

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

    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
barnie-0.3.2 lib/barnie/helpers.rb
barnie-0.3.1 lib/barnie/helpers.rb
barnie-0.3.0 lib/barnie/helpers.rb
barnie-0.2.0 lib/barnie/helpers.rb