solo-rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Solo
2
2
  module Rails
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/solo-rails.rb CHANGED
@@ -15,18 +15,18 @@ module SoloRails
15
15
  end
16
16
 
17
17
  class Catalogue
18
-
18
+
19
19
  def initialize
20
+ # @site = "http://iser-training.soutron.net/Library/WebServices/SoutronApi.svc/"
20
21
  @site = "http://library.iser.essex.ac.uk/Library/WebServices/SoutronApi.svc/"
21
22
  end
22
23
 
23
24
  # returns SoloHash of values for individual catalogue record
24
25
  def show(id)
25
- id = id.to_i
26
26
  response = SoloHash.new
27
- url = "#{@site}getcatalogue?id=#{id}"
27
+ url = "#{@site}getcatalogue?id=#{CGI.escape(id)}"
28
28
  begin
29
- soutron_data = Nokogiri::XML(open(url, :read_timeout => 180))
29
+ soutron_data = Nokogiri::XML(open(url))
30
30
  response[:id] = soutron_data.xpath("/soutron/catalogs_view/ct/cat").attribute("id").text
31
31
  # response[:request_url] = url - removed for security/speed purposes - PG 2011-02-17
32
32
  response[:content_type] = soutron_data.xpath("/soutron/catalogs_view/ct").attribute("name").text
@@ -36,99 +36,153 @@ module SoloRails
36
36
  response[uscore(f.attribute("name").text).to_sym] = parse_values(f.attribute("ft").text, f.xpath("./vs/v"))
37
37
  end
38
38
  end
39
+
39
40
  # related records - PG 2011-03-01
40
41
  if soutron_data.xpath("count(/soutron/catalogs_view/ct/cat/related_catalogs)") > 0
41
42
  @related_records = []
42
43
  soutron_data.xpath("/soutron/catalogs_view/ct/cat/related_catalogs/ct").each do |related_ct|
43
- related_record = SoloHash.new
44
- related_record.merge!( { "content_type".to_sym => related_ct.attribute("name").text } )
45
- related_ct.xpath('ctlgs/cat').each do |r|
46
- related_record.merge!( {"cid".to_sym => r.attribute("id").text } )
47
- end
48
- @related_records << related_record
44
+ related_record = SoloHash.new
45
+ related_record.merge!( { "content_type".to_sym => related_ct.attribute("name").text } )
46
+ related_ct.xpath('ctlgs/cat').each do |r|
47
+ related_record.merge!( {"cid".to_sym => r.attribute("id").text } )
48
+ end
49
+ @related_records << related_record
49
50
  end
50
51
  response[:related_records] = @related_records
51
52
  end
53
+
52
54
  rescue
53
- response = nil
55
+ response = "Record #{id} not found"
54
56
  end
55
57
  return response
56
58
  end
57
59
 
58
60
  # returns nested hash record for given search - PG 2011-01-21
59
61
  def search(*args)
60
- build_query_url(args.pop)
62
+ options = args.pop
63
+ q, ctrt, select, sort, page, material, search_id, per_page, ignore_is_website_feature = iser_solo_parse_options(options)
64
+
65
+ # If we have a value of search_id, then should only also pass: search_id, select, sort, page, per_page & material
66
+ url = "#{@site}searchcatalogues?"
67
+ query_string = []
68
+
69
+ if search_id.present?
70
+ query_string << "searchid=#{search_id}"
71
+ else
72
+ query_string << "q=#{q}"
73
+ query_string << "ctrt=#{ctrt}" if ctrt.present?
74
+ end
75
+
76
+ query_string << "page=#{page}" if page.present?
77
+ query_string << "pageSize=#{per_page}" if per_page.present?
78
+ query_string << "sort=#{sort}" if sort.present?
79
+ query_string << "fields=#{select}" if select.present?
80
+ query_string << "material=#{material}" if material.present?
81
+
82
+ url += query_string.join("&")
83
+
84
+ p "Fetching SOLO data from: #{url}"
85
+
61
86
  begin
62
- parse_results(Nokogiri::XML(open(url, :read_timeout => 180)))
87
+ soutron_data = Nokogiri::XML(open(url, :read_timeout => 180))
63
88
  rescue Exception => e
64
89
  Rails.logger.info("SOLO Error in URL: " + url)
65
90
  end
66
- end
67
91
 
68
- private
92
+ response = SoloHash.new
93
+ Rails.logger.info("#{soutron_data.xpath("/soutron/search_info").attribute("id").text}")
94
+ meta = {:id => soutron_data.xpath("/soutron/search_info").attribute("id").text}
95
+ meta["total_items".to_sym] = soutron_data.xpath("/soutron/search_info").attribute("totalItems").text
96
+ meta["page".to_sym] = page
97
+ meta["per_page".to_sym] = per_page
98
+ meta["select".to_sym] = select
69
99
 
