archivist-client 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTNjMjQ1ZTA3M2RkNzA3NzA0MDg1ZjY2MmMwMzYwODYzYzM1ZTZhMA==
4
+ OGMwNzNlM2QxMTcwZjFlMzlmNTBjMGJlODY3YTdkM2M4MTU2YzViYw==
5
5
  data.tar.gz: !binary |-
6
- ZGYzZGU5NjBlNzI4NGNkZGM4NmVkNWU1NjY0NTM3OGRhM2ZhYjM4OQ==
6
+ Y2RiZjY3Njg4NjIzODQyMmJlZTQ5NDQ3OWNlZDE4YjY4NzBlNmFkZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDQ4YzFhNzM2YjE2YjdhYmU1YTU2MzJlZDE4Nzc4NDM0YmJlZWUyYTI1OTdj
10
- ZTUwNzk0MmUyOTdmMjRjMjYzZmVmMDM0NjhiZjA5ZDc4NmMyNzQ5YzdiM2Nh
11
- NmZhNDVjYTQyNjQ3YjI4MzQ4ZmE0YTY4ZjRjOGNkZTU2MjFjYmU=
9
+ MDJjNmVjZTNmZWQ2YzBjNjMxNDRiNTFlMjRkMmEyZDcwMzFlMDM3NGIyZjU2
10
+ NGZhNGNmMzI4NWNkMGFlMjI0YmJlYjE3MTVlNzJlMDQ1MTE1OGVlNjYzOGZi
11
+ MDhjOTAwM2Y2ZmFmMTJkZDAwZDM4ZjdiYTdiOWJhMmU0Mzk3YTY=
12
12
  data.tar.gz: !binary |-
13
- NGYzZmVmNTY2ZmEzZDM0NGI0OTJhOTNmZWUyNjViYTk3MGI1NzgxMWYxNTI1
14
- NTU0YWE1YjJhYzgxOTY3ZDcyNzFiZWMxZGI5ZDZkNDJjM2Y1YmRiNmY0MTZh
15
- M2M5M2EzZWNiZjNiNTlkMzkxNDBlYzU2ZTBmN2EyNDY2NWJlYWQ=
13
+ NzU3MzQ1NDFkNGJiZGMyNWQ4NmYxMDAwMGM2MTBjYTg5NjY0OGYzYTc1MTE5
14
+ YmFkNjFlMjZmZjhlNTQ4Mjk2NjU1ZjkwNTAwNTQ4Mzc3Mzk1YmYxOTA1NTcw
15
+ MDE5YTQ4YjcxZTQwMmI0NGJjZTgyZjY1NDdmMzY3NzFiYTg3ZjQ=
@@ -29,6 +29,7 @@ Gem::Specification.new do |gem|
29
29
  gem.add_development_dependency "vcr"
30
30
  gem.add_development_dependency "reek", ">= 1.2.8"
31
31
  gem.add_development_dependency "roodi", ">= 2.1.0"
32
+ gem.add_development_dependency "gem-release", ">= 0.7.1"
32
33
 
33
34
  gem.add_dependency "virtus", "~> 1.0.0"
34
35
  gem.add_dependency "representable"
@@ -1,46 +1,56 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
+ require 'archivist/client/filters'
3
4
  require 'archivist/representations'
4
5
 
5
6
  module Archivist
6
7
  module Client
8
+ # This is the primary interface of the gem.
9
+ # Example Usage:
10
+ # require 'archive-client'
11
+ # # Create an Archivist client:
12
+ # client = Archivist::Client::Base.new
13
+ # # Search for the books you're interested in:
14
+ # books = client.search(:start_year => 1500, :end_year => 1510)
15
+ # # Download them:
16
+ # books.each do |book|
17
+ # puts book.download
18
+ # end
7
19
  class Base
20
+ DEFAULT_CONNECTION = Faraday.new(url: 'http://archive.org') do |faraday|
21
+ faraday.use FaradayMiddleware::FollowRedirects
22
+ faraday.request :url_encoded # form-encode POST params
23
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
24
+ end
8
25
  attr_reader :conn
26
+ attr_accessor :filters # will be a Filters.new object
9
27
 
