Class: Archivist::Client::Base
- Inherits:
-
Object
- Object
- Archivist::Client::Base
- Defined in:
- lib/archivist/client/base.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #params(opts = {}) ⇒ Object
- #query(opts) ⇒ Object
- #search(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/archivist/client/base.rb', line 10 def initialize(opts = {}) @opts = { page: 1, rows: 50 }.merge(opts) @conn = 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 end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
8 9 10 |
# File 'lib/archivist/client/base.rb', line 8 def conn @conn end |
Instance Method Details
#params(opts = {}) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/archivist/client/base.rb', line 46 def params(opts = {}) { q: query(opts), fl: %w(identifier title creator date language mediattype), sort: ['date asc'], output: 'json' }.merge(@opts).merge(opts) end |
#query(opts) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/archivist/client/base.rb', line 23 def query(opts) filters = [ 'mediatype:texts', '-mediatype:collection' ] filters.concat(opts.delete(:filters)) if opts[:filters] filters << if opts[:language] "language:#{opts.delete(:language)}" else '(language:eng OR language:English)' end if opts[:start_year] && opts[:end_year] start_year = "#{opts.delete(:start_year)}-01-01" end_year = "#{opts.delete(:end_year)}-12-31" filters << "date:[#{start_year} TO #{end_year}]" end filters.join(' AND ') end |
#search(opts = {}) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/archivist/client/base.rb', line 55 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 |