70
- def build_query_url(*options)
71
- q, ctrt, select, sort, page, material, search_id, per_page, ignore_is_website_feature = iser_solo_parse_options(options)
72
- url = "#{@site}searchcatalogues?"
73
- query_string = []
74
- # If we have a value of search_id, then should only also pass: search_id, select, sort, page, per_page & material
75
- if search_id.present?
76
- query_string << "searchid=#{search_id}"
77
- else
78
- query_string << "q=#{q}"
79
- query_string << "ctrt=#{ctrt}" if ctrt.present?
100
+ if soutron_data.xpath("count(//ct)") > 0
101
+ unless soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.nil?
102
+ meta["active_content_type".to_sym] = soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.attribute("name").text
103
+ meta["active_content_type_count".to_sym] = soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.attribute("count").text
80
104
  end
81
- query_string << "page=#{page}" if page.present?
82
- query_string << "pageSize=#{per_page}" if per_page.present?
83
- query_string << "sort=#{sort}" if sort.present?
84
- query_string << "fields=#{select}" if select.present?
85
- query_string << "material=#{material}" if material.present?
86
- url += query_string.join("&")
87
105
  end
88
106
 
89
- def parse_results(soutron_data)
90
- response = SoloHash.new
91
- meta = {:id => soutron_data.xpath("/soutron/search_info").attribute("id").text}
92
- meta["total_items".to_sym] = soutron_data.xpath("/soutron/search_info").attribute("totalItems").text
93
- meta["page".to_sym] = page
94
- meta["per_page".to_sym] = per_page
95
- meta["select".to_sym] = select
96
- if soutron_data.xpath("count(//ct)") > 0
97
- unless soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.nil?
98
- meta["active_content_type".to_sym] = soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.attribute("name").text
99
- meta["active_content_type_count".to_sym] = soutron_data.xpath("/soutron/search_info/catalogs_view/ct[*]").first.attribute("count").text
100
- end
101
- end
102
- response.merge!("search_info".to_sym => meta)
103
- @content_types = []
104
- soutron_data.xpath("/soutron/search_info/catalogs_view/ct").each do |ct|
105
- content_type = SoloHash.new
106
- content_type.merge!( { "content_type".to_sym => ct.attribute("name").text } )
107
- content_type.merge!( { "size".to_sym => ct.attribute("count").text } )
108
- @records = []
109
- ct.xpath("./ctlgs/cat").each do |cat|
110
- record = SoloHash.new
111
- record.merge!( {:id => cat.attribute("id").text, :record_type => cat.xpath("./rt").attribute("name").text} )
112
- cat.xpath("./fs/f").each do |f|
113
- if f.xpath("count(./vs/v)") > 0 # only include field if it has a value
114
- record[uscore(f.attribute("name").text).to_sym] = parse_values(f.attribute("ft").text, f.xpath("./vs/v"))
115
- end # / if has value
116
- end # /f
117
- @records << record
118
- end # /cat
119
- content_type.merge!({"records".to_sym => @records })
120
- @content_types << content_type
121
- end # /ct
122
- response.merge!(:content_types => @content_types)
123
- return response
107
+ # meta["request_url".to_sym] = url
108
+ response.merge!("search_info".to_sym => meta)
109
+
110
+ @content_types = []
111
+ soutron_data.xpath("/soutron/search_info/catalogs_view/ct").each do |ct|
112
+
113
+ content_type = SoloHash.new
114
+ content_type.merge!( { "content_type".to_sym => ct.attribute("name").text } )
115
+ content_type.merge!( { "size".to_sym => ct.attribute("count").text } )
116
+
117
+ @records = []
118
+ ct.xpath("./ctlgs/cat").each do |cat|
119
+
120
+ record = SoloHash.new
121
+ record.merge!( {:id => cat.attribute("id").text, :record_type => cat.xpath("./rt").attribute("name").text} )
122
+
123
+ cat.xpath("./fs/f").each do |f|
124
+ if f.xpath("count(./vs/v)") > 0 # only include field if it has a value
125
+ record[uscore(f.attribute("name").text).to_sym] = parse_values(f.attribute("ft").text, f.xpath("./vs/v"))
126
+ end # / if has value
127
+ end # /f
128
+
129
+ @records << record
130
+
131
+ end # /cat
132
+
133
+ content_type.merge!({"records".to_sym => @records })
134
+ @content_types << content_type
135
+
136
+ end # /ct
137
+
138
+ response.merge!(:content_types => @content_types)
139
+
140
+ return response
141
+ end
142
+
143
+ def self.published_years(record_type, limit=nil, q=nil)
144
+ q.nil? ? q = "Is Website Feature:Y" : q << ";Is Website Feature:Y"
145
+ newest_record = IserSolo.new.search :q => q,
146
+ :per_page => 1,
147
+ :sort => "Publication Date:d",
148
+ :select => "Publication Date",
149
+ :ctrt => ":#{record_type}"
150
+ d = newest_record.content_types.first.records.first.publication_date
151
+ if (d.to_s =~ /(20|19)\d{2}/) != 0
152
+ newest_year = Chronic.parse("#{d}").year
153
+ else
154
+ newest_year = Chronic.parse("01 Jan #{d}").year
155
+ end
156
+
157
+ oldest_record = IserSolo.new.search :q => q,
158
+ :per_page => 1,
159
+ :sort => "Publication Date:a",
160
+ :select => "Publication Date",
161
+ :ctrt => ":#{record_type}"
162
+ d = oldest_record.content_types.first.records.first.publication_date
163
+ if (d.to_s =~ /(20|19)\d{2}/) != 0
164
+ oldest_year = Chronic.parse("#{d}").year
165
+ else
166
+ oldest_year = Chronic.parse("01 Jan #{d}").year
124
167
  end
