supplejack_client 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@
7
7
 
8
8
  require 'rails_autolink'
9
9
 
10
+ # rubocop:disable All
11
+ # FIXME: make this module presentable
10
12
  module Supplejack
11
13
  module Controllers
12
14
  module Helpers
@@ -7,6 +7,7 @@
7
7
 
8
8
  require 'supplejack_client'
9
9
  require 'rails'
10
+ require 'active_model'
10
11
 
11
12
  module Supplejack
12
13
  class Engine < Rails::Engine
@@ -9,6 +9,9 @@ module Supplejack
9
9
  class RecordNotFound < StandardError
10
10
  end
11
11
 
12
+ class ConceptNotFound < StandardError
13
+ end
14
+
12
15
  class SetNotFound < StandardError
13
16
  end
14
17
 
@@ -24,7 +24,9 @@ module Supplejack
24
24
  def initialize(user_set)
25
25
  @user_set = user_set
26
26
  items_array = user_set.attributes[:records] || []
27
- @items = items_array.map { |hash| Supplejack::Item.new(hash.merge(user_set_id: user_set.id, api_key: user_set.api_key)) }
27
+ @items = items_array.map do |hash|
28
+ Supplejack::Item.new(hash.merge(user_set_id: user_set.id, api_key: user_set.api_key))
29
+ end
28
30
  end
29
31
 
30
32
  # Returns an Array with all items for the current UserSet
@@ -8,6 +8,8 @@
8
8
  module Supplejack
9
9
  class LogSubscriber < ActiveSupport::LogSubscriber
10
10
 
11
+ # rubocop:disable Metrics/LineLength
12
+ # FIXME: make line 24 (request = "") shorter
11
13
  def log_request(duration, payload, solr_request_params={})
12
14
  return unless Supplejack.enable_debugging
13
15
 
@@ -59,11 +59,11 @@ module Supplejack
59
59
  def metadata
60
60
  metadata = []
61
61
 
62
- Supplejack.send("special_fields").each do |schema, fields|
62
+ Supplejack.send('special_fields').each do |schema, fields|
63
63
  fields[:fields].each do |field|
64
64
  if @attributes.has_key?(field)
65
65
  values = @attributes[field]
66
- values ||= [] unless !!values == values #Testing if boolean
66
+ values ||= [] unless !!values == values # Testing if boolean
67
67
  values = [values] unless values.is_a?(Array)
68
68
 
69
69
  case fields[:format]
@@ -72,7 +72,6 @@ module Supplejack
72
72
  when "camelcase" then field = field.to_s.camelcase
73
73
  end
74
74
 
