Class: Alibris::Search

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/alibris.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Search

Returns a new instance of Search.



11
12
13
14
15
# File 'lib/alibris.rb', line 11

def initialize(options = {})
  @api_key = options[:api_key]           # required
  @output_type = options[:output_type]   # optional, valid values ['json', 'xml'], defaults to xml
  @num_results = options[:num_results]   # optional, number of results returned, defaults to 25
end

Instance Method Details

#books(search_term = nil, author = nil, title = nil, topic = nil, options = {}) ⇒ Object



35
36
37
38
# File 'lib/alibris.rb', line 35

def books(search_term=nil, author=nil, title=nil, topic=nil, options={})
  opts = options.merge({:mtype => 'B'})
  works(search_term, author, title, topic, opts)
end

#books_by_author(author, options = {}) ⇒ Object



39
40
41
# File 'lib/alibris.rb', line 39

def books_by_author(author, options={})
  books(nil, author, nil, nil, options)
end

#books_by_title(title, options = {}) ⇒ Object



42
43
44
# File 'lib/alibris.rb', line 42

def books_by_title(title, options={})
  books(nil, nil, title, nil, options)
end

#books_by_topic(topic, options = {}) ⇒ Object



45
46
47
# File 'lib/alibris.rb', line 45

def books_by_topic(topic, options={})
  books(nil, nil, nil, topic, options)
end

#music(search_term = nil, author = nil, title = nil, topic = nil, options = {}) ⇒ Object



48
49
50
51
# File 'lib/alibris.rb', line 48

def music(search_term=nil, author=nil, title=nil, topic=nil, options={})
  opts = options.merge({:mtype => 'M'})
  works(search_term, author, title, topic, opts)
end

#music_by_author(author, options = {}) ⇒ Object



52
53
54
# File 'lib/alibris.rb', line 52

def music_by_author(author, options={})
  music(nil, author, nil, nil, options)
end

#music_by_title(title, options = {}) ⇒ Object



55
56
57
# File 'lib/alibris.rb', line 55

def music_by_title(title, options={})
  music(nil, nil, title, nil, options)
end

#music_by_topic(topic, options = {}) ⇒ Object



58
59
60
# File 'lib/alibris.rb', line 58

def music_by_topic(topic, options={})
  music(nil, nil, nil, topic, options)
end

#videos(search_term = nil, author = nil, title = nil, topic = nil, options = {}) ⇒ Object



61
62
63
64
# File 'lib/alibris.rb', line 61

def videos(search_term=nil, author=nil, title=nil, topic=nil, options={})
  opts = options.merge({:mtype => 'V'})
  works(search_term, author, title, topic, opts)
end

#videos_by_author(author, options = {}) ⇒ Object



65
66
67
# File 'lib/alibris.rb', line 65

def videos_by_author(author, options={})
  videos(nil, author, nil, nil, options)
end

#videos_by_title(title, options = {}) ⇒ Object



68
69
70
# File 'lib/alibris.rb', line 68

def videos_by_title(title, options={})
  videos(nil, nil, title, nil, options)
end

#videos_by_topic(topic, options = {}) ⇒ Object



71
72
73
# File 'lib/alibris.rb', line 71

def videos_by_topic(topic, options={})
  videos(nil, nil, nil, topic, options)
end

#works(search_term = nil, author = nil, title = nil, topic = nil, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/alibris.rb', line 17

def works(search_term=nil, author=nil, title=nil, topic=nil, options={})
  if @api_key.nil?
    # TODO: Create a custom exception
    raise Exception.new("An API Key is required to use the Alibris API. Get a key from http://developer.alibris.com")
  end
  if (author.nil? && title.nil? && topic.nil? && search_term.nil?)
    raise Exception.new("You either need to pass an author, a title, a topic or a search term, to perform searches.")
  end
  path = "/"
  options.merge!({:apikey => @api_key}) unless @api_key.nil?
  options.merge!({:outputtype => @output_type}) unless @output_type.nil?
  options.merge!({:wauth => author}) unless author.nil?
  options.merge!({:wtit => title}) unless title.nil?
  options.merge!({:wtopic => topic}) unless topic.nil?
  options.merge!({:wquery => search_term}) unless search_term.nil?
  options.merge!({:chunk => @num_results}) unless @num_results.nil?
  Hashie::Mash.new Alibris::Search.get(path, :query => options)
end