Class: RBook::TitlePage::WWWClient

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

Overview

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

Constant Summary collapse

TITLEPAGE_DOMAIN =
"www.titlepage.com"
@@uri =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWWWClient

Returns a new instance of WWWClient.



15
16
# File 'lib/rbook/titlepage/wwwclient.rb', line 15

def initialize
end

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.

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


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbook/titlepage/wwwclient.rb', line 79

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)


18
19
20
21
22
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
# File 'lib/rbook/titlepage/wwwclient.rb', line 18

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=ean",
      "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.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbook/titlepage/wwwclient.rb', line 51

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

#logoutObject

logout from the titlepage API



64
65
66
67
68
69
70
71
# File 'lib/rbook/titlepage/wwwclient.rb', line 64

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