supplejack_api 1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +80 -0
- data/Rakefile +42 -0
- data/app/assets/javascripts/supplejack_api/application.js +26 -0
- data/app/assets/stylesheets/supplejack_api/application.css +30 -0
- data/app/controllers/supplejack_api/admin/base_controller.rb +24 -0
- data/app/controllers/supplejack_api/admin/sessions_controller.rb +25 -0
- data/app/controllers/supplejack_api/admin/site_activities_controller.rb +21 -0
- data/app/controllers/supplejack_api/admin/users_controller.rb +55 -0
- data/app/controllers/supplejack_api/application_controller.rb +70 -0
- data/app/controllers/supplejack_api/concepts_controller.rb +52 -0
- data/app/controllers/supplejack_api/concerns/ignore_metrics.rb +14 -0
- data/app/controllers/supplejack_api/concerns/records_controller_metrics.rb +26 -0
- data/app/controllers/supplejack_api/concerns/set_items_controller_metrics.rb +20 -0
- data/app/controllers/supplejack_api/concerns/user_sets_controller_metrics.rb +26 -0
- data/app/controllers/supplejack_api/harvester/concepts_controller.rb +41 -0
- data/app/controllers/supplejack_api/harvester/fragments_controller.rb +30 -0
- data/app/controllers/supplejack_api/harvester/records_controller.rb +68 -0
- data/app/controllers/supplejack_api/metrics_api_controller.rb +44 -0
- data/app/controllers/supplejack_api/partners_controller.rb +39 -0
- data/app/controllers/supplejack_api/records_controller.rb +71 -0
- data/app/controllers/supplejack_api/schema_controller.rb +19 -0
- data/app/controllers/supplejack_api/set_items_controller.rb +38 -0
- data/app/controllers/supplejack_api/sources_controller.rb +67 -0
- data/app/controllers/supplejack_api/status_controller.rb +67 -0
- data/app/controllers/supplejack_api/user_sets_controller.rb +97 -0
- data/app/controllers/supplejack_api/users_controller.rb +49 -0
- data/app/helpers/supplejack_api/application_helper.rb +39 -0
- data/app/helpers/supplejack_api/facets_helper.rb +30 -0
- data/app/mailers/supplejack_api/request_limit_mailer.rb +43 -0
- data/app/models/supplejack_api/api_concept/concept_fragment.rb +31 -0
- data/app/models/supplejack_api/api_record/record_fragment.rb +15 -0
- data/app/models/supplejack_api/concept.rb +46 -0
- data/app/models/supplejack_api/concept_search.rb +86 -0
- data/app/models/supplejack_api/concerns/queryable_by_date.rb +31 -0
- data/app/models/supplejack_api/concerns/record.rb +108 -0
- data/app/models/supplejack_api/concerns/record_fragmentable.rb +24 -0
- data/app/models/supplejack_api/concerns/searchable.rb +430 -0
- data/app/models/supplejack_api/concerns/user_set.rb +219 -0
- data/app/models/supplejack_api/daily_metrics.rb +20 -0
- data/app/models/supplejack_api/faceted_metrics.rb +55 -0
- data/app/models/supplejack_api/fragment.rb +82 -0
- data/app/models/supplejack_api/interaction_models/record.rb +60 -0
- data/app/models/supplejack_api/interaction_models/set.rb +13 -0
- data/app/models/supplejack_api/interaction_updaters/all_usage_metric.rb +41 -0
- data/app/models/supplejack_api/interaction_updaters/set_metrics.rb +32 -0
- data/app/models/supplejack_api/interaction_updaters/usage_metrics.rb +73 -0
- data/app/models/supplejack_api/partner.rb +24 -0
- data/app/models/supplejack_api/preview_record.rb +30 -0
- data/app/models/supplejack_api/record.rb +18 -0
- data/app/models/supplejack_api/record_search.rb +12 -0
- data/app/models/supplejack_api/schema_definition.rb +147 -0
- data/app/models/supplejack_api/search.rb +14 -0
- data/app/models/supplejack_api/set_item.rb +57 -0
- data/app/models/supplejack_api/site_activity.rb +59 -0
- data/app/models/supplejack_api/source.rb +28 -0
- data/app/models/supplejack_api/source_activity.rb +36 -0
- data/app/models/supplejack_api/source_authority.rb +42 -0
- data/app/models/supplejack_api/supplejack_schema.rb +37 -0
- data/app/models/supplejack_api/support/concept/searchable.rb +79 -0
- data/app/models/supplejack_api/support/concept/storable.rb +59 -0
- data/app/models/supplejack_api/support/fragment_helpers.rb +96 -0
- data/app/models/supplejack_api/support/harvestable.rb +105 -0
- data/app/models/supplejack_api/support/searchable.rb +115 -0
- data/app/models/supplejack_api/support/status_logger.rb +23 -0
- data/app/models/supplejack_api/support/storable.rb +49 -0
- data/app/models/supplejack_api/support/validation_logger.rb +23 -0
- data/app/models/supplejack_api/usage_metrics.rb +25 -0
- data/app/models/supplejack_api/user.rb +211 -0
- data/app/models/supplejack_api/user_activity.rb +65 -0
- data/app/models/supplejack_api/user_set.rb +17 -0
- data/app/serializers/supplejack_api/application_serializer.rb +12 -0
- data/app/serializers/supplejack_api/concept_record_serializer.rb +23 -0
- data/app/serializers/supplejack_api/concept_search_serializer.rb +12 -0
- data/app/serializers/supplejack_api/concept_serializer.rb +100 -0
- data/app/serializers/supplejack_api/concerns/record_serializable.rb +119 -0
- data/app/serializers/supplejack_api/concerns/search_serializable.rb +24 -0
- data/app/serializers/supplejack_api/record_search_serializer.rb +64 -0
- data/app/serializers/supplejack_api/record_serializer.rb +13 -0
- data/app/serializers/supplejack_api/search_serializer.rb +13 -0
- data/app/serializers/supplejack_api/source_authority_serializer.rb +23 -0
- data/app/serializers/supplejack_api/user_serializer.rb +13 -0
- data/app/serializers/supplejack_api/user_set_record_serializer.rb +14 -0
- data/app/serializers/supplejack_api/user_set_serializer.rb +77 -0
- data/app/services/metrics_api/v3/api.rb +30 -0
- data/app/services/metrics_api/v3/endpoints/facets.rb +23 -0
- data/app/services/metrics_api/v3/endpoints/global.rb +26 -0
- data/app/services/metrics_api/v3/endpoints/helpers.rb +14 -0
- data/app/services/metrics_api/v3/endpoints/root.rb +97 -0
- data/app/services/metrics_api/v3/presenters/daily_metric.rb +23 -0
- data/app/services/metrics_api/v3/presenters/extended_metadata.rb +41 -0
- data/app/services/metrics_api/v3/presenters/record.rb +26 -0
- data/app/services/metrics_api/v3/presenters/view.rb +28 -0
- data/app/views/layouts/supplejack_api/_head.html.erb +17 -0
- data/app/views/layouts/supplejack_api/_top_nav.html.erb +33 -0
- data/app/views/layouts/supplejack_api/application.html.erb +33 -0
- data/app/views/supplejack_api/admin/sessions/new.html.erb +25 -0
- data/app/views/supplejack_api/admin/shared/_links.erb +34 -0
- data/app/views/supplejack_api/admin/site_activities/index.csv.erb +22 -0
- data/app/views/supplejack_api/admin/site_activities/index.html.erb +54 -0
- data/app/views/supplejack_api/admin/users/edit.html.erb +16 -0
- data/app/views/supplejack_api/admin/users/index.csv.erb +22 -0
- data/app/views/supplejack_api/admin/users/index.html.erb +57 -0
- data/app/views/supplejack_api/admin/users/show.html.erb +33 -0
- data/app/views/supplejack_api/request_limit_mailer/at100percent.text.erb +13 -0
- data/app/views/supplejack_api/request_limit_mailer/at100percent_admin.text.erb +1 -0
- data/app/views/supplejack_api/request_limit_mailer/at90percent.text.erb +13 -0
- data/app/views/supplejack_api/request_limit_mailer/at90percent_admin.text.erb +1 -0
- data/app/workers/supplejack_api/daily_metrics_worker.rb +112 -0
- data/app/workers/supplejack_api/interaction_metrics_worker.rb +45 -0
- data/config/cucumber.yml +16 -0
- data/config/initializers/force_eagerload.rb +11 -0
- data/config/initializers/interaction_updaters.rb +7 -0
- data/config/routes.rb +81 -0
- data/db/binding_records.development.json +7 -0
- data/db/binding_records.staging.json +7 -0
- data/db/concepts.json +33 -0
- data/db/concepts_data.json +569745 -0
- data/db/source_authorities.json +119 -0
- data/lib/generators/supplejack_api/install_generator.rb +124 -0
- data/lib/mongoid/paperclip.rb +53 -0
- data/lib/mongoid/string.rb +20 -0
- data/lib/sunspot/mongoid.rb +51 -0
- data/lib/sunspot/resque_session_proxy.rb +72 -0
- data/lib/sunspot/sunspot_spellcheck.rb +98 -0
- data/lib/supplejack_api.rb +13 -0
- data/lib/supplejack_api/admin/sortable.rb +40 -0
- data/lib/supplejack_api/engine.rb +46 -0
- data/lib/supplejack_api/harvester_constraint.rb +28 -0
- data/lib/supplejack_api/stylesheets.rb +36 -0
- data/lib/supplejack_api/utils.rb +75 -0
- data/lib/supplejack_api/version.rb +11 -0
- data/lib/tasks/resque.rake +22 -0
- data/lib/url_validator.rb +38 -0
- data/spec/controllers/supplejack_api/admin/base_controller_spec.rb +55 -0
- data/spec/controllers/supplejack_api/admin/site_activities_controller_spec.rb +43 -0
- data/spec/controllers/supplejack_api/admin/users_controller_spec.rb +79 -0
- data/spec/controllers/supplejack_api/application_controller_spec.rb +133 -0
- data/spec/controllers/supplejack_api/concepts_controller_spec.rb +83 -0
- data/spec/controllers/supplejack_api/concerns/records_controller_metrics_spec.rb +60 -0
- data/spec/controllers/supplejack_api/harvester/concepts_controller_spec.rb +57 -0
- data/spec/controllers/supplejack_api/harvester/records_controller_spec.rb +138 -0
- data/spec/controllers/supplejack_api/metrics_api_controller_spec.rb +71 -0
- data/spec/controllers/supplejack_api/partners_controller_spec.rb +77 -0
- data/spec/controllers/supplejack_api/records_controller_spec.rb +143 -0
- data/spec/controllers/supplejack_api/set_items_controller_spec.rb +78 -0
- data/spec/controllers/supplejack_api/sources_controller_spec.rb +161 -0
- data/spec/controllers/supplejack_api/status_controller_spec.rb +120 -0
- data/spec/controllers/supplejack_api/user_sets_controller_spec.rb +233 -0
- data/spec/controllers/supplejack_api/users_controller_spec.rb +61 -0
- data/spec/dummy/README.rdoc +268 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +23 -0
- data/spec/dummy/app/assets/stylesheets/application.css +22 -0
- data/spec/dummy/app/controllers/application_controller.rb +10 -0
- data/spec/dummy/app/helpers/application_helper.rb +9 -0
- data/spec/dummy/app/supplejack_api/concept_schema.rb +83 -0
- data/spec/dummy/app/supplejack_api/record_schema.rb +64 -0
- data/spec/dummy/app/supplejack_api/record_schema.txt +66 -0
- data/spec/dummy/app/views/layouts/application.html.erb +23 -0
- data/spec/dummy/app/workers/supplejack_api/clear_index_buffer.rb +32 -0
- data/spec/dummy/app/workers/supplejack_api/flush_old_records_worker.rb +17 -0
- data/spec/dummy/app/workers/supplejack_api/index_buffer.rb +49 -0
- data/spec/dummy/app/workers/supplejack_api/index_source_worker.rb +39 -0
- data/spec/dummy/app/workers/supplejack_api/index_worker.rb +82 -0
- data/spec/dummy/app/workers/supplejack_api/store_user_activity_worker.rb +24 -0
- data/spec/dummy/config.ru +11 -0
- data/spec/dummy/config/application.rb +76 -0
- data/spec/dummy/config/application.yml +31 -0
- data/spec/dummy/config/application.yml.example +36 -0
- data/spec/dummy/config/boot.rb +17 -0
- data/spec/dummy/config/environment.rb +12 -0
- data/spec/dummy/config/environments/development.rb +42 -0
- data/spec/dummy/config/environments/production.rb +76 -0
- data/spec/dummy/config/environments/test.rb +46 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +14 -0
- data/spec/dummy/config/initializers/devise.rb +214 -0
- data/spec/dummy/config/initializers/inflections.rb +22 -0
- data/spec/dummy/config/initializers/kaminari_config.rb +17 -0
- data/spec/dummy/config/initializers/mime_types.rb +12 -0
- data/spec/dummy/config/initializers/mongoid.rb +9 -0
- data/spec/dummy/config/initializers/quiet_logger.rb +23 -0
- data/spec/dummy/config/initializers/resque.rb +18 -0
- data/spec/dummy/config/initializers/secret_token.rb +14 -0
- data/spec/dummy/config/initializers/session_store.rb +15 -0
- data/spec/dummy/config/initializers/simple_form.rb +149 -0
- data/spec/dummy/config/initializers/simple_form_foundation.rb +33 -0
- data/spec/dummy/config/initializers/state_machine.rb +8 -0
- data/spec/dummy/config/initializers/sunspot.rb +32 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +17 -0
- data/spec/dummy/config/locales/devise.en.yml +63 -0
- data/spec/dummy/config/locales/en.yml +66 -0
- data/spec/dummy/config/locales/simple_form.en.yml +33 -0
- data/spec/dummy/config/mongoid.travis.yml +34 -0
- data/spec/dummy/config/mongoid.yml +36 -0
- data/spec/dummy/config/resque-pool.yml +18 -0
- data/spec/dummy/config/resque_schedule.yml +12 -0
- data/spec/dummy/config/routes.rb +11 -0
- data/spec/dummy/config/secrets.yml +8 -0
- data/spec/dummy/config/sunspot.yml +38 -0
- data/spec/dummy/db/concepts.json +37 -0
- data/spec/dummy/db/source_authorities.json +116 -0
- data/spec/dummy/public/404.html +35 -0
- data/spec/dummy/public/422.html +35 -0
- data/spec/dummy/public/500.html +34 -0
- data/spec/dummy/public/favicon.ico +8 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/solr/collection1/conf/elevate.xml +36 -0
- data/spec/dummy/solr/collection1/conf/mapping-FoldToASCII.txt +3813 -0
- data/spec/dummy/solr/collection1/conf/schema.xml +292 -0
- data/spec/dummy/solr/collection1/conf/solrconfig.xml +1780 -0
- data/spec/dummy/solr/collection1/conf/spellings.txt +2 -0
- data/spec/dummy/solr/collection1/conf/stopwords.txt +58 -0
- data/spec/dummy/solr/collection1/conf/synonyms.txt +31 -0
- data/spec/factories/concepts.rb +16 -0
- data/spec/factories/daily_metrics.rb +10 -0
- data/spec/factories/faceted_metrics.rb +25 -0
- data/spec/factories/fragment.rb +14 -0
- data/spec/factories/partners.rb +14 -0
- data/spec/factories/record_interaction.rb +17 -0
- data/spec/factories/records.rb +47 -0
- data/spec/factories/set_interaction.rb +15 -0
- data/spec/factories/source_authorities.rb +19 -0
- data/spec/factories/sources.rb +16 -0
- data/spec/factories/usage_metrics.rb +20 -0
- data/spec/factories/user_activities.rb +14 -0
- data/spec/factories/user_sets.rb +27 -0
- data/spec/factories/users.rb +23 -0
- data/spec/helpers/application_helper_spec.rb +35 -0
- data/spec/models/concept_spec.rb +163 -0
- data/spec/models/fragment_spec.rb +15 -0
- data/spec/models/site_activity_spec.rb +103 -0
- data/spec/models/source_activity_spec.rb +56 -0
- data/spec/models/source_authority_spec.rb +42 -0
- data/spec/models/supplejack_api/api_concept/concept_fragment_spec.rb +189 -0
- data/spec/models/supplejack_api/api_record/record_fragment_spec.rb +189 -0
- data/spec/models/supplejack_api/concept_spec.rb +0 -0
- data/spec/models/supplejack_api/interaction_updaters/all_usage_metric_spec.rb +41 -0
- data/spec/models/supplejack_api/interaction_updaters/set_metrics_spec.rb +32 -0
- data/spec/models/supplejack_api/interaction_updaters/usage_metrics_spec.rb +44 -0
- data/spec/models/supplejack_api/partner_spec.rb +20 -0
- data/spec/models/supplejack_api/record_search_spec.rb +429 -0
- data/spec/models/supplejack_api/record_spec.rb +280 -0
- data/spec/models/supplejack_api/schema_definition_spec.rb +272 -0
- data/spec/models/supplejack_api/search_spec.rb +243 -0
- data/spec/models/supplejack_api/set_item_spec.rb +123 -0
- data/spec/models/supplejack_api/source_spec.rb +22 -0
- data/spec/models/supplejack_api/support/fragment_helpers_spec.rb +216 -0
- data/spec/models/supplejack_api/support/harvestable_spec.rb +177 -0
- data/spec/models/supplejack_api/support/searchable_spec.rb +85 -0
- data/spec/models/supplejack_api/support/storable_spec.rb +18 -0
- data/spec/models/supplejack_api/user_activity_spec.rb +88 -0
- data/spec/models/supplejack_api/user_set_spec.rb +559 -0
- data/spec/models/supplejack_api/user_spec.rb +348 -0
- data/spec/routing/concepts_routing_spec.rb +22 -0
- data/spec/routing/harvester_constraint_spec.rb +66 -0
- data/spec/routing/harvester_routing_spec.rb +51 -0
- data/spec/routing/partner_routing_spec.rb +34 -0
- data/spec/routing/records_routing_spec.rb +26 -0
- data/spec/routing/sets_routing_spec.rb +44 -0
- data/spec/routing/source_routing_spec.rb +38 -0
- data/spec/routing/users_routing_spec.rb +18 -0
- data/spec/serializers/concept_record_serializer_spec.rb +30 -0
- data/spec/serializers/supplejack_api/application_serializer_spec.rb +14 -0
- data/spec/serializers/supplejack_api/concept_search_serializer_spec.rb +34 -0
- data/spec/serializers/supplejack_api/concept_serializer_spec.rb +92 -0
- data/spec/serializers/supplejack_api/record_search_serializer_spec.rb +34 -0
- data/spec/serializers/supplejack_api/record_serializer_spec.rb +271 -0
- data/spec/serializers/supplejack_api/search_serializer_spec.rb +90 -0
- data/spec/serializers/supplejack_api/user_serializer_spec.rb +22 -0
- data/spec/serializers/supplejack_api/user_set_serializer_spec.rb +96 -0
- data/spec/services/metrics_api/v3/api_spec.rb +23 -0
- data/spec/services/metrics_api/v3/endpoints/facets_spec.rb +25 -0
- data/spec/services/metrics_api/v3/endpoints/global_spec.rb +22 -0
- data/spec/services/metrics_api/v3/endpoints/root_spec.rb +49 -0
- data/spec/services/metrics_api/v3/presenters/extended_metadata_spec.rb +49 -0
- data/spec/spec_helper.rb +70 -0
- data/spec/support/api/schemas/metrics/daily_metrics_metadata.json +24 -0
- data/spec/support/api/schemas/metrics/extended_metrics.json +26 -0
- data/spec/support/api/schemas/metrics/extended_response.json +6 -0
- data/spec/support/api/schemas/metrics/facets_response.json +6 -0
- data/spec/support/api/schemas/metrics/record_metadata.json +33 -0
- data/spec/support/api/schemas/metrics/top_level_response.json +6 -0
- data/spec/support/api/schemas/metrics/view_metadata.json +33 -0
- data/spec/support/api_schema_matcher.rb +14 -0
- data/spec/support/devise.rb +10 -0
- data/spec/workers/supplejack_api/daily_metrics_worker_spec.rb +140 -0
- data/spec/workers/supplejack_api/flush_old_records_worker_spec.rb +20 -0
- data/spec/workers/supplejack_api/index_worker_spec.rb +52 -0
- data/spec/workers/supplejack_api/source_index_worker_spec.rb +61 -0
- metadata +1231 -0
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class UserActivity
|
11
|
+
include Mongoid::Document
|
12
|
+
include Mongoid::Timestamps
|
13
|
+
|
14
|
+
store_in collection: 'user_activities'
|
15
|
+
|
16
|
+
belongs_to :user, class_name: 'SupplejackApi::User'
|
17
|
+
|
18
|
+
field :user_sets, type: Hash
|
19
|
+
field :search, type: Hash
|
20
|
+
field :records, type: Hash
|
21
|
+
field :custom_searches, type: Hash
|
22
|
+
field :total, type: Integer
|
23
|
+
|
24
|
+
index created_at: 1
|
25
|
+
|
26
|
+
def self.build_from_user(daily_activity)
|
27
|
+
user_activity = new
|
28
|
+
daily_activity ||= {}
|
29
|
+
|
30
|
+
%w(user_sets search records custom_searches).each do |group|
|
31
|
+
group_activity = daily_activity[group]
|
32
|
+
|
33
|
+
if group_activity
|
34
|
+
user_activity[group] = group_activity
|
35
|
+
user_activity.calculate_total_for(group)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
user_activity.calculate_total
|
40
|
+
user_activity
|
41
|
+
end
|
42
|
+
|
43
|
+
def calculate_total_for(field)
|
44
|
+
total = 0
|
45
|
+
|
46
|
+
if activities = self[field]
|
47
|
+
activities.each { |_action, count| total += count.to_i }
|
48
|
+
else
|
49
|
+
activities = {}
|
50
|
+
end
|
51
|
+
|
52
|
+
activities['total'] = total
|
53
|
+
self[field] = activities
|
54
|
+
end
|
55
|
+
|
56
|
+
def calculate_total
|
57
|
+
count = 0
|
58
|
+
%w(user_sets search records custom_searches).each do |group|
|
59
|
+
values = send(group)
|
60
|
+
count += values['total'].to_i if values && values.is_a?(Hash)
|
61
|
+
end
|
62
|
+
self.total = count
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class UserSet
|
11
|
+
include Mongoid::Document
|
12
|
+
include Mongoid::Timestamps
|
13
|
+
include ActionView::Helpers::SanitizeHelper
|
14
|
+
include ActiveModel::MassAssignmentSecurity
|
15
|
+
include SupplejackApi::Concerns::UserSet
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class ApplicationSerializer < ActiveModel::Serializer
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class ConceptRecordSerializer < ActiveModel::Serializer
|
11
|
+
attributes :title, :description, :date, :display_content_partner, :display_collection, :thumbnail_url
|
12
|
+
|
13
|
+
TYPE_PROXY = 'edm:ProvidedCHO'
|
14
|
+
|
15
|
+
def serializable_hash
|
16
|
+
hash = {}
|
17
|
+
hash['@id'] = "http://#{ENV['WWW_DOMAIN']}/records/#{object.record_id}"
|
18
|
+
hash['@type'] = TYPE_PROXY
|
19
|
+
hash = hash.merge!(attributes)
|
20
|
+
hash
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class ConceptSearchSerializer < SearchSerializer
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class ConceptSerializer < ActiveModel::Serializer
|
11
|
+
has_many :source_authorities, serializer: SupplejackApi::SourceAuthoritySerializer
|
12
|
+
has_many :records, serializer: SupplejackApi::ConceptRecordSerializer
|
13
|
+
|
14
|
+
ConceptSchema.groups.keys.each do |group|
|
15
|
+
define_method("#{group}?") do
|
16
|
+
return false unless options[:groups].try(:any?)
|
17
|
+
options[:groups].include?(group)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def serializable_hash
|
22
|
+
hash = attributes
|
23
|
+
# Create empty context block to add to once we know what fields we have.
|
24
|
+
hash['@context'] = {}
|
25
|
+
hash['@type'] = object.concept_type
|
26
|
+
hash['@id'] = object.site_id
|
27
|
+
|
28
|
+
if default? || verbose?
|
29
|
+
groups = (options[:groups] & ConceptSchema.groups.keys) || []
|
30
|
+
fields = Set.new
|
31
|
+
|
32
|
+
groups.each do |group|
|
33
|
+
fields.merge(ConceptSchema.groups[group].try(:fields)) rescue {}
|
34
|
+
end
|
35
|
+
|
36
|
+
fields.each do |field|
|
37
|
+
# Doesn't allow the real record id to be overwritten by mongo id
|
38
|
+
hash[field] = field == :id ? hash[field] : field_value(field, options)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
include_individual_fields!(hash)
|
43
|
+
include_context_fields!(hash)
|
44
|
+
include_reverse_fields!(hash) if reverse?
|
45
|
+
include!(:source_authorities, node: hash) if source_authorities?
|
46
|
+
hash
|
47
|
+
end
|
48
|
+
|
49
|
+
def include_context_fields!(hash)
|
50
|
+
fields = hash.dup
|
51
|
+
fields.keep_if { |field| ConceptSchema.model_fields.include?(field) }
|
52
|
+
field_keys = fields.keys
|
53
|
+
|
54
|
+
# Include unstored fields from the Schema
|
55
|
+
record_fields = ConceptSchema.model_fields.select { |_key, value| value.try(:store) == false }
|
56
|
+
field_keys += record_fields.keys
|
57
|
+
|
58
|
+
hash['@context'] = if options[:inline_context]
|
59
|
+
Concept.build_context(field_keys)
|
60
|
+
else
|
61
|
+
object.context
|
62
|
+
end
|
63
|
+
hash
|
64
|
+
end
|
65
|
+
|
66
|
+
def include_individual_fields!(hash)
|
67
|
+
if options[:fields].present?
|
68
|
+
options[:fields].push(:concept_id)
|
69
|
+
options[:fields].sort!
|
70
|
+
options[:fields].each do |field|
|
71
|
+
hash[field] = object.send(field)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
hash
|
75
|
+
end
|
76
|
+
|
77
|
+
def include_reverse_fields!(hash)
|
78
|
+
hash['@reverse'] = {}
|
79
|
+
key = concept.edm_type
|
80
|
+
include!(:records, node: hash['@reverse'], key: key)
|
81
|
+
hash
|
82
|
+
end
|
83
|
+
|
84
|
+
def field_value(field)
|
85
|
+
value = if ConceptSchema.fields[field].try(:search_value) && ConceptSchema.fields[field].try(:store) == false
|
86
|
+
ConceptSchema.fields[field].search_value.call(object)
|
87
|
+
else
|
88
|
+
object.public_send(field)
|
89
|
+
end
|
90
|
+
|
91
|
+
value = ConceptSchema.fields[field].try(:default_value) if value.nil? rescue nil
|
92
|
+
|
93
|
+
if ConceptSchema.fields[field].try(:date_format)
|
94
|
+
value = format_date(value, ConceptSchema.fields[field].try(:date_format))
|
95
|
+
end
|
96
|
+
|
97
|
+
value
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module SupplejackApi::Concerns::RecordSerializable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
RecordSchema.groups.keys.each do |group|
|
9
|
+
define_method("#{group}?") do
|
10
|
+
return false unless options[:groups].try(:any?)
|
11
|
+
options[:groups].include?(group)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_xml(*_args)
|
16
|
+
serializable_hash.to_xml(root: 'record')
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove_restricted_fields!(hash)
|
20
|
+
role_field_restrictions.each do |conditional_field, restrictions|
|
21
|
+
restrictions.each do |condition, restricted_fields|
|
22
|
+
if field_restricted?(conditional_field, condition)
|
23
|
+
remove_field_values(restricted_fields, hash)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# REFACTOR -- Used in concept_serializer.rb too
|
30
|
+
def field_value(field, _options = {})
|
31
|
+
value = if RecordSchema.fields[field].try(:search_value) && RecordSchema.fields[field].try(:store) == false
|
32
|
+
RecordSchema.fields[field].search_value.call(object)
|
33
|
+
else
|
34
|
+
object.public_send(field)
|
35
|
+
end
|
36
|
+
|
37
|
+
value = RecordSchema.fields[field].try(:default_value) if value.nil? rescue nil
|
38
|
+
|
39
|
+
if RecordSchema.fields[field].try(:date_format)
|
40
|
+
value = format_date(value, RecordSchema.fields[field].try(:date_format))
|
41
|
+
end
|
42
|
+
|
43
|
+
value
|
44
|
+
end
|
45
|
+
|
46
|
+
def role
|
47
|
+
@role ||= options[:scope].role.to_sym rescue nil
|
48
|
+
end
|
49
|
+
|
50
|
+
def format_date(date, format)
|
51
|
+
date.strftime(format) rescue date
|
52
|
+
end
|
53
|
+
|
54
|
+
def role_field_restrictions
|
55
|
+
restrictions = []
|
56
|
+
|
57
|
+
if role && RecordSchema.roles[role] && RecordSchema.roles[role].field_restrictions.present?
|
58
|
+
restrictions = RecordSchema.roles[role].field_restrictions
|
59
|
+
end
|
60
|
+
|
61
|
+
restrictions
|
62
|
+
end
|
63
|
+
|
64
|
+
def field_restricted?(conditional_field, condition)
|
65
|
+
field_values = field_value(conditional_field).to_a
|
66
|
+
restricted = false
|
67
|
+
|
68
|
+
field_values.each do |value|
|
69
|
+
if (condition.is_a?(Regexp) && value.match(condition)) ||
|
70
|
+
(condition.is_a?(String) && value.include?(condition))
|
71
|
+
restricted = true
|
72
|
+
end
|
73
|
+
break if restricted
|
74
|
+
end
|
75
|
+
|
76
|
+
restricted
|
77
|
+
end
|
78
|
+
|
79
|
+
def remove_field_values(restricted_fields, hash)
|
80
|
+
restricted_fields.each do |field|
|
81
|
+
hash[field.to_sym] = nil
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns a hash including all desirable record attributes and its associations
|
86
|
+
def serializable_hash
|
87
|
+
hash = attributes
|
88
|
+
groups = (options[:groups] & RecordSchema.groups.keys) || []
|
89
|
+
fields = Set.new
|
90
|
+
groups.each do |group|
|
91
|
+
fields.merge(RecordSchema.groups[group].try(:fields))
|
92
|
+
end
|
93
|
+
|
94
|
+
# hash[:id] = field_value(:record_id, options) if fields.any?
|
95
|
+
fields.each do |field|
|
96
|
+
hash[field] = field_value(field, options)
|
97
|
+
end
|
98
|
+
|
99
|
+
include_individual_fields!(hash)
|
100
|
+
remove_restricted_fields!(hash)
|
101
|
+
|
102
|
+
hash[:next_page] = object.next_page if object.next_page.present?
|
103
|
+
hash[:next_record] = object.next_record if object.next_record.present?
|
104
|
+
hash[:previous_page] = object.previous_page if object.previous_page.present?
|
105
|
+
hash[:previous_record] = object.previous_record if object.previous_record.present?
|
106
|
+
|
107
|
+
hash
|
108
|
+
end
|
109
|
+
|
110
|
+
def include_individual_fields!(hash)
|
111
|
+
if options[:fields].present?
|
112
|
+
options[:fields].each do |field|
|
113
|
+
hash[field] = record.send(field)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
hash
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module SupplejackApi::Concerns::SearchSerializable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
def serializable_hash
|
7
|
+
hash = {}
|
8
|
+
hash[:result_count] = object.total
|
9
|
+
hash[:results] = records_serialized_array
|
10
|
+
hash[:per_page] = object.per_page
|
11
|
+
hash[:page] = object.page
|
12
|
+
hash[:request_url] = object.request_url
|
13
|
+
hash[:solr_request_params] = object.solr_request_params if object.solr_request_params
|
14
|
+
hash[:warnings] = object.warnings if object.warnings.present?
|
15
|
+
hash[:suggestion] = object.collation if object.options[:suggest]
|
16
|
+
hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def records_serialized_array
|
20
|
+
args = { fields: object.field_list, groups: object.group_list, scope: object.scope }
|
21
|
+
ActiveModel::ArraySerializer.new(object.results, args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
7
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
8
|
+
|
9
|
+
module SupplejackApi
|
10
|
+
class RecordSearchSerializer < SearchSerializer
|
11
|
+
RecordSchema.groups.keys.each do |group|
|
12
|
+
define_method("#{group}?") do
|
13
|
+
return false unless options[:groups].try(:any?)
|
14
|
+
options[:groups].include?(group)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def json_facets
|
19
|
+
facets = {}
|
20
|
+
object.facets.map do |facet|
|
21
|
+
rows = {}
|
22
|
+
facet.rows.each do |row|
|
23
|
+
rows[row.value] = row.count
|
24
|
+
end
|
25
|
+
|
26
|
+
facets.merge!(facet.name => rows)
|
27
|
+
end
|
28
|
+
facets
|
29
|
+
end
|
30
|
+
|
31
|
+
def xml_facets
|
32
|
+
facets = []
|
33
|
+
object.facets.map do |facet|
|
34
|
+
values = facet.rows.map do |row|
|
35
|
+
{ name: row.value, count: row.count }
|
36
|
+
end
|
37
|
+
facets << { name: facet.name.to_s, values: values }
|
38
|
+
end
|
39
|
+
facets
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_json(options = {})
|
43
|
+
rendered_json = as_json(options).to_json
|
44
|
+
rendered_json = "#{object.jsonp}(#{rendered_json})" if object.jsonp
|
45
|
+
rendered_json
|
46
|
+
end
|
47
|
+
|
48
|
+
def as_json(_options = {})
|
49
|
+
hash = { search: serializable_hash }
|
50
|
+
hash[:search][:facets] = json_facets
|
51
|
+
hash
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_xml(*args)
|
55
|
+
hash = serializable_hash
|
56
|
+
hash[:facets] = xml_facets
|
57
|
+
|
58
|
+
options = {}
|
59
|
+
options = args.first.merge(root: :search) if args.first.is_a?(Hash)
|
60
|
+
|
61
|
+
hash.to_xml(options)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|