168
+ if limit.nil?
169
+ newest_year.downto(oldest_year)
170
+ else
171
+ Range.new(oldest_year, newest_year).to_a.reverse[0..limit]
172
+ end
173
+ end
174
+
175
+ private
125
176
 
126
177
  # returns array of URL safe variables from options
127
178
  def iser_solo_parse_options(options)
128
179
  q = options[:q]
129
180
  unless options[:ignore_is_website_feature] == true
130
- q.nil? ? nil : q << ";Is Website Feature:Y"
181
+ q.nil? ? nil : q << ";Is ISER Staff Publication:Y"
131
182
  end
183
+ # unless options[:ignore_is_website_feature] == true
184
+ # q.nil? ? nil : q << ";Is Website Feature:Y"
185
+ # end
132
186
  ctrt = options[:ctrt]
133
187
  select = options[:select]
134
188
  sort = options[:sort]
@@ -171,20 +225,18 @@ module SoloRails
171
225
  def parse_value(field_type, element)
172
226
  field_type = field_type.to_i
173
227
  case field_type
174
- when 1, 4, 5, 6, 11, 12 then element.text.to_s
175
- when 2 then element.text.to_i
176
- when 3 then Date.parse(element.text.to_s)
177
- # when 3 then element.text.to_s
178
- when 7 then
179
- if element.attribute("desc").value.size > 0 && !element.attribute("desc").value.eql?(element.text.to_s)
180
- # "#{element.attribute("desc")} - #{ActionController::Base.helpers.auto_link(element.text.to_s)}"
181
- "#{element.attribute("desc")} - element.text.to_s)}"
182
- else
183
- # ActionController::Base.helpers.auto_link(element.text.to_s)
184
- element.text.to_s
185
- end
186
- when 8 then parse_complex_date(element)
187
- else field_type
228
+ when 1, 4, 5, 6, 11, 12 then element.text.to_s
229
+ when 2 then element.text.to_i
230
+ when 3 then Date.parse(element.text.to_s)
231
+ # when 3 then element.text.to_s
232
+ when 7 then element.text.to_s
233
+ # if element.attribute("desc").value.size > 0 && !element.attribute("desc").value.eql?(element.text.to_s)
234
+ # "#{element.attribute("desc")} - #{ActionController::Base.helpers.auto_link(element.text.to_s)}"
235
+ # else
236
+ # ActionController::Base.helpers.auto_link(element.text.to_s)
237
+ # end
238
+ when 8 then parse_complex_date(element)
239
+ else field_type
188
240
  end
189
241
  end
190
242
 
@@ -197,28 +249,21 @@ module SoloRails
197
249
  date << d[0] unless d[0].nil?
198
250
  circa = "circa " if element.attribute("circa").to_s == "1"
199
251
  nodate = "forthcoming " if element.attribute("nodate").to_s == "1"
200
- ongoing = "ongoing" if element.attribute("ongoing").to_s == "1"
252
+ ongoing = "ongoing" if element.attribute("ongoing").to_s == "1"
201
253
  begin
202
254
  ret = Date.parse("#{date[0]}-#{date[1]}-#{date[2]}")
203
255
  rescue
204
- ret = "#{circa}#{nodate}#{date.join("-")} #{ongoing}".capitalize.strip
256
+ ret = "#{circa}#{nodate}#{date.join("-")} #{ongoing}".capitalize
205
257
  end
206
258
  ret
207
259
  end
208
260
 
209
- # removes spaces from 'str' and then 'borrows' rails underscore method
261
+ # removes spaces from 'str' and then applies rails underscore method
210
262
  # converts strings like "Publication Date" into "publication_date"
211
263
  def uscore(str)
212
- word = str.gsub(/\s*/,"")
213
- word = word.to_s.dup
214
- word.gsub!(/::/, '/')
215
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
216
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
217
- word.tr!("-", "_")
218
- word.downcase!
219
- word
264
+ str.gsub(/\s*/,"").underscore
220
265
  end
221
-
266
+
222
267
  end
223
268
 
224
269
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70135075996920 !ruby/object:Gem::Requirement
16
+ requirement: &70095837742940 !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: *70135075996920
24
+ version_requirements: *70095837742940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &70135075995560 !ruby/object:Gem::Requirement
27
+ requirement: &70095837742140 !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: :runtime
34
34
  prerelease: false
35
- version_requirements: *70135075995560
35
+ version_requirements: *70095837742140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: chronic
38
- requirement: &70135075994020 !ruby/object:Gem::Requirement
38
+ requirement: &70095837741340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70135075994020
46
+ version_requirements: *70095837741340
47
47
  description: Provides methods to query Soutron Solo catalog from Ruby
48
48
  email:
49
49
  - github@modagoo.co.uk