Class: OsuCtlScraper::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/osu-ctl-scraper/book.rb

Constant Summary collapse

RESOURCE =
"/Faculty/GetTextbooks/"

Class Method Summary collapse

Class Method Details

.find_by(subject_code, year, term) ⇒ Array<Hash>

Returns an array of Books

Parameters:

  • subject_code (String)
  • year (Integer)
  • term (Symbol)

    Must be one of: “winter”, “spring”, “summer”, or “fall”

Returns:

  • (Array<Hash>)


11
12
13
14
15
# File 'lib/osu-ctl-scraper/book.rb', line 11

def self.find_by(subject_code, year, term)
  res = get_html(subject_code, year, term)
  html = format_response(res.body)
  process_html(subject_code, html)
end

.form_params(subject_code, year, term) ⇒ Hash

Parameters:

  • subject_code (String)
  • year (Integer)
  • term (Symbol)

    Must be one of: “winter”, “spring”, “summer”, or “fall”

Returns:

  • (Hash)


31
32
33
34
35
36
# File 'lib/osu-ctl-scraper/book.rb', line 31

def self.form_params(subject_code, year, term)
  {
    termcode: termcode(year, term),
    dept: subject_code
  }
end

.format_response(body) ⇒ String

Marshals the server response into a format understandable by Nokogiri

Parameters:

  • body (String)

Returns:

  • (String)


52
53
54
55
56
57
# File 'lib/osu-ctl-scraper/book.rb', line 52

def self.format_response(body)
  body.
    gsub('\u003c', '<').
    gsub('\u003e', '>').
    gsub('\\"', '"')
end

.get_html(subject_code, year, term) ⇒ String

Parameters:

  • subject_code (String)
  • year (Integer)
  • term (Symbol)

    Must be one of: “winter”, “spring”, “summer”, or “fall”

Returns:

  • (String)


21
22
23
24
25
# File 'lib/osu-ctl-scraper/book.rb', line 21

def self.get_html(subject_code, year, term)
  url = URI.parse("#{ENDPOINT}#{RESOURCE}")
  params = form_params(subject_code, year, term)
  res = Net::HTTP.post_form(url, params)
end

.process_html(subject_code, html) ⇒ Array<Hash>

Parameters:

  • subject_code (String)
  • html (String)

Returns:

  • (Array<Hash>)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/osu-ctl-scraper/book.rb', line 62

def self.process_html(subject_code, html)
  books = []
  ng = Nokogiri::HTML(html)
  ng.css("tr:not(:first-child)").each do |row|
    book = process_row(row)
    unless book.nil?
      book[:subject_code] = subject_code
      books << book
    end
  end
  books
end

.process_row(row) ⇒ Hash?

Parameters:

  • row (Nokogiri::XML::Element)

Returns:

  • (Hash, nil)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/osu-ctl-scraper/book.rb', line 77

def self.process_row(row)
  book = {
    course:      row.css("td:nth-child(1)").text.strip,
    section:     row.css("td:nth-child(2)").text.strip,
    instructor:  row.css("td:nth-child(3)").text.strip,
    title:       row.css("td:nth-child(4)").text.strip,
    edition:     row.css("td:nth-child(5)").text.strip,
    author:      row.css("td:nth-child(6)").text.strip,
    isbn:        row.css("td:nth-child(7)").text.strip,
    publisher:   row.css("td:nth-child(8)").text.strip,
    requirement: row.css("td:nth-child(9)").text.strip,
    sku:         row.css("td:nth-child(10)").text.strip,
    comments:    row.css("td:nth-child(11)").text.strip,
    req_date:    row.css("td:nth-child(12)").text.strip
  }

  # Only return the book if is has an ISBN
  book[:isbn].empty? ? nil : book
end

.termcode(year, term) ⇒ String

Formats the year and term into the expected server request format

Parameters:

  • year (Integer)
  • term (Symbol)

    Must be one of: “winter”, “spring”, “summer”, or “fall”

Returns:

  • (String)


43
44
45
46
# File 'lib/osu-ctl-scraper/book.rb', line 43

def self.termcode(year, term)
  terms = { winter: "A", spring: "B", summer: "C", fall: "D" }
  "#{year}#{terms[term]}"
end