75
- # field = field.to_s.camelcase(:lower) if schema == :dcterms
76
75
  field = field.to_s.sub(/#{schema}_/, '')
77
76
  values.each do |value|
78
77
  metadata << {:name => field, :schema => schema.to_s, :value => value }
@@ -85,7 +84,9 @@ module Supplejack
85
84
  end
86
85
 
87
86
  def format
88
- raise NoMethodError, "undefined method 'format' for Supplejack::Record:Module" unless @attributes.has_key?(:format)
87
+ unless @attributes.has_key?(:format)
88
+ raise NoMethodError, "undefined method 'format' for Supplejack::Record:Module"
89
+ end
89
90
  @attributes[:format]
90
91
  end
91
92
 
@@ -99,26 +100,10 @@ module Supplejack
99
100
  true
100
101
  end
101
102
 
102
- # def build_authorities(name)
103
- # @attributes[:authorities] ||= []
104
- # authorities = @attributes[:authorities].find_all {|authority| authority["name"] == name }
105
- # authorities.map {|attributes| Supplejack::Authority.new(attributes) }
106
- # end
107
-
108
103
  def method_missing(symbol, *args, &block)
109
- if symbol.to_s.match(/(.+)_authorities/)
110
- return build_authorities("#{$1}_authority")
111
- end
112
-
113
- if symbol.to_s.match(/(.+)_terms/)
114
- return build_authorities("#{$1}_term")
104
+ unless @attributes.has_key?(symbol)
105
+ raise NoMethodError, "undefined method '#{symbol.to_s}' for Supplejack::Record:Module"
115
106
  end
116
-
117
- if [:series_parent, :child_series, :collection_parent, :collection_root, :collection_mid].include?(symbol)
118
- return build_authorities(symbol.to_s)
119
- end
120
-
121
- raise NoMethodError, "undefined method '#{symbol.to_s}' for Supplejack::Record:Module" unless @attributes.has_key?(symbol)
122
107
  @attributes[symbol]
123
108
  end
124
109
 
@@ -130,7 +115,8 @@ module Supplejack
130
115
  # @params [ Hash ] options Search options used to perform a search in order to get the next/previous
131
116
  # records within the search results.
132
117
  #
133
- # @return [ Supplejack::Record ] A record or array of records initialized with the class of where the Supplejack::Record module was included
118
+ # @return [ Supplejack::Record ] A record or array of records initialized with the class of where the
119
+ # Supplejack::Record module was included
134
120
  #
135
121
  def find(id_or_array, options={})
136
122
  if id_or_array.is_a?(Array)
@@ -13,6 +13,7 @@ module Supplejack
13
13
 
14
14
  def get(path, params={}, options={})
15
15
  params ||= {}
16
+
16
17
  url = full_url(path, options[:format], params)
17
18
 
18
19
  started = Time.now
@@ -37,21 +38,30 @@ module Supplejack
37
38
  def post(path, params={}, payload={}, options={})
38
39
  payload ||= {}
39
40
  log_request(:post, path, params, payload) do
40
- response = RestClient::Request.execute(:url => full_url(path, nil, params), :method => :post, :payload => payload.to_json, :timeout => timeout(options), :headers => {:content_type => :json, :accept => :json})
41
+ response = RestClient::Request.execute(:url => full_url(path, nil, params),
42
+ :method => :post, :payload => payload.to_json,
43
+ :timeout => timeout(options),
44
+ :headers => {:content_type => :json, :accept => :json})
41
45
  JSON.parse(response) rescue {}.to_json
42
46
  end
43
47
  end
44
48
 
45
49
  def delete(path, params={}, options={})
46
50
  log_request(:delete, path, params, {}) do
47
- RestClient::Request.execute(:url => full_url(path, nil, params), :method => :delete, :timeout => timeout(options))
51
+ RestClient::Request.execute(:url => full_url(path, nil, params),
52
+ :method => :delete,
53
+ :timeout => timeout(options))
48
54
  end
49
55
  end
50
56
 
51
57
  def put(path, params={}, payload={}, options={})
52
58
  payload ||= {}
53
59
  log_request(:put, path, params, payload) do
54
- response = RestClient::Request.execute(:url => full_url(path, nil, params), :method => :put, :payload => payload.to_json, :timeout => timeout(options), :headers => {:content_type => :json, :accept => :json})
60
+ response = RestClient::Request.execute(:url => full_url(path, nil, params),
61
+ :method => :put,
62
+ :payload => payload.to_json,
63
+ :timeout => timeout(options),
64
+ :headers => {:content_type => :json, :accept => :json})
55
65
  JSON.parse(response) rescue {}.to_json
56
66
  end
57
67
  end
@@ -91,5 +101,5 @@ module Supplejack
91
101
  end
92
102
  end
93
103
 
94
- end
104
+ end
95
105
  end
@@ -9,10 +9,13 @@ require 'supplejack/request'
9
9
  require 'digest/md5'
10
10
 
11
11
  module Supplejack
12
+ # rubocop:disable Metrics/ClassLength
13
+ # FIXME: make me smaller!
12
14
  class Search
13
15
  include Supplejack::Request
14
16
 
15
- attr_accessor :results, :text, :page, :per_page, :pagination_limit, :direction, :sort, :filters, :record_type, :record_klass
17
+ attr_accessor :results, :text, :page, :per_page, :pagination_limit, :direction
18
+ attr_accessor :sort, :filters, :record_type, :record_klass, :geo_bbox
16
19
  attr_accessor :url_format, :without, :and, :or, :params, :api_params
17
20
 
18
21
  def initialize(params={})
@@ -32,6 +35,7 @@ module Supplejack
32
35
  @direction = @params[:direction]
33
36
  @url_format = Supplejack.url_format_klass.new(@params, self)
34
37
  @filters = @url_format.filters
38
+
35
39
  @api_params = @url_format.to_api_hash
36
40
  @record_klass = @params[:record_klass] || Supplejack.record_klass
37
41
 
@@ -92,7 +96,8 @@ module Supplejack
92
96
  #
93
97
  # @param [ Hash ] options Supported options: :drill_dates
94
98
  #
95
- # @return [ Array<Supplejack::Facet> ] Every element in the array is a Supplejack::Facet object, and responds to name and values
99
+ # @return [ Array<Supplejack::Facet> ] Every element in the array is a Supplejack::Facet object,
100
+ # and responds to name and values
96
101
  #
97
102
  def facets(options={})
98
103
  return @facets if @facets
@@ -155,9 +160,10 @@ module Supplejack
155
160
  # @example Returns the following hash:
156
161
  # {"photos" => 100}
157
162
  #
163
+ # rubocop:disable Metrics/LineLength
158
164
  # @param [Hash{String => Hash{String => String}}] a hash with query names as keys and a hash with filters as values.
159
165
  # @return [Hash{String => Integer}] A hash with the query names as keys and the result count for every query as values
160
- #
166
+ # rubocop:enable Metrics/LineLength
161
167
  def counts(query_parameters={})
162
168
  if Supplejack.enable_caching
163
169
  cache_key = Digest::MD5.hexdigest(counts_params(query_parameters).to_query)
@@ -196,10 +202,10 @@ module Supplejack
196
202
  query_record_type = count_filters[:record_type].to_i
197
203
  type = query_record_type == 0 ? :items : :headings
198
204
  filters = self.url_format.and_filters(type).dup
199
-
205
+
200
206
  without_filters = self.url_format.without_filters(type).dup
201
- without_filters = Hash[without_filters.map {|key, value| ["-#{key}".to_sym, value]}]
202
-
207
+ without_filters = Hash[without_filters.map {|key, value| ["-#{key}".to_sym, value]}]
208
+
203
209
  filters.merge!(without_filters)
204
210
  query_with_filters.merge!({count_name.to_sym => Supplejack::Util.deep_merge(filters, count_filters) })
205
211
  end
@@ -207,7 +213,7 @@ module Supplejack
207
213
  params = {:facet_query => query_with_filters, :record_type => "all"}
208
214
  params[:text] = self.url_format.text
209
215
  params[:text] = self.text if self.text.present?
210
- # params[:geo_bbox] = self.geo_bbox if self.geo_bbox.present?
216
+ params[:geo_bbox] = self.geo_bbox if self.geo_bbox.present?
211
217
  params[:query_fields] = self.url_format.query_fields
212
218
  params = merge_extra_filters(params)
213
219
  params
@@ -240,7 +246,7 @@ module Supplejack
240
246
  end
241
247
 
242
248
  @facet_values["All"] = response["search"]["result_count"] if options[:all]
243
-
249
+
244
250
  facet = Supplejack::Facet.new(facet_name, @facet_values)
245
251
  @facet_values = facet.values(options[:sort])
246
252
 
@@ -316,7 +322,7 @@ module Supplejack
316
322
  def categories(options={})
317
323
  return @categories if @categories
318
324
  @categories = facet_values("category", options)
319
- end
325
+ end
320
326
 
321
327
  # Convienence method to find out if the search object has any specific filter
322
328
  # applied to it. It works for both single and multiple value filters.
@@ -350,6 +356,6 @@ module Supplejack
350
356
 
351
357
  Util.deep_merge(existing_filters, extra_filters)
352
358
  end
353
-
359
+
354
360
  end
355
- end
361
+ end
@@ -8,9 +8,9 @@
8
8
  module Supplejack
9
9
  module UrlFormats
10
10
  class ItemHash
11
-
11
+
12
12
  attr_accessor :params, :search, :i_unlocked, :i_locked, :h_unlocked, :h_locked
13
-
13
+
14
14
  def initialize(params={}, search=nil)
15
15
  @params = params || {}
16
16
  @search = search
@@ -19,7 +19,7 @@ module Supplejack
19
19
  @h_unlocked = filters_of_type(:h)
20
20
  @h_locked = filters_of_type(:hl)
21
21
  end
22
-
22
+
23
23
  def to_api_hash
24
24
  hash = {}
25
25
  text_value = text(params[:text])
@@ -36,51 +36,54 @@ module Supplejack
36
36
  hash[:fields] = params[:fields] || Supplejack.fields.join(',')
37
37
  hash[:query_fields] = query_fields if query_fields
38
38
  hash[:solr_query] = params[:solr_query] if params[:solr_query].present?
39
-
39
+ hash[:ignore_metrics] = params[:ignore_metrics] if params[:ignore_metrics].present?
40
+
40
41
  if params[:sort].present?
41
42
  hash[:sort] = params[:sort]
42
43
  hash[:direction] = params[:direction] || "asc"
43
44
  end
44
-
45
+
45
46
  hash
46
47
  end
47
-
48
+
48
49
  # Returns all the active filters for the current search
49
50
  # These filters are used to scope the search results
50
51
  #
51
52
  def filters(filter_type=nil)
52
53
  filters = {}
53
-
54
+
54
55
  symbol = filter_symbol(filter_type)
55
-
56
+
56
57
  memoized_filters = self.instance_variable_get("@#{symbol}_filters")
57
58
  return memoized_filters if memoized_filters
58
-
59
+
59
60
  unlocked = filters_of_type(symbol.to_sym)
60
61
  locked = filters_of_type("#{symbol}l".to_sym)
61
-
62
+
62
63
  filters = Supplejack::Util.deep_merge!(unlocked, locked)
63
-
64
+
64
65
  @all_filters = filters.dup.symbolize_keys.to_hash rescue {}
65
66
  self.instance_variable_set("@#{symbol}_filters", @all_filters)
66
67
  @all_filters
67
68
  end
68
-
69
+
69
70
  def and_filters(filter_type=nil)
70
71
  @and_filters ||= {}
71
- @and_filters[filter_symbol(filter_type)] ||= filters(filter_type).reject {|filter, value| filter.to_s.match(/-(.+)/) or is_text_field?(filter)}
72
+ valid_filters = filters(filter_type).reject {|filter, value| filter.to_s.match(/-(.+)/)}
73
+ .reject {|filter, value| is_text_field?(filter)}
74
+ @and_filters[filter_symbol(filter_type)] ||= valid_filters
72
75
  end
73
76
 
74
77
  def is_text_field?(filter)
75
- return false if filter.nil?
78
+ return false if filter.nil? || Supplejack.non_text_fields.include?(filter.to_sym)
76
79
  filter.to_s.split(//).last(5).join('').to_s == '_text'
77
80
  end
78
-
81
+
79
82
  def without_filters(filter_type=nil)
80
83
  symbol = filter_symbol(filter_type)
81
- @without_filters ||= {}
84
+ @without_filters ||= {}
82
85
  return @without_filters[symbol] if @without_filters[symbol]
83
-
86
+
84
87
  @without_filters[symbol] = {}
85
88
  filters(filter_type).each_pair do |filter, value|
86
89
  if filter.to_s.match(/-(.+)/)
@@ -89,13 +92,13 @@ module Supplejack
89
92
  end
90
93
  @without_filters[symbol]
91
94
  end
92
-
95
+
93
96
  def all_filters
94
97
  return @all_filters if @all_filters
95
98
  self.filters
96
99
  @all_filters
97
100
  end
98
-
101
+
99
102
  def filter_symbol(filter_type=nil)
100
103
  if filter_type
101
104
  filter_type == :items ? 'i' : 'h'
@@ -103,7 +106,7 @@ module Supplejack
103
106
  params[:record_type].to_i == 0 ? 'i' : 'h'
104
107
  end
105
108
  end
106
-
109
+
107
110
  # Returns the value from the text param and joins any values from
108
111
  # fields which end in _text (ie. creator_text)
109
112
  #
@@ -112,32 +115,32 @@ module Supplejack
112
115
  def text(default_text=nil)
113
116
  text_values = []
114
117
  text_values << default_text if default_text.present?
115
-
118
+
116
119
  all_filters.each do |filter, value|
117
120
  text_values << value if is_text_field?(filter)
118
121
  end
119
-
122
+
120
123
  return nil if text_values.empty?
121
124
  text_values.join(' ')
122
125
  end
123
-
126
+
124
127
  # Returns the query_fields from the current search filters so that
125
128
  # specific fields can be searched.
126
129
  # The '_text' is removed from the end of the field name
127
130
  #
128
131
  def query_fields
129
132
  query_fields = []
130
-
133
+
131
134
  all_filters.each do |filter, value|
132
135
  if is_text_field?(filter)
133
136
  query_fields << filter.to_s.chomp!('_text').to_sym
134
137
  end
135
138
  end
136
-
139
+
137
140
  return nil if query_fields.empty?
138
141
  query_fields
139
142
  end
140
-
143
+
141
144
  # Returns one type of filters
142
145
  #
143
146
  # @param [ :i, :il, :h, :hl ] filter_type The symbol of the filter type
@@ -145,7 +148,7 @@ module Supplejack
145
148
  def filters_of_type(filter_type)
146
149
  params[filter_type].dup.symbolize_keys.to_hash rescue {}
147
150
  end
148
-
151
+
149
152
  # Returns a hash options to be used for generating URL's with all the search state.
150
153
  #
151
154
  # @param [ Hash ] filter_options A hash of options to be able to remove or add filters
@@ -153,25 +156,27 @@ module Supplejack
153
156
  # @filter_option option [ Array ] :except A array of filter names to be removed
154
157
  # @filter_options option [ Hash ] :plus A hash with filters and their values to be added
155
158
  #
159
+ # rubocop:disable Metrics/MethodLength
160
+ # FIXME: make me smaller!
156
161
  def options(filter_options={})
157
162
  filter_options.reverse_merge!({:except => [], :plus => {}})
158
163
  filter_options[:except] ||= []
159
-
164
+
160
165
  hash = {}
161
166
  {:i => :i_unlocked, :il => :i_locked, :h => :h_unlocked, :hl => :h_locked}.each_pair do |symbol, instance_name|
162
167
  filters = self.send(instance_name).clone || {}
163
-
168
+
164
169
  filters.each_pair do |name, value|
165
170
  filters.delete(name) if value.blank?
166
171
  end
167
-
172
+
168
173
  filter_options[:except].each do |exception|
169
174
  if exception.is_a?(Hash)
170
175
  facet, values_to_delete = exception.first
171
176
  values_to_delete = Util.array(values_to_delete)
172
177
  existing_values = Util.array(filters[facet])
173
178
  new_values = existing_values - values_to_delete
174
-
179
+
175
180
  if new_values.any?
176
181
  new_values = new_values.first if new_values.size == 1
177
182
  filters[facet] = new_values
@@ -182,27 +187,28 @@ module Supplejack
182
187
  filters.delete(exception)
183
188
  end
184
189
  end
185
-
190
+
186
191
  if filter_options[:plus].try(:any?) && filter_options[:plus][symbol].try(:any?)
187
192
  filters = Util.deep_merge(filters, filter_options[:plus][symbol])
188
193
  end
189
-
194
+
190
195
  hash[symbol] = filters.symbolize_keys if filters.any?
191
196
  end
192
-
197
+
193
198
  [:text, :direction, :sort].each do |attribute|
194
199
  attribute_value = search.send(attribute)
195
200
  hash.merge!(attribute => attribute_value) if attribute_value.present?
196
201
  end
197
-
202
+
198
203
  unless filter_options[:except].include?(:page)
199
204
  hash.merge!(:page => search.page) if search.page.present? && search.page != 1
200
205
  end
201
-
206
+
202
207
  hash.merge!(:record_type => 1) if search.record_type > 0
203
208
  return hash
204
209
  end
205
-
210
+ # rubocop:enable Metrics/MethodLength
211
+
206
212
  end
207
213
  end
208
214
  end
@@ -132,5 +132,10 @@ module Supplejack
132
132
  response = post("/users", {}, {user: attributes})
133
133
  new(response["user"])
134
134
  end
135
+
136
+ def self.update(attributes={})
137
+ response = put("/users/#{attributes[:api_key]}", {}, {user: attributes})
138
+ new(response["user"])
139
+ end
135
140
  end
136
141
  end
@@ -27,7 +27,8 @@ module Supplejack
27
27
  include ActiveModel::Conversion
28
28
  extend ActiveModel::Naming
29
29
 
30
- ATTRIBUTES = [:id, :name, :description, :privacy, :url, :priority, :count, :tags, :tag_list, :homepage, :records, :created_at, :updated_at, :approved, :record]
30
+ ATTRIBUTES = [:id, :name, :description, :privacy, :url, :priority, :count, :tags, :tag_list,
31
+ :featured, :records, :created_at, :updated_at, :approved, :record]
31
32
  attr_accessor *ATTRIBUTES
32
33
  attr_accessor :api_key, :errors, :user
33
34
 
@@ -55,7 +56,7 @@ module Supplejack
55
56
  #
56
57
  def api_attributes
57
58
  api_attributes = {}
58
- [:name, :description, :privacy, :priority, :tag_list, :homepage, :approved].each do |attr|
59
+ [:name, :description, :privacy, :priority, :tag_list, :featured, :approved].each do |attr|
59
60
  api_attributes[attr] = self.send(attr)
60
61
  end
61
62
  api_attributes[:records] = self.api_records
@@ -203,7 +204,7 @@ module Supplejack
203
204
  # @return [ Array ] A array of hashes in the format {record_id: 1, position: 1}
204
205
  #
205
206
  # @example
206
- # user_set.ordered_records_from_array([89,66]) => [{record_id: 89, position: 1}, {record_id: 66, position: 2}]
207
+ # user_set.ordered_records_from_array([89,66]) => [{record_id: 89, position: 1}, {record_id: 66, position: 2}]
207
208
  #
208
209
  def ordered_records_from_array(record_ids)
209
210
  records = []
@@ -313,16 +314,19 @@ module Supplejack
313
314
  response = get("/sets/public", options)
314
315
  sets_array = response["sets"] || []
315
316
  user_sets = sets_array.map {|attrs| new(attrs) }
316
- Supplejack::PaginatedCollection.new(user_sets, options[:page].to_i, options[:per_page].to_i, response["total"].to_i)
317
+ Supplejack::PaginatedCollection.new(user_sets,
318
+ options[:page].to_i,
319
+ options[:per_page].to_i,
320
+ response["total"].to_i)
317
321
  end
318
322
 
319
- # Execute a GET request to the API /sets/home endpoint to retrieve
320
- # all UserSet objects which have the :homepage flag set to true
323
+ # Execute a GET request to the API /sets/featured endpoint to retrieve
324
+ # all UserSet objects which have the :featured flag set to true
321
325
  #
322
326
  # @return [ Array ] A array of Supplejack::UserSet objects
323
327
  #
324
- def self.homepage_sets
325
- path = "/sets/home"
328
+ def self.featured_sets
329
+ path = "/sets/featured"
326
330
  if Supplejack.enable_caching
327
331
  response = Rails.cache.fetch(path, expires_in: 1.day) do
328
332
  get(path)
@@ -117,4 +117,4 @@ module Supplejack
117
117
  end
118
118
 
119
119
  end
120
- end
120
+ end
@@ -6,5 +6,5 @@
6
6
  # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
7
 
8
8
  module Supplejack
9
- VERSION = '1.0.4'
9
+ VERSION = '1.0.5'
10
10
  end
@@ -10,11 +10,12 @@ require 'supplejack/version'
10
10
 
11
11
  module Supplejack
12
12
  extend Config
13
-
13
+
14
14
  require 'supplejack/engine'
15
15
  require 'supplejack/exceptions'
16
16
  require 'supplejack/log_subscriber'
17
17
  require 'supplejack/record'
18
+ require 'supplejack/concept'
18
19
  require "supplejack/request"
19
20
  require 'supplejack/paginated_collection'
20
21
  require 'supplejack/controllers/helpers'
data/spec/spec_helper.rb CHANGED
@@ -7,12 +7,18 @@
7
7
 
8
8
  require 'rubygems'
9
9
  require 'bundler/setup'
10
+ require 'simplecov'
10
11
 
11
12
  Bundler.require(:default)
12
13
 
13
14
  require 'supplejack_client'
14
15
  require 'active_support/all'
15
16
 
17
+ require "codeclimate-test-reporter"
18
+ CodeClimate::TestReporter.start
19
+
20
+ #SimpleCov.start
21
+
16
22
  RSpec.configure do |config|
17
23
  # some (optional) config here
18
24
  config.mock_with :rspec