Class: RBook::TitlePage::Client

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

Overview

a convenience class for accessing the SOAP API for www.titlepage.com. Uses boilerplate code generated by soap4r.

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

For a basic usage overview, check out RBook::TitlePage

Constant Summary collapse

@@uri =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver = nil) ⇒ Client

Optional driver parameter allows an alternative SOAP driver to the default to be specified. This is primarily for testing purposes and probably isn’t useful to anyone in the real world.



40
41
42
43
# File 'lib/rbook/titlepage/client.rb', line 40

def initialize(driver = nil)
  @driver = driver || TitleQueryPortType.new
  @token = nil
end

Class Method Details

.find(username, password, isbn, driver = nil) ⇒ Object

a convenience method to make single queries to title page a little cleaner.

result = RBook::TitlePage.find("username","password","9780091835132")
puts result.inspect


95
96
97
98
99
100
101
# File 'lib/rbook/titlepage/client.rb', line 95

def self.find(username, password, isbn, driver = nil)
  result = nil
  RBook::TitlePage::Client.open(username, password, driver) do |tp|
    result = tp.find(isbn)
  end
  return result
end

.open(username, password, driver = nil) ⇒ 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.open("username","password") do |tp|
  result = tp.find("9780091835132")
end


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rbook/titlepage/client.rb', line 109

def self.open(username, password, driver = nil)

  tp = self.new(driver || TitleQueryPortType.new)

  begin
    tp.(username, password)

    yield(tp)

  ensure
    tp.logout
  end
end

.use_proxy(uri) ⇒ Object

Tells all instances of this class to query the titlepage API via a Distributed Ruby proxy. This is useful where multiple machines need to make queries and you need to be careful not to exceed the maximum query rate.

There is an example proxy executable available in the bin/ directory of rbook.

Assuming the proxy is running on port 61676 of the local machine, the following code can be used to query titlepage:

RBook::TitlePage::Client.use_proxy("druby://127.0.0.1:61676")
RBook::TitlePage::Client.open("username", "password") do |tp|
  puts tp.find("0091835135").product.title.titleText
end


34
35
36
# File 'lib/rbook/titlepage/client.rb', line 34

def self.use_proxy(uri)
  @@uri = uri
end

Instance Method Details

#find(isbn) ⇒ Object

retrieve information on a specified ISBN



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbook/titlepage/client.rb', line 69

def find(isbn)
  return NotLoggedInError, 'You must login to titlepage API before performing a search' unless @token || @@uri
  isbn = ISBN::convert_to_isbn13(isbn)
  return nil if isbn.nil?
  begin
    if @@uri
      proxy = DRbObject.new_with_uri(@@uri)
      result = proxy.find(@username, @password, isbn) 
    else
      result = @driver.SearchByEAN(@token, isbn)
    end
    if result.Product.nil?
      return nil
    else
      return result
    end
  rescue 
    return nil
  end

end

#login(username, password) ⇒ Object

login to the titlepage API.



46
47
48
49
50
51
52
53
54
55
# File 'lib/rbook/titlepage/client.rb', line 46

def (username, password)
  raise InvalidRubyVersionError, 'Ruby 1.8.3 or higher is required to use this class' unless RUBY_VERSION >= "1.8.3"
  logout if @token
  @username = username
  @password = password
  unless @@uri
    @token = @driver.(username, password)
    return true if @token
  end
end

#logoutObject

logout from the titlepage API



58
59
60
61
62
63
64
65
66
# File 'lib/rbook/titlepage/client.rb', line 58

def logout
  @username = nil
  @password = nil
  if @token and @@uri != nil
    @driver.logout(@token)
  else
    true
  end
end