Class: RBook::TitlePage::WWWClient
- Inherits:
-
Object
- Object
- RBook::TitlePage::WWWClient
- 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
-
.open(username, password) ⇒ Object
a convenience method to make queries to title page a little cleaner.
Instance Method Summary collapse
- #get_onix_file(isbn) ⇒ Object
-
#initialize ⇒ WWWClient
constructor
A new instance of WWWClient.
-
#login(username, password) ⇒ Object
login to the titlepage website.
-
#logout ⇒ Object
logout from the titlepage API.
Constructor Details
#initialize ⇒ WWWClient
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
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.login(username, password) yield(tp) ensure tp.logout end end |
Instance Method Details
#get_onix_file(isbn) ⇒ Object
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 } login_response = 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 = login_response.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 login(username, password) login_response = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http| data = [ "usr=#{username}", "pwd=#{password}", "login=Login" ].join("&") http.post('/index.php', data) end @cookie = login_response['set-cookie'] end |
#logout ⇒ Object
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 login_response = Net::HTTP.start(TITLEPAGE_DOMAIN, 80) do |http| http.get("/logout.php") end @cookie = nil end end |