Class: Archivist::Client::Base

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

Overview

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

require 'archive-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 collapse

DEFAULT_CONNECTION =
Faraday.new(url: 'http://archive.org') do |faraday|
  faraday.use FaradayMiddleware::FollowRedirects
  faraday.request  :url_encoded             # form-encode POST params
  faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
end

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.


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

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.



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

def conn
  @conn
end

#filtersObject

will be a Filters.new object



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

def filters
  @filters
end

Instance Method Details

#search(opts = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/archivist/client/base.rb', line 42

def search(opts = {})
  Model::QueryResponse.new.tap do |qr|
    response = @conn.get('/advancedsearch.php', params(opts))
    Representation::QueryResponse.new(qr).from_json(response.body)
  end
end