Class: RBook::GBIP::POS

Inherits:
Object
  • Object
show all
Defined in:
lib/rbook/gbip/pos.rb

Overview

Provides easy access to the globalbooksinprint.com search API. RBook::GBIP has basic usage examples.

Constant Summary collapse

POS_SERVER =

don’t think we really need these attr_accessor :username, :password

"pos.bowker.com"
POS_PORT =
7052

Instance Method Summary collapse

Constructor Details

#initialize(username, password, socket_class = nil) ⇒ POS

creates a new POS object ready to perform a search



20
21
22
23
24
# File 'lib/rbook/gbip/pos.rb', line 20

def initialize(username, password, socket_class = nil)
  @username = username
  @password = password
  @socket_class = socket_class || TCPSocket
end

Instance Method Details

#find(type, isbn, options = {}) ⇒ Object

search for the specified ISBN on globalbooksinprint.com. Accepts both ISBN10 and ISBN13’s.

Supported options: :markets => only search the specified markets. comma sperated list :timeout => amount of time in seconds to wait for a reponse from the server before timing out. Defaults to 10.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rbook/gbip/pos.rb', line 32

def find(type, isbn, options = {})
  case type
    when :first then default_return = nil
    when :all then default_return = []
    else raise ArgumentError, 'type must by :first or :all'
  end
  
  options = {:timeout => 10}.merge(options)

  # global only accepts ISBNs as 10 digits at this stage
  isbn = RBook::ISBN.convert_to_isbn10(isbn.to_s)
  return default_return if isbn.nil?
  
  request_format = "POS"
   = "3"
  product = "2"
  username = "#{@username}"
  password = "#{@password}"
  version = "2"
  supplier = ""
  request = "bn=#{isbn}"
  filters = "1|1|1|1|1|1|1|1|1|1"
  if type.eql?(:first)
    records = "1,0"
  else
    records = "10,0"
  end
  sort_order = "1"
  markets = options[:markets] || ""

  request_string = "#{request_format}\t#{}\t#{product}\t#{username}\t#{password}\t"
  request_string << "#{version}\t#{supplier}\t#{request}\t#{filters}\t#{records}\t#{sort_order}\t"
  request_string << "#{markets}\t"

  gbip = @socket_class.new(POS_SERVER, POS_PORT)
  gbip.print request_string
  response = Timeout::timeout(options[:timeout]) { gbip.gets(nil) }
  response = response.split("#")
  gbip.close
  
  header = nil
  titles_arr = []
  response.each do |arr|
    if header.nil?
      header = arr.split("\t")
    else
      titles_arr << arr.split("\t")
      titles_arr[-1] = titles_arr.last[1,titles_arr.last.size-1]
    end
  end
  
  # raise an exception if incorrect login details were provided
  if header.first.eql?("66") 
    raise RBook::InvalidLoginError, "Invalid username or password"
  end
   
  if titles_arr.size == 0
    # return nil if no matching records found
    return nil
  else

    titles = []
    titles_arr.each do |arr|
      title = build_object_tree(arr)
      titles << title unless title.nil?
    end

    if type.eql?(:first)
      return titles.first
    else
      return titles
    end
  end

end