10
- def initialize(opts = {})
28
+ # filter_opts can be provided here, or when search is called.
29
+ # filters_opts are:
30
+ # :language => if *any* search opts provided there.
31
+ # :start_year => search opts takes precedence when provided there.
32
+ def initialize(opts = {}, filter_opts = {})
33
+ @filters = Archivist::Client::Filters.new(filter_opts)
11
34
  @opts = {
12
35
  page: 1,
13
36
  rows: 50
14
37
  }.merge(opts)
15
38
 
16
- @conn = Faraday.new(url: 'http://archive.org') do |faraday|
17
- faraday.use FaradayMiddleware::FollowRedirects
18
- faraday.request :url_encoded # form-encode POST params
19
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
20
- end
39
+ @conn = DEFAULT_CONNECTION
21
40
  end
22
41
 
23
- def query(opts)
24
- filters = [
25
- 'mediatype:texts',
26
- '-mediatype:collection'
27
- ]
28
-
29
- filters.concat(opts.delete(:filters)) if opts[:filters]
30
-
31
- filters << if opts[:language]
32
- "language:#{opts.delete(:language)}"
33
- else
34
- '(language:eng OR language:English)'
42
+ def search(opts = {})
43
+ Model::QueryResponse.new.tap do |qr|
44
+ response = @conn.get('/advancedsearch.php', params(opts))
45
+ Representation::QueryResponse.new(qr).from_json(response.body)
35
46
  end
47
+ end
36
48
 
37
- if opts[:start_year] && opts[:end_year]
38
- start_year = "#{opts.delete(:start_year)}-01-01"
39
- end_year = "#{opts.delete(:end_year)}-12-31"
40
- filters << "date:[#{start_year} TO #{end_year}]"
41
- end
49
+ private
42
50
 
43
- filters.join(' AND ')
51
+ def query(opts)
52
+ @filters.update!(opts)
53
+ @filters.to_query
44
54
  end
45
55
 
46
56
  def params(opts = {})
@@ -52,12 +62,6 @@ module Archivist
52
62
  }.merge(@opts).merge(opts)
53
63
  end
54
64
 
55
- def search(opts = {})
56
- Model::QueryResponse.new.tap do |qr|
57
- response = @conn.get('/advancedsearch.php', params(opts))
58
- Representation::QueryResponse.new(qr).from_json(response.body)
59
- end
60
- end
61
65
  end
62
66
  end
63
67
  end
