philologic-client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,7 +2,7 @@
2
2
  *.gem
3
3
  .bundle
4
4
  coverage/
5
- doc/
6
5
  Gemfile.lock
6
+ html/
7
7
  out.txt
8
8
  pkg/*
data/HISTORY.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  = Philologic::Client History
2
2
 
3
+ == 2012-01-04 v0.0.6
4
+
5
+ * +Philologic::Client#search()+ now supports arbitrary key-value query parameters.
6
+
3
7
  == 2012-01-03 v0.0.5
4
8
 
5
9
  * Added +Philologic::Link#document()+
data/README.rdoc CHANGED
@@ -16,11 +16,16 @@
16
16
  doc = first.document
17
17
  txt = doc.text
18
18
 
19
- # Search (currently only limited support)
19
+ # Simple search
20
20
  client.search('sartre').each do |result|
21
21
  puts [ child['href'], child['author'], child['title'] ].join("\t")
22
22
  end
23
23
 
24
+ # Search by arbitrary key-value pairs
25
+ client.search( :query => 'sartre', :foo => 1 ).each do |result|
26
+ puts [ child['href'], child['author'], child['title'] ].join("\t")
27
+ end
28
+
24
29
  # Get/Set encoding
25
30
  encoding = client.encoding
26
31
  client.encoding = 'utf-8' # Default
@@ -46,10 +51,7 @@ https://github.com/blairc/philologic-client/
46
51
 
47
52
  == To Do
48
53
 
49
- * Expand search support
50
- * Switch to MiniTest::Mock?
51
54
  * Add +children+ to Philologic::Document
52
55
  * Add +parent+ to Philologic::Document
53
56
  * Unify Philologic::Link and Philologic::Document
54
- * Build +RDoc+
55
57
 
data/Rakefile CHANGED
@@ -2,9 +2,10 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/clean'
3
3
  require 'rake/testtask'
4
4
  require 'rdoc-readme/rake_task'
5
+ require 'rdoc/task'
5
6
 
6
- %w{ coverage doc out.txt pkg }.each { |p| CLEAN.include(p) }
7
- %w{ build install test }.each { |t| task t.to_sym => [ 'rdoc:readme' ] }
7
+ %w{ coverage html out.txt pkg }.each { |p| CLEAN.include(p) }
8
+ %w{ build install rdoc test }.each { |t| task t.to_sym => [ 'rdoc:readme' ] }
8
9
 
9
10
  task :default => :test
10
11
 
@@ -16,3 +17,8 @@ end
16
17
 
17
18
  RDoc::Readme::RakeTask.new 'lib/philologic-client.rb', 'README.rdoc'
18
19
 
20
+ RDoc::Task.new do |rdoc|
21
+ rdoc.main = 'README.rdoc'
22
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
23
+ end
24
+
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philologic
4
4
  class Client
5
- VERSION = '0.0.5'
5
+ VERSION = '0.0.6'
6
6
  end
7
7
  end
@@ -24,11 +24,16 @@ require 'open-uri'
24
24
  # doc = first.document
25
25
  # txt = doc.text
26
26
  #
27
- # # Search (currently only limited support)
27
+ # # Simple search
28
28
  # client.search('sartre').each do |result|
29
29
  # puts [ child['href'], child['author'], child['title'] ].join("\t")
30
30
  # end
31
31
  #
32
+ # # Search by arbitrary key-value pairs
33
+ # client.search( :query => 'sartre', :foo => 1 ).each do |result|
34
+ # puts [ child['href'], child['author'], child['title'] ].join("\t")
35
+ # end
36
+ #
32
37
  # # Get/Set encoding
33
38
  # encoding = client.encoding
34
39
  # client.encoding = 'utf-8' # Default
@@ -54,20 +59,22 @@ require 'open-uri'
54
59
  #
55
60
  # == To Do
56
61
  #
57
- # * Expand search support
58
- # * Switch to MiniTest::Mock?
59
62
  # * Add +children+ to Philologic::Document
60
63
  # * Add +parent+ to Philologic::Document
61
64
  # * Unify Philologic::Link and Philologic::Document
62
- # * Build +RDoc+
63
65
  #
64
- module Philologic
66
+ module Philologic # :nodoc:
65
67
 
66
68
  #
67
69
  # Philologic client.
68
70
  #
69
71
  class Client
70
72
 
73
+ #
74
+ # Default encoding.
75
+ #
76
+ DEFAULT_ENCODING = 'utf-8'
77
+
71
78
  #
72
79
  # Get/Set +encoding+
73
80
  #
@@ -82,10 +89,10 @@ module Philologic
82
89
  # Initialize Philologic::Client
83
90
  #
84
91
  # Params:
85
- # +endpoint+:: Philologic endpoint (defaults to +nil+)
92
+ # +endpoint+:: Philologic endpoint (defaults to +nil+)
86
93
  #
87
94
  def initialize( endpoint = nil )
88
- @encoding = 'utf-8'
95
+ @encoding = DEFAULT_ENCODING
89
96
  @endpoint = endpoint
90
97
  yield self if block_given?
91
98
  self
@@ -95,7 +102,7 @@ module Philologic
95
102
  # Return +Array+ of Philologic::Link objects.
96
103
  #
97
104
  # Params:
98
- # +path+:: Return children of this path (defaults to +/+)
105
+ # +path+:: Return children of this path (defaults to +/+)
99
106
  #
100
107
  def children( path = '/' )
101
108
  doc = _get(path)
@@ -106,7 +113,7 @@ module Philologic
106
113
  # Return Philologic::Document object or +nil+.
107
114
  #
108
115
  # Params:
109
- # +path+:: Return document at this path (defaults to +/+)
116
+ # +path+:: Return document at this path (defaults to +/+)
110
117
  #
111
118
  def document( path = '/' )
112
119
  doc = _get(path)
@@ -116,30 +123,41 @@ module Philologic
116
123
  #
117
124
  # Return +Array+ of Philologic::Link objects.
118
125
  #
119
- # *NOTE:* Currently only returns the first page of results.
126
+ # *NOTE:* Currently only returns the first page of results by default.
120
127
  #
121
128
  # Params:
122
- # +query+:: Search for this query string.
129
+ # +query+:: Search for this query +String+ or +Hash+ key-value pairs.
123
130
  #
124
131
  def search(query)
125
- raise('no query specified') if query.nil?
126
- doc = _query(query)
132
+ raise('no query specified') if ( query.nil? || !( [ Hash, String ].include?( query.class ) )) || query.empty?
133
+ doc = _query( query.kind_of?(String) ? { :query => query } : query )
127
134
  return doc.kind_of?(Array) ? doc : nil
128
135
  end
129
136
 
137
+
130
138
  private
131
139
 
132
- def _build_query(query)
133
- raise('no query specified') if query.nil?
134
- "?query=#{query}"
140
+ #
141
+ # Build query +String+ from +Hash+.
142
+ #
143
+ def _build_query(pairs)
144
+ raise('no query specified') if pairs.nil? || !pairs.kind_of?(Hash) || pairs.empty?
145
+ "?" + pairs.map { |k,v| "#{k}=#{v}" }.join('&')
135
146
  end
136
147
 
148
+ #
149
+ # Return parsed version of +path+.
150
+ #
137
151
  def _get(path)
138
152
  raise('no endpoint specified') if @endpoint.nil?
139
153
  uri = "#{ @endpoint }#{ @endpoint =~ /\/$/ || '/' }#{ path }"
140
154
  _parse( Nokogiri::HTML( open(uri).read, nil, @encoding ) )
141
155
  end
142
156
 
157
+ #
158
+ # Return parsed document or +nil+.
159
+ #
160
+ # TODO This needs some love.
143
161
  def _parse(doc)
144
162
  if !doc.css('div.philologic_cite_list').first.nil?
145
163
  children = []
@@ -159,6 +177,9 @@ module Philologic
159
177
  nil
160
178
  end
161
179
 
180
+ #
181
+ # Build query string from +query+, perform query and return results.
182
+ #
162
183
  def _query(query)
163
184
  return _get( _build_query(query) )
164
185
  end
@@ -171,6 +192,12 @@ module Philologic
171
192
  #
172
193
  class Document
173
194
 
195
+ #
196
+ # Initialize Philologic::Document object.
197
+ #
198
+ # Params:
199
+ # +document+:: Nokogiri document
200
+ #
174
201
  def initialize(document)
175
202
  @doc = document
176
203
  @text = nil
@@ -199,6 +226,13 @@ module Philologic
199
226
  #
200
227
  class Link
201
228
 
229
+ #
230
+ # Initialize Philologic::Link object.
231
+ #
232
+ # Params:
233
+ # +client+:: Philologic::Client object
234
+ # +document+:: Nokogiri document
235
+ #
202
236
  def initialize(client, document)
203
237
  @client = client
204
238
  @doc = document
@@ -20,8 +20,9 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'rdoc'
23
24
  s.add_development_dependency 'mocha'
24
- s.add_development_dependency 'rdoc-readme'
25
+ s.add_development_dependency 'rdoc-readme', '~> 0.1.2'
25
26
  s.add_development_dependency 'simplecov'
26
27
  s.add_runtime_dependency 'nokogiri'
27
28
  end
@@ -52,13 +52,9 @@ class TestPhilologicClient < Test::Unit::TestCase
52
52
 
53
53
  def test_encoding_accessor
54
54
  Philologic::Client.new do |client|
55
- assert_equal 'utf-8', client.encoding
56
- assert_equal 'foo', client.encoding = 'foo'
57
- assert_equal 'foo', client.encoding
58
- #
59
- # assert_nil client.endpoint, 'defaults to nil'
60
- # assert_equal @endpoint, client.endpoint = @endpoint, 'returns value on set'
61
- # assert_equal @endpoint, client.endpoint, 'returns value on get'
55
+ assert_equal Philologic::Client::DEFAULT_ENCODING, client.encoding
56
+ assert_equal 'foo', client.encoding = 'foo'
57
+ assert_equal 'foo', client.encoding
62
58
  end
63
59
  end
64
60
 
@@ -169,44 +165,45 @@ class TestPhilologicClient < Test::Unit::TestCase
169
165
  end
170
166
  end
171
167
 
172
- def test_nil_search
168
+ def test_underscore_query
173
169
  Philologic::Client.new(@endpoint) do |client|
174
- assert_raise(RuntimeError, 'no query specified') { client.search(nil) }
170
+ doc = client.send( :_parse, Nokogiri::HTML( open(@query_file) ) )
171
+ client.stubs(:_get).with('?query=sartre').returns(doc)
172
+ assert_equal doc, client.send( :_query, :query => 'sartre' )
175
173
  end
176
174
  end
177
175
 
178
- def test_underscore_query
176
+ def test_underscore_build_query_with_invalid_queries
179
177
  Philologic::Client.new(@endpoint) do |client|
180
- doc = client.send( :_parse, Nokogiri::HTML( open(@query_file) ) )
181
- q = 'sartre'
182
- qs = "?query=#{q}"
183
- client.stubs(:_get).with(qs).returns(doc)
184
- assert_equal doc, client.send(:_query, q)
178
+ assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, nil) }
179
+ assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, {}) }
180
+ assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, []) }
181
+ assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, '') }
182
+ assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, 'foo') }
185
183
  end
186
184
  end
187
185
 
188
- def test_underscore_build_query_with_nil_query
186
+ def test_underscore_build_query
189
187
  Philologic::Client.new(@endpoint) do |client|
190
- assert_raise(RuntimeError, 'no query specified') { client.send(:_build_query, nil) }
188
+ assert_equal '?query=sartre', client.send( :_build_query, :query => 'sartre' )
189
+ assert_equal '?query=sartre&foo=bar', client.send( :_build_query, :query => 'sartre', :foo => 'bar' )
191
190
  end
192
191
  end
193
192
 
194
- def test_underscore_build_query
193
+ def test_search_with_invalid_queries
195
194
  Philologic::Client.new(@endpoint) do |client|
196
- q = 'sartre'
197
- qs = "?query=#{q}"
198
- assert_equal qs, client.send(:_build_query, q)
195
+ [ nil, [], {}, '', [ '' ], [ nil ], Object.new ].each do |q|
196
+ assert_raise(RuntimeError, 'invalid query') { client.search(q) }
197
+ end
199
198
  end
200
199
  end
201
200
 
202
201
  def test_search
203
202
  Philologic::Client.new(@endpoint) do |client|
204
203
  doc = client.send( :_parse, Nokogiri::HTML( open(@query_file) ) )
205
- q = 'sartre'
206
- qs = "?query=#{q}"
207
- client.stubs(:_get).with(qs).returns(doc)
204
+ client.stubs(:_get).with('?query=sartre').returns(doc)
208
205
 
209
- results = client.search(q)
206
+ results = client.search('sartre')
210
207
 
211
208
  assert_not_nil results
212
209
  assert_kind_of Array, results
@@ -236,5 +233,42 @@ class TestPhilologicClient < Test::Unit::TestCase
236
233
 
237
234
  end
238
235
  end
236
+
237
+ def test_search_with_key_value_pairs
238
+ Philologic::Client.new(@endpoint) do |client|
239
+ doc = client.send( :_parse, Nokogiri::HTML( open(@query_file) ) )
240
+ client.stubs(:_get).with('?query=sartre').returns(doc)
241
+
242
+ results = client.search( :query => 'sartre' )
243
+
244
+ assert_not_nil results
245
+ assert_kind_of Array, results
246
+ assert_equal 50, results.size
247
+ results.each do |result|
248
+ assert_kind_of Philologic::Link, result
249
+ assert_equal 'philologic_cite', result['class']
250
+ assert_not_nil result['href']
251
+ assert_nil result['some random attribute']
252
+ assert_not_nil result['author']
253
+ assert_nil result['filename']
254
+ assert_not_nil result['title']
255
+ assert_raise(NoMethodError) { result.text }
256
+ end
257
+
258
+ assert_equal './54/1/0/0', results.first['href']
259
+ assert_equal 'Peiresc N. de', results.first['author']
260
+ assert_equal 'Lettres T. 6 1602-1637', results.first['title']
261
+ assert_nil results.first['filename']
262
+ assert_raise(NoMethodError) { results.first.text }
263
+
264
+ assert_equal './1792/11/0/0', results.last['href']
265
+ assert_equal 'Gide A.', results.last['author']
266
+ assert_equal 'Journal 1939-1949', results.last['title']
267
+ assert_nil results.last['filename']
268
+ assert_raise(NoMethodError) { results.last.text }
269
+
270
+ end
271
+ end
272
+
239
273
  end
240
274
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philologic-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-03 00:00:00.000000000Z
12
+ date: 2012-01-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70270934047440 !ruby/object:Gem::Requirement
16
+ requirement: &70118250803080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70270934047440
24
+ version_requirements: *70118250803080
25
25
  - !ruby/object:Gem::Dependency
26
- name: mocha
27
- requirement: &70270934047000 !ruby/object:Gem::Requirement
26
+ name: rdoc
27
+ requirement: &70118250802640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70270934047000
35
+ version_requirements: *70118250802640
36
36
  - !ruby/object:Gem::Dependency
37
- name: rdoc-readme
38
- requirement: &70270934046580 !ruby/object:Gem::Requirement
37
+ name: mocha
38
+ requirement: &70118250802220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70270934046580
46
+ version_requirements: *70118250802220
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc-readme
49
+ requirement: &70118250801720 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.2
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70118250801720
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: simplecov
49
- requirement: &70270934046160 !ruby/object:Gem::Requirement
60
+ requirement: &70118250801300 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70270934046160
68
+ version_requirements: *70118250801300
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: nokogiri
60
- requirement: &70270934045740 !ruby/object:Gem::Requirement
71
+ requirement: &70118250800840 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,7 +76,7 @@ dependencies:
65
76
  version: '0'
66
77
  type: :runtime
67
78
  prerelease: false
68
- version_requirements: *70270934045740
79
+ version_requirements: *70118250800840
69
80
  description: Ruby client for interacting with the Philologic API.
70
81
  email:
71
82
  - blair.christensen@gmail.com
@@ -99,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
110
  version: '0'
100
111
  segments:
101
112
  - 0
102
- hash: -2626252476221214395
113
+ hash: -808024732695732071
103
114
  required_rubygems_version: !ruby/object:Gem::Requirement
104
115
  none: false
105
116
  requirements:
@@ -108,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
119
  version: '0'
109
120
  segments:
110
121
  - 0
111
- hash: -2626252476221214395
122
+ hash: -808024732695732071
112
123
  requirements: []
113
124
  rubyforge_project: philologic-client
114
125
  rubygems_version: 1.8.7