Class: BorrowDirect::GenerateQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/borrow_direct/generate_query.rb

Overview

Generate a “deep link” to query results in BD’s native HTML interface.

Constant Summary collapse

@@fields =

Hash from our own API argument to BD field code

{
  :keyword  => "term",
  :title    => "ti",
  :author   => "au",
  :subject  => "su",
  :isbn     => "isbn",
  :issn     => "issn"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_base = nil) ⇒ GenerateQuery

Returns a new instance of GenerateQuery.



19
20
21
# File 'lib/borrow_direct/generate_query.rb', line 19

def initialize(url_base = nil)
  self.url_base = (url_base || BorrowDirect::Defaults.html_base_url)
end

Instance Attribute Details

#url_baseObject

Returns the value of attribute url_base.



7
8
9
# File 'lib/borrow_direct/generate_query.rb', line 7

def url_base
  @url_base
end

Instance Method Details

#best_known_item_query_url_with(options) ⇒ Object



63
64
65
66
67
# File 'lib/borrow_direct/generate_query.rb', line 63

def best_known_item_query_url_with(options)
  query = best_known_item_query_with(options)

  return self.url_base + '?' + "query=#{CGI.escape query}"      
end

#best_known_item_query_with(options) ⇒ Object

Pass in :title, :author, :isbn, etc – if we have an isbn or issn, we’ll use that alone, otherwise we’ll use title and author



47
48
49
50
51
52
53
54
55
# File 'lib/borrow_direct/generate_query.rb', line 47

def best_known_item_query_with(options)
  if options[:isbn]
    return query_with(options.dup.delete_if {|k| k != :isbn})
  elsif options[:issn]
    return query_with(options.dup.delete_if {|k| k != :issn})
  else
    return query_with options
  end
end

#escape_query_value(str) ⇒ Object

We don’t really know how to escape, for now we just remove double quotes and parens, and replace with spaces. those seem to cause problems, and that seems to work.



72
73
74
# File 'lib/borrow_direct/generate_query.rb', line 72

def escape_query_value(str)
  str.gsub(/[")()]/, ' ')
end

#query_url_with(options) ⇒ Object



57
58
59
60
61
# File 'lib/borrow_direct/generate_query.rb', line 57

def query_url_with(options)
  query = query_with(options)

  return self.url_base + '?' + "query=#{CGI.escape query}"
end

#query_with(options) ⇒ Object

query_with(:title => “one two”, :author => “three four”) valid keys are those supported by BD HTML interface:

:title, :author, :isbn, :subject, :keyword, :isbn, :issn

For now, the value is always searched as a phrase, and multiple fields are always ‘and’d. We may enhance/expand later.

Returns an un-escaped query, still needs to be put into a URL



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/borrow_direct/generate_query.rb', line 31

def query_with(options)
  clauses = []

  options.each_pair do |field, value|
    code = @@fields[field]

    raise ArgumentError.new("Don't recognize field code `#{field}`") unless code

    clauses << %Q{#{code}="#{escape_query_value value}"}
  end

  return clauses.join(" and ")
end