supplejack_client 1.0.5 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -2
- data/.ruby-version +1 -0
- data/.travis.yml +2 -2
- data/Gemfile +4 -2
- data/README.md +3 -3
- data/lib/generators/locales/en.yml +5 -3
- data/lib/generators/supplejack/install_generator.rb +5 -3
- data/lib/generators/templates/supplejack_client.rb +5 -3
- data/lib/supplejack/concept.rb +5 -3
- data/lib/supplejack/config.rb +5 -3
- data/lib/supplejack/controllers/helpers.rb +5 -3
- data/lib/supplejack/engine.rb +5 -3
- data/lib/supplejack/exceptions.rb +8 -3
- data/lib/supplejack/facet.rb +5 -3
- data/lib/supplejack/item.rb +7 -5
- data/lib/supplejack/item_relation.rb +7 -4
- data/lib/supplejack/log_subscriber.rb +5 -3
- data/lib/supplejack/paginated_collection.rb +5 -3
- data/lib/supplejack/record.rb +5 -3
- data/lib/supplejack/request.rb +18 -4
- data/lib/supplejack/search.rb +5 -3
- data/lib/supplejack/story.rb +232 -0
- data/lib/supplejack/story_item.rb +149 -0
- data/lib/supplejack/story_item_relation.rb +81 -0
- data/lib/supplejack/url_formats/item_hash.rb +5 -3
- data/lib/supplejack/user.rb +9 -3
- data/lib/supplejack/user_set.rb +28 -26
- data/lib/supplejack/user_set_relation.rb +5 -3
- data/lib/supplejack/user_story_relation.rb +141 -0
- data/lib/supplejack/util.rb +5 -3
- data/lib/supplejack/version.rb +7 -5
- data/lib/supplejack_client.rb +14 -3
- data/spec/spec_helper.rb +7 -5
- data/spec/supplejack/concept_spec.rb +5 -3
- data/spec/supplejack/facet_spec.rb +5 -3
- data/spec/supplejack/item_relation_spec.rb +5 -3
- data/spec/supplejack/item_spec.rb +7 -5
- data/spec/supplejack/log_subscriber_spec.rb +5 -3
- data/spec/supplejack/paginated_collection_spec.rb +5 -3
- data/spec/supplejack/record_spec.rb +5 -3
- data/spec/supplejack/request_spec.rb +5 -3
- data/spec/supplejack/search_spec.rb +5 -3
- data/spec/supplejack/story_item_relation_spec.rb +149 -0
- data/spec/supplejack/story_item_spec.rb +114 -0
- data/spec/supplejack/story_spec.rb +401 -0
- data/spec/supplejack/user_set_relation_spec.rb +5 -3
- data/spec/supplejack/user_set_spec.rb +9 -7
- data/spec/supplejack/user_spec.rb +5 -3
- data/spec/supplejack/user_story_relation_spec.rb +157 -0
- data/stories_integration_test.rb +97 -0
- data/supplejack_client.gemspec +9 -3
- metadata +32 -10
@@ -0,0 +1,149 @@
|
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# See https://github.com/DigitalNZ/supplejack_client for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
9
|
+
|
10
|
+
module Supplejack
|
11
|
+
|
12
|
+
# The +StoryItem+ class represents a StoryItem on the Supplejack API
|
13
|
+
#
|
14
|
+
# A StoryItem always belongs to a Story. represents.
|
15
|
+
#
|
16
|
+
# A StoryItem object has the following values:
|
17
|
+
# - id
|
18
|
+
# - type
|
19
|
+
# - sub_type
|
20
|
+
# - position
|
21
|
+
# - meta [Hash]
|
22
|
+
# - content [Hash]
|
23
|
+
class StoryItem
|
24
|
+
include Supplejack::Request
|
25
|
+
|
26
|
+
MODIFIABLE_ATTRIBUTES = [:position, :meta, :content, :type, :sub_type].freeze
|
27
|
+
UNMODIFIABLE_ATTRIBUTES = [:id, :story_id].freeze
|
28
|
+
ATTRIBUTES = (MODIFIABLE_ATTRIBUTES + UNMODIFIABLE_ATTRIBUTES).freeze
|
29
|
+
|
30
|
+
attr_accessor *ATTRIBUTES
|
31
|
+
attr_accessor :errors
|
32
|
+
attr_reader :api_key
|
33
|
+
|
34
|
+
def initialize(attributes={})
|
35
|
+
@attributes = attributes.try(:deep_symbolize_keys) || {}
|
36
|
+
@api_key = @attributes[:api_key]
|
37
|
+
|
38
|
+
self.meta ||= {}
|
39
|
+
self.attributes = @attributes
|
40
|
+
end
|
41
|
+
|
42
|
+
# Assigns the provided attributes to the StoryItem object
|
43
|
+
#
|
44
|
+
def attributes=(attributes)
|
45
|
+
attributes = attributes.try(:deep_symbolize_keys) || {}
|
46
|
+
|
47
|
+
attributes.each do |attr, value|
|
48
|
+
self.send("#{attr}=", value) if ATTRIBUTES.include?(attr)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def attributes
|
53
|
+
retrieve_attributes(ATTRIBUTES)
|
54
|
+
end
|
55
|
+
|
56
|
+
def api_attributes
|
57
|
+
retrieve_attributes(MODIFIABLE_ATTRIBUTES)
|
58
|
+
end
|
59
|
+
|
60
|
+
def new_record?
|
61
|
+
id.nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Executes a POST request when the StoryItem hasn't been persisted to
|
65
|
+
# the API, otherwise it execute a PATCH request.
|
66
|
+
#
|
67
|
+
# When the API returns a error response, the errors are available through the Story#errors
|
68
|
+
# virtual attribute.
|
69
|
+
#
|
70
|
+
# @return [ true, false ] True if the API response was successful, false if not.
|
71
|
+
#
|
72
|
+
def save
|
73
|
+
begin
|
74
|
+
if self.new_record?
|
75
|
+
self.attributes = post(
|
76
|
+
"/stories/#{story_id}/items",
|
77
|
+
{api_key: api_key},
|
78
|
+
{item: self.api_attributes}
|
79
|
+
)
|
80
|
+
else
|
81
|
+
self.attributes = patch(
|
82
|
+
"/stories/#{story_id}/items/#{id}",
|
83
|
+
params: {api_key: api_key},
|
84
|
+
payload: {item: self.api_attributes}
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
Rails.cache.delete("/users/#{self.api_key}/stories") if Supplejack.enable_caching
|
89
|
+
|
90
|
+
true
|
91
|
+
rescue StandardError => e
|
92
|
+
self.errors = e.message
|
93
|
+
|
94
|
+
false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Executes a DELETE request to the API with the StoryItem ID and the stories api_key
|
99
|
+
#
|
100
|
+
# When the API returns an error response, the errors are available through the StoryItem#errors
|
101
|
+
# virtual attribute.
|
102
|
+
#
|
103
|
+
# @return [ true, false ] True if the API response was successful, false if not.
|
104
|
+
#
|
105
|
+
def destroy
|
106
|
+
return false if self.new_record?
|
107
|
+
|
108
|
+
begin
|
109
|
+
delete("/stories/#{story_id}/items/#{id}", {api_key: api_key})
|
110
|
+
|
111
|
+
Rails.cache.delete("/users/#{api_key}/stories") if Supplejack.enable_caching
|
112
|
+
|
113
|
+
true
|
114
|
+
rescue StandardError => e
|
115
|
+
self.errors = e.message
|
116
|
+
|
117
|
+
false
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Updates the StoryItems attributes and persists it to the API
|
122
|
+
#
|
123
|
+
# @return [ true, false ] True if the API response was successful, false if not.
|
124
|
+
#
|
125
|
+
def update_attributes(attributes={})
|
126
|
+
self.attributes = attributes
|
127
|
+
|
128
|
+
self.save
|
129
|
+
end
|
130
|
+
|
131
|
+
def to_json
|
132
|
+
attributes.to_json
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def retrieve_attributes(attributes_list)
|
138
|
+
attributes = {}
|
139
|
+
|
140
|
+
attributes_list.each do |attribute|
|
141
|
+
value = self.send(attribute)
|
142
|
+
|
143
|
+
attributes[attribute] = value unless value.nil?
|
144
|
+
end
|
145
|
+
|
146
|
+
attributes
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
3
|
+
# and is licensed under the GNU General Public License, version 3.
|
4
|
+
# See https://github.com/DigitalNZ/supplejack_client for details.
|
5
|
+
#
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
9
|
+
|
10
|
+
module Supplejack
|
11
|
+
class StoryItemRelation
|
12
|
+
include Request
|
13
|
+
|
14
|
+
attr_reader :story, :errors
|
15
|
+
|
16
|
+
def initialize(story)
|
17
|
+
@story = story
|
18
|
+
items = story.attributes.try(:fetch, :contents, nil) || []
|
19
|
+
build_items(items)
|
20
|
+
end
|
21
|
+
|
22
|
+
def all
|
23
|
+
@items
|
24
|
+
end
|
25
|
+
|
26
|
+
def build(attributes = {})
|
27
|
+
story_item = Supplejack::StoryItem.new(attributes.merge(story_id: story.id, api_key: story.api_key))
|
28
|
+
|
29
|
+
@items << story_item
|
30
|
+
|
31
|
+
story_item
|
32
|
+
end
|
33
|
+
|
34
|
+
def create(attributes = {})
|
35
|
+
story_item = build(attributes)
|
36
|
+
|
37
|
+
story_item.save
|
38
|
+
end
|
39
|
+
|
40
|
+
def move_item(item_id, position)
|
41
|
+
begin
|
42
|
+
response = post("/stories/#{story.id}/items/#{item_id}/moves", {api_key: story.api_key}, {item_id: item_id, position: position})
|
43
|
+
|
44
|
+
build_items(response)
|
45
|
+
|
46
|
+
true
|
47
|
+
rescue StandardError => e
|
48
|
+
@errors = e.inspect
|
49
|
+
|
50
|
+
false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def find(id)
|
55
|
+
@items.detect{|i| i.id == id.to_i}
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_json
|
59
|
+
all.to_json
|
60
|
+
end
|
61
|
+
|
62
|
+
# Any method missing on this class is delegated to the StoryItems objects array
|
63
|
+
# so that the developer can easily execute any Array method on the StoryItemRelation
|
64
|
+
#
|
65
|
+
# @example
|
66
|
+
# story.each .... => Iterate through the StoryItem objects array
|
67
|
+
# story.size => Get the size of the StoryItem objects array
|
68
|
+
#
|
69
|
+
def method_missing(method, *args, &block)
|
70
|
+
all.send(method, *args, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def build_items(items)
|
76
|
+
@items = items.map do |hash|
|
77
|
+
Supplejack::StoryItem.new(hash.merge(story_id: story.id, api_key: story.api_key))
|
78
|
+
end.sort_by(&:position)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
|
-
# The Supplejack
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
2
3
|
# and is licensed under the GNU General Public License, version 3.
|
3
4
|
# See https://github.com/DigitalNZ/supplejack_client for details.
|
4
5
|
#
|
5
|
-
# Supplejack was created by DigitalNZ at the
|
6
|
-
# and the Department of Internal Affairs.
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
7
9
|
|
8
10
|
module Supplejack
|
9
11
|
module UrlFormats
|
data/lib/supplejack/user.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
# The Supplejack
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
2
3
|
# and is licensed under the GNU General Public License, version 3.
|
3
4
|
# See https://github.com/DigitalNZ/supplejack_client for details.
|
4
5
|
#
|
5
|
-
# Supplejack was created by DigitalNZ at the
|
6
|
-
# and the Department of Internal Affairs.
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
7
9
|
|
8
10
|
module Supplejack
|
9
11
|
|
@@ -44,6 +46,10 @@ module Supplejack
|
|
44
46
|
@sets ||= UserSetRelation.new(self)
|
45
47
|
end
|
46
48
|
|
49
|
+
def stories
|
50
|
+
@stories ||= UserStoryRelation.new(self)
|
51
|
+
end
|
52
|
+
|
47
53
|
# Initializes a UserSetRelation class which adds behaviour to build, create
|
48
54
|
# find and order sets related to this particular User instance
|
49
55
|
#
|
data/lib/supplejack/user_set.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
# The Supplejack
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
2
3
|
# and is licensed under the GNU General Public License, version 3.
|
3
4
|
# See https://github.com/DigitalNZ/supplejack_client for details.
|
4
5
|
#
|
5
|
-
# Supplejack was created by DigitalNZ at the
|
6
|
-
# and the Department of Internal Affairs.
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
7
9
|
|
8
10
|
module Supplejack
|
9
11
|
|
@@ -21,19 +23,19 @@ module Supplejack
|
|
21
23
|
# - priority
|
22
24
|
# - count
|
23
25
|
# - tags
|
24
|
-
#
|
26
|
+
#
|
25
27
|
class UserSet
|
26
28
|
extend Supplejack::Request
|
27
29
|
include ActiveModel::Conversion
|
28
30
|
extend ActiveModel::Naming
|
29
31
|
|
30
|
-
ATTRIBUTES = [:id, :name, :description, :privacy, :url, :priority, :count, :tags, :tag_list,
|
32
|
+
ATTRIBUTES = [:id, :name, :description, :privacy, :url, :priority, :count, :tags, :tag_list,
|
31
33
|
:featured, :records, :created_at, :updated_at, :approved, :record]
|
32
34
|
attr_accessor *ATTRIBUTES
|
33
35
|
attr_accessor :api_key, :errors, :user
|
34
36
|
|
35
37
|
PRIVACY_STATES = ['public', 'hidden', 'private']
|
36
|
-
|
38
|
+
|
37
39
|
def initialize(attributes={})
|
38
40
|
@attributes = attributes.try(:symbolize_keys) || {}
|
39
41
|
@user = Supplejack::User.new(@attributes[:user])
|
@@ -72,7 +74,7 @@ module Supplejack
|
|
72
74
|
#
|
73
75
|
def api_records
|
74
76
|
records = self.records.is_a?(Array) ? self.records : []
|
75
|
-
records.map do |record_hash|
|
77
|
+
records.map do |record_hash|
|
76
78
|
record_hash = record_hash.try(:symbolize_keys) || {}
|
77
79
|
if record_hash[:record_id]
|
78
80
|
{record_id: record_hash[:record_id], position: record_hash[:position] }
|
@@ -98,7 +100,7 @@ module Supplejack
|
|
98
100
|
end
|
99
101
|
|
100
102
|
# Returns the api_key from the UserSet or from the User which this set belongs to.
|
101
|
-
#
|
103
|
+
#
|
102
104
|
def api_key
|
103
105
|
@api_key || @user.try(:api_key)
|
104
106
|
end
|
@@ -146,11 +148,11 @@ module Supplejack
|
|
146
148
|
# Executes a POST request when the UserSet hasn't been persisted to the API, otherwise it executes
|
147
149
|
# a PUT request.
|
148
150
|
#
|
149
|
-
# When the API returns a error response, the errors are available through the UserSet#errors
|
151
|
+
# When the API returns a error response, the errors are available through the UserSet#errors
|
150
152
|
# virtual attribute.
|
151
153
|
#
|
152
154
|
# @return [ true, false ] True if the API response was successful, false if not.
|
153
|
-
#
|
155
|
+
#
|
154
156
|
def save
|
155
157
|
begin
|
156
158
|
if self.new_record?
|
@@ -162,7 +164,7 @@ module Supplejack
|
|
162
164
|
Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching
|
163
165
|
return true
|
164
166
|
rescue StandardError => e
|
165
|
-
self.errors = e.
|
167
|
+
self.errors = e.message
|
166
168
|
return false
|
167
169
|
end
|
168
170
|
end
|
@@ -177,7 +179,7 @@ module Supplejack
|
|
177
179
|
end
|
178
180
|
|
179
181
|
# Assigns the provided attributes to the UserSet object
|
180
|
-
#
|
182
|
+
#
|
181
183
|
def attributes=(attributes)
|
182
184
|
attributes = attributes.try(:symbolize_keys) || {}
|
183
185
|
attributes.each do |attr, value|
|
@@ -188,7 +190,7 @@ module Supplejack
|
|
188
190
|
|
189
191
|
# Define setter methods for both created_at and updated_at so that
|
190
192
|
# they always return a Time object.
|
191
|
-
#
|
193
|
+
#
|
192
194
|
[:created_at, :updated_at].each do |attribute|
|
193
195
|
define_method("#{attribute}=") do |time|
|
194
196
|
self.instance_variable_set("@#{attribute}", Util.time(time))
|
@@ -223,9 +225,9 @@ module Supplejack
|
|
223
225
|
end
|
224
226
|
|
225
227
|
# Executes a DELETE request to the API with the UserSet ID and the user's api_key
|
226
|
-
#
|
228
|
+
#
|
227
229
|
# @return [ true, false ] True if the API response was successful, false if not.
|
228
|
-
#
|
230
|
+
#
|
229
231
|
def destroy
|
230
232
|
if self.new_record?
|
231
233
|
return false
|
@@ -235,7 +237,7 @@ module Supplejack
|
|
235
237
|
Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching
|
236
238
|
return true
|
237
239
|
rescue StandardError => e
|
238
|
-
self.errors = e.
|
240
|
+
self.errors = e.message
|
239
241
|
return false
|
240
242
|
end
|
241
243
|
end
|
@@ -248,7 +250,7 @@ module Supplejack
|
|
248
250
|
# Fetches the UserSet information from the API again, in case it had changed.
|
249
251
|
# This can be useful if they items for a UserSet changed and you want the relation
|
250
252
|
# to have the most up to date items.
|
251
|
-
#
|
253
|
+
#
|
252
254
|
def reload
|
253
255
|
begin
|
254
256
|
self.instance_variable_set("@items", nil)
|
@@ -264,7 +266,7 @@ module Supplejack
|
|
264
266
|
# @param [ Supplejack::User ] A Supplejack::User object
|
265
267
|
#
|
266
268
|
# @return [ true, false ] True if the user can view the current UserSet, false if not.
|
267
|
-
#
|
269
|
+
#
|
268
270
|
def viewable_by?(user)
|
269
271
|
return true if self.public? || self.hidden?
|
270
272
|
self.owned_by?(user)
|
@@ -286,10 +288,10 @@ module Supplejack
|
|
286
288
|
# a UserSet object with the response from the API.
|
287
289
|
#
|
288
290
|
# @return [ UserSet ] A UserSet object
|
289
|
-
#
|
290
|
-
def self.find(id, api_key=nil)
|
291
|
+
#
|
292
|
+
def self.find(id, api_key=nil, params={})
|
291
293
|
begin
|
292
|
-
response = get("/sets/#{id}")
|
294
|
+
response = get("/sets/#{id}", params)
|
293
295
|
attributes = response["set"] || {}
|
294
296
|
user_set = new(attributes)
|
295
297
|
user_set.api_key = api_key if api_key.present?
|
@@ -308,15 +310,15 @@ module Supplejack
|
|
308
310
|
# @option options [ Integer ] :per_page The per_page number to select the number of UserSet objects per page.
|
309
311
|
#
|
310
312
|
# @return [ Array ] A array of Supplejack::UserSet objects
|
311
|
-
#
|
313
|
+
#
|
312
314
|
def self.public_sets(options={})
|
313
315
|
options.reverse_merge!(page: 1, per_page: 100)
|
314
316
|
response = get("/sets/public", options)
|
315
317
|
sets_array = response["sets"] || []
|
316
318
|
user_sets = sets_array.map {|attrs| new(attrs) }
|
317
|
-
Supplejack::PaginatedCollection.new(user_sets,
|
318
|
-
options[:page].to_i,
|
319
|
-
options[:per_page].to_i,
|
319
|
+
Supplejack::PaginatedCollection.new(user_sets,
|
320
|
+
options[:page].to_i,
|
321
|
+
options[:per_page].to_i,
|
320
322
|
response["total"].to_i)
|
321
323
|
end
|
322
324
|
|
@@ -345,7 +347,7 @@ module Supplejack
|
|
345
347
|
# This is useful when using a Supplejack::UserSet object in the Rails provided routes helpers. Example:
|
346
348
|
#
|
347
349
|
# user_set_path(@user_set)
|
348
|
-
#
|
350
|
+
#
|
349
351
|
def self.model_name
|
350
352
|
ActiveModel::Name.new(self, nil, "UserSet")
|
351
353
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
-
# The Supplejack
|
1
|
+
# The Supplejack Common code is
|
2
|
+
# Crown copyright (C) 2014, New Zealand Government,
|
2
3
|
# and is licensed under the GNU General Public License, version 3.
|
3
4
|
# See https://github.com/DigitalNZ/supplejack_client for details.
|
4
5
|
#
|
5
|
-
# Supplejack was created by DigitalNZ at the
|
6
|
-
# and the Department of Internal Affairs.
|
6
|
+
# Supplejack was created by DigitalNZ at the
|
7
|
+
# National Library of NZ and the Department of Internal Affairs.
|
8
|
+
# http://digitalnz.org/supplejack
|
7
9
|
|
8
10
|
module Supplejack
|
9
11
|
|