supplejack_client 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +16 -0
  5. data/Guardfile +24 -0
  6. data/LICENSE.txt +674 -0
  7. data/README.md +153 -0
  8. data/Rakefile +9 -0
  9. data/lib/generators/locales/en.yml +11 -0
  10. data/lib/generators/supplejack/install_generator.rb +28 -0
  11. data/lib/generators/templates/README +19 -0
  12. data/lib/generators/templates/supplejack_client.rb +120 -0
  13. data/lib/supplejack/config.rb +116 -0
  14. data/lib/supplejack/controllers/helpers.rb +172 -0
  15. data/lib/supplejack/engine.rb +20 -0
  16. data/lib/supplejack/exceptions.rb +17 -0
  17. data/lib/supplejack/facet.rb +33 -0
  18. data/lib/supplejack/item.rb +94 -0
  19. data/lib/supplejack/item_relation.rb +73 -0
  20. data/lib/supplejack/log_subscriber.rb +58 -0
  21. data/lib/supplejack/paginated_collection.rb +61 -0
  22. data/lib/supplejack/record.rb +147 -0
  23. data/lib/supplejack/request.rb +95 -0
  24. data/lib/supplejack/search.rb +346 -0
  25. data/lib/supplejack/url_formats/item_hash.rb +208 -0
  26. data/lib/supplejack/user.rb +132 -0
  27. data/lib/supplejack/user_set.rb +349 -0
  28. data/lib/supplejack/user_set_relation.rb +143 -0
  29. data/lib/supplejack/util.rb +120 -0
  30. data/lib/supplejack/version.rb +10 -0
  31. data/lib/supplejack_client.rb +29 -0
  32. data/spec/spec_helper.rb +23 -0
  33. data/spec/supplejack/controllers/helpers_spec.rb +277 -0
  34. data/spec/supplejack/facet_spec.rb +44 -0
  35. data/spec/supplejack/item_relation_spec.rb +111 -0
  36. data/spec/supplejack/item_spec.rb +115 -0
  37. data/spec/supplejack/log_subscriber_spec.rb +40 -0
  38. data/spec/supplejack/paginated_collection_spec.rb +43 -0
  39. data/spec/supplejack/record_spec.rb +255 -0
  40. data/spec/supplejack/request_spec.rb +195 -0
  41. data/spec/supplejack/search_spec.rb +727 -0
  42. data/spec/supplejack/url_formats/item_hash_spec.rb +341 -0
  43. data/spec/supplejack/user_set_relation_spec.rb +149 -0
  44. data/spec/supplejack/user_set_spec.rb +465 -0
  45. data/spec/supplejack/user_spec.rb +159 -0
  46. data/supplejack_client.gemspec +30 -0
  47. metadata +159 -0
