Class: TitlePage::WWWClient

Inherits:
Object
  • Object
show all
Defined in:
lib/titlepage/wwwclient.rb

Overview

A helper class for grabbing ONIX files from titlepage.com

A valid login is required, see the site for information on applying for access.

You should be aware of any limits of query volume imposed by the provider - currently a maximum of 30 queries per minute is permitted.

Usage

TitlePage::WWWClient.open("username","password") do |tp|
  puts tp.get_onix_file("9780091835132")
end

Constant Summary collapse

TITLEPAGE_DOMAIN =
"www.titlepage.com"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(username, password) ⇒ Object

a convenience method to make queries to title page a little cleaner. This function essentially calls the login and logout functions for you automatically.

TitlePage::WWWClient.open("username","password") do |tp|
  result = tp.get_onix_file("9780091835132")
end


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/titlepage/wwwclient.rb', line 85

def self.open(username, password)

  tp = self.new

  begin
    tp.(username, password)

    yield(tp)

  ensure
    tp.logout
  end
end

Instance Method Details

#get_onix_file(isbn) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/titlepage/wwwclient.rb', line 23

def get_onix_file(isbn)
  isbn = RBook::ISBN.convert_to_isbn13(isbn)
  raise ArgumentError, 'Invalid ISBN supplied' if isbn.nil?

  headers = { 'Cookie' => @cookie }

   = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http|
    data = [
      "posted=yes",
      "quicksearch=#{isbn}",
      "qsrchby=isbn13",
        "detailed=Search"
    ].join("&")
    http.post('/results.php', data, headers)
  end
  regex = /onclick=\"bookPopUp\(\'(.+)\'\);\"/
    code = .body.match(regex)
  if code.nil?
    return nil
  else
    code = code[1]
  end
  onix_file = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http|
    data = [
      "download=Download",
      "rr=#{code}"
    ].join("&")
    http.post('/detail.php', data, headers)
  end
  return onix_file.body
end

#login(username, password) ⇒ Object

login to the titlepage website.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/titlepage/wwwclient.rb', line 56

def (username, password)
   = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http|
    data = [
      "usr=#{username}",
      "pwd=#{password}",
      "login=Login"
    ].join("&")
    headers = { 'Referer' => 'http://www.titlepage.com/index.php' }
    http.post('/index.php', data, headers)
  end
  @cookie = ['set-cookie']
end

#logoutObject

logout from the titlepage API



70
71
72
73
74
75
76
77
# File 'lib/titlepage/wwwclient.rb', line 70

def logout
  if @cookie
     = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http|
      http.get("/logout.php")
    end
    @cookie = nil
  end
end