@@ -0,0 +1,104 @@
1
+ module Archivist
2
+ module Client
3
+ # Filters class manages the available filters that are configured to work with Archive.org
4
+ # The public API is very simple, and is used to create Archive.org query strings:
5
+ # filters = Filters.new(options)
6
+ # query = filters.to_query
7
+ class Filters
8
+
9
+ # When Filters Provided are impossible try to Fail well.
10
+ class FilterException < StandardError; end
11
+
12
+ STANDARD_OPTIONS = [
13
+ :filters,
14
+ :language,
15
+ :start_year, :end_year
16
+ ]
17
+ DEFAULT_FILTERS = [
18
+ 'mediatype:texts',
19
+ '-mediatype:collection'
20
+ ]
21
+
22
+ attr_accessor :options,
23
+ :filters,
24
+ :language,
25
+ :start_year, :end_year
26
+
27
+ def initialize(opts)
28
+ @options = {}
29
+ self.update!(opts)
30
+ end
31
+
32
+ def update!(opts)
33
+ self.prune_options!(opts)
34
+ self.set_language!
35
+ self.set_years!
36
+ self.set_filters!
37
+ end
38
+
39
+ # May result in a URI with filters looking like:
40
+ # http://archive.org/advancedsearch.php?fl%5B0%5D=identifier&fl%5B1%5D=title&fl%5B2%5D=creator&fl%5B3%5D=date&fl%5B4%5D=language&fl%5B5%5D=mediattype&output=json&page=1&q=mediatype:texts%20AND%20-mediatype:collection%20AND%20(language:eng%20OR%20language:English)%20AND%20date:%5B1500-01-01%20TO%201550-12-31%5D&rows=50&sort%5B0%5D=date%20asc
41
+ alias :to_query :to_s
42
+ def to_query
43
+ self.finalize_filters!
44
+ @filters.join(' AND ')
45
+ end
46
+
47
+ protected
48
+
49
+ def prune_options!(opts)
50
+ # Save the options we need to build the filter
51
+ @options.merge!(opts)
52
+ # Remove the options used from the passed in options, so they don't get converted into parameters
53
+ STANDARD_OPTIONS.each do |opt|
54
+ opts.delete(opt)
55
+ end
56
+ end
57
+
58
+ # Only overwrite if update! options has language key
59
+ def set_language!
60
+ @language = @options[:language] if @options.has_key?(:language)
61
+ end
62
+
63
+ def set_years!
64
+ start = @options[:start_year]
65
+ finish = @options[:end_year]
66
+ @start_year = "#{start}-01-01" if start
67
+ @end_year = "#{finish}-12-31" if finish
68
+ self.validate_years!
69
+ end
70
+
71
+ # If one is provided, they must *both* be provided
72
+ def validate_years!
73
+ unless @start_year && @end_year
74
+ raise FilterException, ":start_year and :end_year must always be provided together, but only :start_year provided as #{@start_year}." if @start_year
75
+ raise FilterException, ":start_year and :end_year must always be provided together, but only :end_year provided as #{@end_year}." if @end_year
76
+ end
77
+ return true
78
+ end
79
+
80
+ def set_filters!
81
+ @filters = Array(@options[:filters]) # If opts[:filters] is nil => self.array is []
82
+ @filters |= DEFAULT_FILTERS
83
+ end
84
+
85
+ def add_language_filter!
86
+ @filters << if @language
87
+ "language:#{@language}"
88
+ else
89
+ '(language:eng OR language:English)'
90
+ end
91
+ end
92
+
93
+ def add_date_filter!
94
+ @filters << "date:[#{@start_year} TO #{@end_year}]"
95
+ end
96
+
97
+ def finalize_filters!
98
+ self.add_language_filter!
99
+ self.add_date_filter!
100
+ self
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,5 +1,5 @@
1
1
  module Archivist
2
2
  module Client
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -1,3 +1,5 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
1
3
  require 'virtus'
2
4
  require 'date'
3
5
 
@@ -5,6 +7,13 @@ module Archivist
5
7
  module Model
6
8
  class Document
7
9
  include Virtus.model
10
+ DEFAULT_CONNECTION = Faraday.new(url: 'http://archive.org') do |faraday|
11
+ faraday.use FaradayMiddleware::FollowRedirects
12
+ # faraday.response :logger # log requests to STDOUT
13
+ faraday.request :url_encoded # form-encode POST params
14
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
15
+ end
16
+ attr_reader :conn
8
17
 
9
18
  attribute :identifier, String
10
19
  attribute :title, String
@@ -13,12 +22,7 @@ module Archivist
13
22
  attribute :creators, Array[String]
14
23
 
15
24
  def initialize
16
- @conn = Faraday.new(url: "http://archive.org") do |faraday|
17
- faraday.use FaradayMiddleware::FollowRedirects
18
- faraday.request :url_encoded # form-encode POST params
19
- # faraday.response :logger # log requests to STDOUT
20
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
21
- end
25
+ @conn = DEFAULT_CONNECTION
22
26
  end
23
27
 
24
28
  def format_index
@@ -8,7 +8,7 @@ module Archivist
8
8
  attribute :formats
9
9
 
10
10
  def find(key)
11
- formats.find{ |f| f.format == key }
11
+ formats.find{ |value| value.format == key }
12
12
  end
13
13
  alias_method :[], :find
14
14
 
@@ -17,4 +17,4 @@ module Archivist
17
17
  end
18
18
  end
19
19
  end
20
- end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: archivist-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duane Johnson
@@ -123,6 +123,20 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: 2.1.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: gem-release
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: 0.7.1
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: 0.7.1
126
140
  - !ruby/object:Gem::Dependency
127
141
  name: virtus
128
142
  requirement: !ruby/object:Gem::Requirement
@@ -191,6 +205,7 @@ files:
191
205
  - archivist-client.gemspec
192
206
  - README.md
193
207
  - lib/archivist/client/base.rb
208
+ - lib/archivist/client/filters.rb
194
209
  - lib/archivist/client/version.rb
195
210
  - lib/archivist/models/body.rb
196
211
  - lib/archivist/models/document.rb