@@ -0,0 +1,153 @@
1
+ # Supplejack Client
2
+
3
+ The Supplejack Client is a library to abstract the interaction with the Supplejack API. It connects to the Supplejack API, and allows you to treat models as if they were in a local database.
4
+
5
+ For more information on how to configure and use this application refer to the [documentation](http://digitalnz.github.io/supplejack).
6
+
7
+ ## Installation
8
+
9
+ Add it to your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'supplejack_client', github: 'git@github.com:DigitalNZ/supplejack_client.git'
13
+ ```
14
+
15
+ Run bundle install:
16
+
17
+ ```ruby
18
+ bundle install
19
+ ```
20
+
21
+ Run the installation generator:
22
+
23
+ ```ruby
24
+ rails g supplejack:install
25
+ ```
26
+
27
+ An initializer was created at `config/initializers/supplejack_client.rb`
28
+
29
+ You should set the variables needed by the Supplejack API:
30
+ - Api Key
31
+ - Api URL
32
+
33
+ To start using Supplejack gem you have add the following line to any plain ruby class
34
+
35
+ ```ruby
36
+ class Item
37
+ include Supplejack::Record
38
+ end
39
+ ```
40
+
41
+ Then do `Search.new(params)` or `Item.find(id)`
42
+
43
+ ## Configuration
44
+
45
+ Modify the initializer at `config/initializers/supplejack_client.rb`.
46
+
47
+ ```ruby
48
+ Supplejack.configure do |config|
49
+
50
+ # ===> Credentials
51
+ # Use the api_key for your Supplejack user
52
+ # Please replace XXXX with your own api key
53
+ config.api_key = "XXXX"
54
+ #
55
+ # ===> End point
56
+ # For production use default url which is http://api.youapihost.org
57
+ config.api_url = "http://youapihost.org"
58
+ #
59
+ # ===> URL Format
60
+ # This is the format use for the url's in the application
61
+ # The default is the item hash which looks like: "text='dog'&i[content_partner]=NLNZ&i[category]=Images"
62
+ config.url_format = :item_hash
63
+ #
64
+ # ===> Facets
65
+ # The is the list of facets that are going to be requested to the api
66
+ # When you ask for the facets, they are going to be ordered in the
67
+ # order presented here
68
+ config.facets = [
69
+ :name,
70
+ :description,
71
+ :age
72
+ ]
73
+ #
74
+ # ===> Facet values sorting
75
+ # By default facet values are sorted by whatever solr returns.
76
+ # The sorting options are :index and :count
77
+ # :index means lexical sorting (Alphabetical)
78
+ # :count means is ordered by the number of results for each facet value
79
+ config.facets_sort = nil
80
+ #
81
+ # ===> Fields
82
+ # This is a list of fields/groups that will be requested to the API for every
83
+ # record. :default will return the default set of fields.
84
+ #
85
+ config.fields = [
86
+ :default
87
+ ]
88
+
89
+ # ===> Number of facet values
90
+ # This will limit the number of facet values returned for each facet
91
+ # Be carefull not to make it too high for performance reasons
92
+ config.facets_per_page = 10
93
+ #
94
+ # ===> Per Page
95
+ # Number of results returned per page
96
+ config.per_page = 20
97
+ #
98
+ # ===> Timeout
99
+ # By default the request to the API will timeout after 30 seconds
100
+ config.timeout = 30
101
+ #
102
+ # ===> Single value methods
103
+ # Some of the values returned by the API are actually multiple values
104
+ # so they are returned as a array. But most of the time we are only intereseted
105
+ # in one of those values. Here you can define which values would you like to
106
+ # be converted to a string.
107
+ config.single_value_methods = [
108
+ :email
109
+ ]
110
+ #
111
+ # ===> Search attributes
112
+ # The search object can store any number of attributes which are actually
113
+ # the filters passed to the search
114
+ # Here you can define which attributes you want the search to accept.
115
+ # This is going to allow you to do:
116
+ # search = Search.new(text: 'dog', i: {category: 'Images'})
117
+ # search.category
118
+ #
119
+ config.search_attributes = [
120
+ :category
121
+ ]
122
+ #
123
+ # ===> Record klass
124
+ # Name of the main model throught which you interact with the Supplejack API
125
+ # This is used to initialize objects of this class when the list of
126
+ # favourites is fetched.
127
+ #
128
+ config.record_klass = "Record"
129
+ #
130
+ # ===> Enable Debugging
131
+ # Set this flag to true in order to get display errors and the actual SOLR requests
132
+ # in the logs generated by the API.
133
+ #
134
+ config.enable_debugging = true
135
+ #
136
+ # ===> Enable Caching
137
+ # Set this flag to true in order to cache the facet_value response and the search counts
138
+ #
139
+ config.enable_caching = true
140
+ end
141
+ ```
142
+
143
+ ## COPYRIGHT AND LICENSING


144
+
145
+ ### SUPPLEJACK CODE - GNU GENERAL PUBLIC LICENCE, VERSION 3


146
+
147
+ Supplejack is a tool for aggregating, searching and sharing metadata records. Supplejack Client is a component of Supplejack. The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government. Supplejack was created by DigitalNZ at the National Library of NZ and the Department of Internal Affairs. http://digitalnz.org/supplejack
148
+
149
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
150
+
151
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
152
+
153
+ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses / http://www.gnu.org/licenses/gpl-3.0.txt

@@ -0,0 +1,9 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ and
6
+ # the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ #!/usr/bin/env rake
9
+ require 'bundler/gem_tasks'
@@ -0,0 +1,11 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ en:
9
+ supplejack_client:
10
+ next: Next
11
+ previous: Previous
@@ -0,0 +1,28 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ module Supplejack
9
+ module Generators
10
+ class InstallGenerator < Rails::Generators::Base
11
+ source_root File.expand_path("../../templates", __FILE__)
12
+
13
+ desc "Creates a Supplejack Client initializer."
14
+
15
+ def copy_initializer
16
+ template "supplejack_client.rb", "config/initializers/supplejack_client.rb"
17
+ end
18
+
19
+ def copy_locale
20
+ copy_file "../locales/en.yml", "config/locales/supplejack_client.en.yml"
21
+ end
22
+
23
+ def show_readme
24
+ readme "README"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ ======================================
2
+
3
+ You are almost finished:
4
+
5
+ An initializer was created at config/initializers/supplejack_client.rb
6
+
7
+ You should set the variables needed by the Supplejack API:
8
+ - Api Key
9
+ - Api URL
10
+
11
+ To start using Supplejack gem you have add the following line to any plain ruby class
12
+
13
+ class Item
14
+ include Supplejack::Record
15
+ end
16
+
17
+ Then do Search.new(params) or Item.find(id)
18
+
19
+ ======================================
@@ -0,0 +1,120 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ Supplejack.configure do |config|
9
+
10
+ # ===> Credentials
11
+ # Use the api_key for your Supplejack user
12
+ # Please replace XXXX with your own api key
13
+ config.api_key = "XXXX"
14
+ #
15
+ # ===> End point
16
+ # For production use default url which is http://api.youapihost.org
17
+ # config.api_url = "http://youapihost.org"
18
+ #
19
+ # ===> URL Format
20
+ # This is the format use for the url's in the application
21
+ # The default is the item hash which looks like: "text='dog'&i[content_partner]=NLNZ&i[category]=Images"
22
+ # config.url_format = :item_hash
23
+ #
24
+ # ===> Custom Search
25
+ # In case you want to filter the results through a custom search
26
+ # specify the slug
27
+ # config.custom_search = nil
28
+ #
29
+ # ===> Facets
30
+ # The is the list of facets that are going to be requested to the api
31
+ # When you ask for the facets, they are going to be ordered in the
32
+ # order presented here
33
+ config.facets = [
34
+ :name,
35
+ :description,
36
+ :age
37
+ ]
38
+ #
39
+ # ===> Facet values sorting
40
+ # By default facet values are sorted by whatever solr returns.
41
+ # The sorting options are :index and :count
42
+ # :index means lexical sorting (Alphabetical)
43
+ # :count means is ordered by the number of results for each facet value
44
+ # config.facets_sort = nil
45
+ #
46
+ # ===> Fields
47
+ # This is a list of fields/groups that will be requested to the API for every
48
+ # record. :default will return the default set of fields.
49
+ #
50
+ config.fields = [
51
+ :default
52
+ ]
53
+
54
+ # ===> Number of facet values
55
+ # This will limit the number of facet values returned for each facet
56
+ # Be carefull not to make it too high for performance reasons
57
+ # config.facets_per_page = 10
58
+ #
59
+ # ===> Per Page
60
+ # Number of results returned per page
61
+ # config.per_page = 20
62
+ #
63
+ # ===> Timeout
64
+ # By default the request to the API will timeout after 30 seconds
65
+ # config.timeout = 30
66
+ #
67
+ # ===> Single value methods
68
+ # Some of the values returned by the API are actually multiple values
69
+ # so they are returned as a array. But most of the time we are only intereseted
70
+ # in one of those values. Here you can define which values would you like to
71
+ # be converted to a string.
72
+ # config.single_value_methods = [
73
+ # :email
74
+ # ]
75
+ #
76
+ # ===> Search attributes
77
+ # The search object can store any number of attributes which are actually
78
+ # the filters passed to the search
79
+ # Here you can define which attributes you want the search to accept.
80
+ # This is going to allow you to do:
81
+ # search = Search.new(text: 'dog', i: {category: 'Images'})
82
+ # search.category
83
+ #
84
+ # config.search_attributes = [
85
+ # :category
86
+ # ]
87
+ #
88
+ # ===> Record klass
89
+ # Name of the main model throught which you interact with the Supplejack API
90
+ # This is used to initialize objects of this class when the list of
91
+ # favourites is fetched.
92
+ #
93
+ # config.record_klass = "Record"
94
+ #
95
+ # ===> Enable Debugging
96
+ # Set this flag to true in order to get display errors and the actual SOLR requests
97
+ # in the logs generated by the API.
98
+ #
99
+ # config.enable_debugging = true
100
+ #
101
+ # ===> Enable Caching
102
+ # Set this flag to true in order to cache the facet_value response and the search counts
103
+ #
104
+ # config.enable_caching = true
105
+ #
106
+ # ===> Attribute Tag
107
+ # HTML tag used by default to display the record attributes
108
+ #
109
+ # config.attribute_tag = :p
110
+ #
111
+ # ===> Label Tag
112
+ # HTML tag used by default to display the record attributes label
113
+ #
114
+ # config.label_tag = :span
115
+ #
116
+ # ===> Label class
117
+ # The CSS class used by the attribute label
118
+ #
119
+ # config.label_class = "label"
120
+ end
@@ -0,0 +1,116 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ module Supplejack
9
+ module Config
10
+
11
+ # Default values for the supplejack configuration.
12
+ #
13
+ # These values can be overwritten in a rails initializer for example:
14
+ #
15
+ # /config/initializers/supplejack.rb
16
+ #
17
+ # Supplejack.configure do |config|
18
+ # config.api_key = "xxxx"
19
+ # config.api_url = "api.supplejack.org"
20
+ # etc....
21
+ # end
22
+
23
+ API_KEY = nil
24
+ API_URL = 'http://api.digitalnz.org'
25
+ URL_FORMAT = :item_hash
26
+ FACETS = []
27
+ FACETS_PER_PAGE = 10
28
+ FACETS_SORT = nil
29
+ PER_PAGE = 20
30
+ PAGINATION_LIMIT = nil
31
+ TIMEOUT = 30
32
+ RECORD_KLASS = 'Record'
33
+ SEARCH_KLASS = nil
34
+ FIELDS = [:default]
35
+ SUPPLEJACK_FIELDS = []
36
+ ADMIN_FIELDS = []
37
+ ENABLE_DEBUGGING = false
38
+ ENABLE_CACHING = false
39
+ ATTRIBUTE_TAG = :p
40
+ LABEL_TAG = :strong
41
+ LABEL_CLASS = nil
42
+
43
+ VALID_OPTIONS_KEYS = [
44
+ :api_key,
45
+ :api_url,
46
+ :facets,
47
+ :facets_per_page,
48
+ :facets_sort,
49
+ :single_value_methods,
50
+ :search_attributes,
51
+ :url_format,
52
+ :per_page,
53
+ :pagination_limit,
54
+ :timeout,
55
+ :record_klass,
56
+ :search_klass,
57
+ :fields,
58
+ :supplejack_fields,
59
+ :admin_fields,
60
+ :enable_debugging,
61
+ :enable_caching,
62
+ :attribute_tag,
63
+ :label_tag,
64
+ :label_class
65
+ ]
66
+
67
+ SINGLE_VALUE_METHODS = [
68
+ :description
69
+ ]
70
+
71
+ SEARCH_ATTRIBUTES = [
72
+ :location
73
+ ]
74
+
75
+ attr_accessor *VALID_OPTIONS_KEYS
76
+
77
+ # When this module is extended, set all configuration options to their default values
78
+ def self.extended(base)
79
+ base.reset
80
+ end
81
+
82
+ def configure
83
+ yield self
84
+ end
85
+
86
+ def url_format_klass
87
+ "Supplejack::UrlFormats::#{Supplejack.url_format.to_s.classify}".constantize
88
+ end
89
+
90
+ # Reset all configuration options to defaults
91
+ def reset
92
+ self.api_key = API_KEY
93
+ self.api_url = API_URL
94
+ self.facets = FACETS
95
+ self.facets_per_page = FACETS_PER_PAGE
96
+ self.facets_sort = FACETS_SORT
97
+ self.single_value_methods = SINGLE_VALUE_METHODS
98
+ self.search_attributes = SEARCH_ATTRIBUTES
99
+ self.url_format = URL_FORMAT
100
+ self.per_page = PER_PAGE
101
+ self.pagination_limit = PAGINATION_LIMIT
102
+ self.timeout = TIMEOUT
103
+ self.record_klass = RECORD_KLASS
104
+ self.search_klass = SEARCH_KLASS
105
+ self.fields = FIELDS
106
+ self.supplejack_fields = SUPPLEJACK_FIELDS
107
+ self.admin_fields = ADMIN_FIELDS
108
+ self.enable_debugging = ENABLE_DEBUGGING
109
+ self.enable_caching = ENABLE_CACHING
110
+ self.attribute_tag = ATTRIBUTE_TAG
111
+ self.label_tag = LABEL_TAG
112
+ self.label_class = LABEL_CLASS
113
+ self
114
+ end
115
+ end
116
+ end