dnz-client 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.1.1 2010-05-24
2
+
3
+ * Add support for fetching individual record metadata.
4
+
1
5
  === 0.1.0 2010-05-13
2
6
 
3
7
  * Support for a global connection. This allows some convenient methods on other classes.
data/Manifest.txt CHANGED
@@ -12,6 +12,8 @@ lib/dnz/facet_array.rb
12
12
  lib/dnz/result.rb
13
13
  lib/dnz/results.rb
14
14
  lib/dnz/custom_search.rb
15
+ lib/dnz/namespace_array.rb
16
+ lib/dnz/record.rb
15
17
  lib/dnz/search.rb
16
18
  lib/dnz/memoizable.rb
17
19
  lib/dnz/error/invalid_api_key.rb
@@ -1,5 +1,9 @@
1
1
  module DNZ
2
2
  module Attributes
3
+ def id
4
+ @attributes['id']
5
+ end
6
+
3
7
  def method_missing(symbol, *args)
4
8
  if args.empty? && @attributes.has_key?(symbol.to_s)
5
9
  @attributes[symbol.to_s]
data/lib/dnz/client.rb CHANGED
@@ -4,6 +4,7 @@ require 'set'
4
4
  require 'active_support'
5
5
  require 'dnz/custom_search'
6
6
  require 'dnz/search'
7
+ require 'dnz/record'
7
8
  require 'dnz/error/invalid_api_key'
8
9
 
9
10
  module DNZ
@@ -38,7 +39,8 @@ module DNZ
38
39
  APIS = {
39
40
  :search => 'records/${version}.xml/',
40
41
  :custom_search => 'custom_searches/${version}/${custom_search}.xml',
41
- :custom_search_preview => 'custom_searches/${version}/test.xml'
42
+ :custom_search_preview => 'custom_searches/${version}/test.xml',
43
+ :record => 'records/${version}/${id}.xml'
42
44
  }
43
45
 
44
46
  # API Arguments
@@ -113,6 +115,10 @@ module DNZ
113
115
  @api_key = api_key
114
116
  @base_url = base_url
115
117
  @version = version
118
+
119
+ if @base_url =~ /^(.*)\/$/
120
+ @base_url = $1
121
+ end
116
122
  end
117
123
 
118
124
  # Get a list of all categories using the 'category' facet.
@@ -213,7 +219,7 @@ module DNZ
213
219
  variable_name = $1.to_sym
214
220
 
215
221
  if options.has_key?(variable_name)
216
- path.sub!(variable_regex, options.delete(variable_name))
222
+ path.sub!(variable_regex, options.delete(variable_name).to_s)
217
223
  else
218
224
  raise ArgumentError.new("Required argument missing: #{variable_name}")
219
225
  end
