Class: Archivist::Client::Base

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/archivist/client/base.rb

Overview

This is the primary interface of the gem. Example Usage:

require 'archivist/client'
# Create an Archivist client:
client = Archivist::Client::Base.new
# Search for the books you're interested in:
books = client.search(:start_year => 1500, :end_year => 1510)
# Download them:
books.each do |book|
  puts book.download
end

Constant Summary

Constants included from Constants

Constants::DEFAULT_CONNECTION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, filter_opts = {}) ⇒ Base

filter_opts can be provided here, or when search is called. filters_opts are:

:language   => if *any* search opts provided there.
:start_year => search opts takes precedence when provided there.


31
32
33
34
35
36
37
38
39
# File 'lib/archivist/client/base.rb', line 31

def initialize(opts = {}, filter_opts = {})
  @filters = Archivist::Client::Filters.new(filter_opts)
  @opts = {
    page: 1,
    rows: 50
  }.merge(opts)

  @conn = DEFAULT_CONNECTION
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



24
25
26
# File 'lib/archivist/client/base.rb', line 24

def conn
  @conn
end

#filtersObject

will be a Filters.new object



25
26
27
# File 'lib/archivist/client/base.rb', line 25

def filters
  @filters
end

Instance Method Details

#search(opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/archivist/client/base.rb', line 41

def search(opts = {})
  Model::QueryResponse.new.tap do |qr|
    response = @conn.get('/advancedsearch.php', params(opts))
    rep = Representation::QueryResponse.new(qr)
    begin
      rep.from_json(response.body)
    rescue => e
      $stderr.puts "Unable to parse as Archivist::Representation::QueryResponse:"
      $stderr.puts response.body
      raise e
    end
  end
end