@@ -0,0 +1,74 @@
1
+ # This class takes an array of Nokogiri::XML::Element objects and
2
+ # makes them accessible by namespace and node name.
3
+ #
4
+ # Example:
5
+ #
6
+ # <dc:title>Test</dc:title>
7
+ # <dnz:tag>ok</dnz:tag>
8
+ # <dnz:content_partner>National Library</dnz:content_partner>
9
+ #
10
+ # @array.dc.size => 1
11
+ # @array.dnz.size => 2
12
+ # @array.dc.title.text => Test
13
+ # @array.dnz.tag.text => ok
14
+ #
15
+ # Namespace is optional:
16
+ # @array.title.text => Test
17
+ #
18
+ # Return value is another NamespaceArray:
19
+ # @array.dnz.class => NamespaceArray
20
+ #
21
+ module DNZ
22
+ class NamespaceArray < Array
23
+ def [](index)
24
+ if index.is_a?(String)
25
+ if (nodes = self.select{|node| node.namespace.prefix.downcase.to_s == index }).any?
26
+ return NamespaceArray.new(nodes)
27
+ end
28
+
29
+ if (nodes = self.select{|node| node.name == index }).any?
30
+ return NamespaceArray.new(nodes)
31
+ end
32
+
33
+ NamespaceArray.new
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def type
40
+ self['type']
41
+ end
42
+
43
+ # An array of all unique node names in this array.
44
+ def names
45
+ map(&:name).map(&:downcase).uniq
46
+ end
47
+
48
+ # An array of all unique namespaces in this array.
49
+ def namespaces
50
+ map(&:namespace).map(&:prefix).map(&:to_s).map(&:downcase).uniq
51
+ end
52
+
53
+ # The combined text of all nodes in this array.
54
+ def text
55
+ map(&:text).join
56
+ end
57
+
58
+ def inspect
59
+ namespaces.collect {|namespace| '%s => [ %s ]' % [namespace, self[namespace].names.join(', ')] }.join(', ')
60
+ end
61
+
62
+ def method_missing(symbol, *args, &block)
63
+ method = symbol.to_s
64
+
65
+ if method =~ /(.*)\?$/
66
+ self[$1].any?
67
+ elsif self.length == 1 && self.first.respond_to?(symbol)
68
+ self.first.send(symbol, *args, &block)
69
+ elsif args.empty?
70
+ self[method]
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/dnz/record.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'nokogiri'
2
+ require 'dnz/memoizable'
3
+ require 'dnz/namespace_array'
4
+
5
+ module DNZ
6
+ class Record
7
+ def self.find(id)
8
+ self.new(Client.connection, id)
9
+ end
10
+
11
+
12
+ def initialize(client, id)
13
+ @client = client
14
+ options = {:id => id}
15
+
16
+ @xml = @client.fetch(:record, options)
17
+ parse_record
18
+ end
19
+
20
+
21
+ def method_missing(method, *args, &block)
22
+ if attribute = document.root.attributes[method.to_s.upcase]
23
+ attribute.to_s
24
+ else
25
+ @data.send(method, *args, &block)
26
+ end
27
+ end
28
+
29
+
30
+ private
31
+ # Return a Nokogiri document for the XML
32
+ def document
33
+ @doc ||= Nokogiri::XML(@xml)
34
+ end
35
+
36
+
37
+
38
+ # Parse the result into an array of DNZ::Result
39
+ def parse_record
40
+ @data = NamespaceArray.new
41
+ document.xpath('//xmlns:xmlData').each do |xdata|
42
+ xdata.children.each do |child|
43
+ if child.element?
44
+ @data << child
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/dnz.rb CHANGED
@@ -4,5 +4,5 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'dnz/client'
5
5
 
6
6
  module Dnz
7
- VERSION = '0.1.0'
7
+ VERSION = '0.1.1'
8
8
  end
@@ -130,7 +130,26 @@ describe Client do
130
130
  end
131
131
  end
132
132
  end
133
+
134
+ describe '#new' do
135
+ before do
136
+ @api_key = 'abc'
137
+ @url = 'http://example.com/'
138
+ end
139
+
140
+ it 'should return a Client instance' do
141
+ Client.new(@api_key).should be_a(Client)
142
+ end
133
143
 
144
+
145
+ it 'should remove the last forward slash from the URL' do
146
+ @client = Client.new(@api_key, 'v1', @url)
147
+ @client.should_receive(:open).with do |url|
148
+ url.should include('http://example.com/records/v1.xml/?')
149
+ end
150
+ @client.fetch(:search, :search_text => '*:*')
151
+ end
152
+ end
134
153
 
135
154
  describe '#search' do
136
155
  it 'should create a new search object and return it' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jeremy Wells
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-13 00:00:00 +12:00
17
+ date: 2010-05-24 00:00:00 +12:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -101,6 +101,8 @@ files:
101
101
  - lib/dnz/result.rb
102
102
  - lib/dnz/results.rb
103
103
  - lib/dnz/custom_search.rb
104
+ - lib/dnz/namespace_array.rb
105
+ - lib/dnz/record.rb
104
106
  - lib/dnz/search.rb
105
107
  - lib/dnz/memoizable.rb
106
108
  - lib/dnz/error/invalid_api_key.rb