supplejack_client 1.0.5 → 1.0.7

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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -2
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +2 -2
  5. data/Gemfile +4 -2
  6. data/README.md +3 -3
  7. data/lib/generators/locales/en.yml +5 -3
  8. data/lib/generators/supplejack/install_generator.rb +5 -3
  9. data/lib/generators/templates/supplejack_client.rb +5 -3
  10. data/lib/supplejack/concept.rb +5 -3
  11. data/lib/supplejack/config.rb +5 -3
  12. data/lib/supplejack/controllers/helpers.rb +5 -3
  13. data/lib/supplejack/engine.rb +5 -3
  14. data/lib/supplejack/exceptions.rb +8 -3
  15. data/lib/supplejack/facet.rb +5 -3
  16. data/lib/supplejack/item.rb +7 -5
  17. data/lib/supplejack/item_relation.rb +7 -4
  18. data/lib/supplejack/log_subscriber.rb +5 -3
  19. data/lib/supplejack/paginated_collection.rb +5 -3
  20. data/lib/supplejack/record.rb +5 -3
  21. data/lib/supplejack/request.rb +18 -4
  22. data/lib/supplejack/search.rb +5 -3
  23. data/lib/supplejack/story.rb +232 -0
  24. data/lib/supplejack/story_item.rb +149 -0
  25. data/lib/supplejack/story_item_relation.rb +81 -0
  26. data/lib/supplejack/url_formats/item_hash.rb +5 -3
  27. data/lib/supplejack/user.rb +9 -3
  28. data/lib/supplejack/user_set.rb +28 -26
  29. data/lib/supplejack/user_set_relation.rb +5 -3
  30. data/lib/supplejack/user_story_relation.rb +141 -0
  31. data/lib/supplejack/util.rb +5 -3
  32. data/lib/supplejack/version.rb +7 -5
  33. data/lib/supplejack_client.rb +14 -3
  34. data/spec/spec_helper.rb +7 -5
  35. data/spec/supplejack/concept_spec.rb +5 -3
  36. data/spec/supplejack/facet_spec.rb +5 -3
  37. data/spec/supplejack/item_relation_spec.rb +5 -3
  38. data/spec/supplejack/item_spec.rb +7 -5
  39. data/spec/supplejack/log_subscriber_spec.rb +5 -3
  40. data/spec/supplejack/paginated_collection_spec.rb +5 -3
  41. data/spec/supplejack/record_spec.rb +5 -3
  42. data/spec/supplejack/request_spec.rb +5 -3
  43. data/spec/supplejack/search_spec.rb +5 -3
  44. data/spec/supplejack/story_item_relation_spec.rb +149 -0
  45. data/spec/supplejack/story_item_spec.rb +114 -0
  46. data/spec/supplejack/story_spec.rb +401 -0
  47. data/spec/supplejack/user_set_relation_spec.rb +5 -3
  48. data/spec/supplejack/user_set_spec.rb +9 -7
  49. data/spec/supplejack/user_spec.rb +5 -3
  50. data/spec/supplejack/user_story_relation_spec.rb +157 -0
  51. data/stories_integration_test.rb +97 -0
  52. data/supplejack_client.gemspec +9 -3
  53. metadata +32 -10
@@ -0,0 +1,141 @@
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 UserStoryRelation
12
+ include Supplejack::Request
13
+
14
+ attr_reader :user
15
+
16
+ def initialize(user)
17
+ @user = user
18
+ @stories = []
19
+ # This is used to determine if we have requested the Stories from the API
20
+ # Previously @stories defaulted to nil so the ||= operator was used to determine
21
+ # whether to fetch. This was changed to allow built/created stories to be
22
+ # automatically included in the relation
23
+ @initial_fetch = false
24
+ end
25
+
26
+ # Returns an array of Story objects and memoizes the array
27
+ #
28
+ def fetch(force: false)
29
+ fetch_stories = -> { fetch_api_stories.map{|s| Supplejack::Story.new(s.merge(user: user.attributes))} }
30
+
31
+ @stories = if force
32
+ fetch_stories.call
33
+ elsif !@initial_fetch
34
+ @initial_fetch = true
35
+ fetch_stories.call
36
+ else
37
+ @stories
38
+ end
39
+
40
+ @stories
41
+ end
42
+ alias_method :all, :fetch
43
+
44
+ # Finds a Story object with the provided ID that belongs to the current
45
+ # User
46
+ #
47
+ # @param [ String ] A 24 character string representing the Story ID
48
+ #
49
+ # @return [ Supplejack::Story ] A Story object
50
+ #
51
+ def find(story_id)
52
+ path = "/stories/#{story_id}"
53
+ build get(path, api_key: API_KEY)
54
+ end
55
+
56
+ # Initializes a new Story object and links it to the current User
57
+ #
58
+ # @param [ Hash ] A hash with the Story field names and values
59
+ #
60
+ # @return [ Supplejack::Story] A new Story object
61
+ #
62
+ def build(attributes={})
63
+ story = Supplejack::Story.new(attributes)
64
+ story.api_key = user.api_key
65
+
66
+ # Possible at some point in the future we'll want
67
+ # to change this to something more robust.
68
+ #
69
+ # Just not sure if unsaved Stories being in this
70
+ # will ever be a problem or not
71
+ @stories << story
72
+
73
+ story
74
+ end
75
+
76
+ # Build and persist a Story object with the provided attributes
77
+ #
78
+ # @param [ Hash ] A hash with the Story field names and values
79
+ #
80
+ # @return [ Supplejack::Story ] A persisted Story object
81
+ #
82
+ def create(attributes={})
83
+ story = build(attributes)
84
+ story.save
85
+
86
+ story
87
+ end
88
+
89
+ # Return an array of Story objects ordered in ascending order
90
+ # by the specified attribute.
91
+ #
92
+ # @param [ Symbol ] A symbol with the attribute to order the Story objects on
93
+ #
94
+ # @return [ Array ] A array of Supplejack::Story objects.
95
+ #
96
+ def order(attribute)
97
+ @stories = all.sort_by do |story|
98
+ value = story.send(attribute)
99
+ value = value.downcase if value.is_a?(String)
100
+
101
+ value
102
+ end
103
+ end
104
+
105
+ def to_json(include_contents: true)
106
+ all.map{|story| story.as_json(include_contents: include_contents)}.to_json
107
+ end
108
+
109
+ # Any method missing on this class is delegated to the Stories objects array
110
+ # so that the developer can easily execute any Array method on the UserSttoryRelation
111
+ #
112
+ # @example
113
+ # user.stories.each .... => Iterate through the Story objects array
114
+ # user.stories.size => Get the size of the Story objects array
115
+ #
116
+ def method_missing(method, *args, &block)
117
+ all.send(method, *args, &block)
118
+ end
119
+
120
+ private
121
+
122
+ def fetch_api_stories
123
+ params = {}
124
+
125
+ if user.use_own_api_key?
126
+ path = "/stories"
127
+ params[:api_key] = user.api_key
128
+ else
129
+ path = "/users/#{user.api_key}/stories"
130
+ end
131
+
132
+ if Supplejack.enable_caching
133
+ Rails.cache.fetch(path, expires_in: 1.day) do
134
+ get(path, params)
135
+ end
136
+ else
137
+ get(path, params)
138
+ end
139
+ end
140
+ end
141
+ end
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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 Util
@@ -1,10 +1,12 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
- VERSION = '1.0.5'
10
- end
11
+ VERSION = '1.0.7'
12
+ end
@@ -1,9 +1,16 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
+ begin
11
+ require 'pry'
12
+ rescue LoadError
13
+ end
7
14
 
8
15
  require 'supplejack/config'
9
16
  require 'supplejack/version'
@@ -23,8 +30,12 @@ module Supplejack
23
30
  require 'supplejack/util'
24
31
  require 'supplejack/facet'
25
32
  require 'supplejack/user_set'
33
+ require 'supplejack/story'
34
+ require 'supplejack/story_item'
35
+ require 'supplejack/story_item_relation'
26
36
  require 'supplejack/item_relation'
27
37
  require 'supplejack/item'
28
38
  require 'supplejack/user'
29
39
  require 'supplejack/user_set_relation'
40
+ require 'supplejack/user_story_relation'
30
41
  end
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'rubygems'
9
11
  require 'bundler/setup'
@@ -14,8 +16,8 @@ Bundler.require(:default)
14
16
  require 'supplejack_client'
15
17
  require 'active_support/all'
16
18
 
17
- require "codeclimate-test-reporter"
18
- CodeClimate::TestReporter.start
19
+ require 'simplecov'
20
+ SimpleCov.start
19
21
 
20
22
  #SimpleCov.start
21
23
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -65,7 +67,7 @@ module Supplejack
65
67
 
66
68
  it 'stores the error when a error is raised' do
67
69
  item.save
68
- item.errors.should eq 'Forbidden: '
70
+ item.errors.should eq 'Forbidden'
69
71
  end
70
72
  end
71
73
  end
@@ -89,7 +91,7 @@ module Supplejack
89
91
 
90
92
  it 'stores the error when a error is raised' do
91
93
  item.destroy
92
- item.errors.should eq 'Forbidden: '
94
+ item.errors.should eq 'Forbidden'
93
95
  end
94
96
  end
95
97
  end
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -1,9 +1,11 @@
1
- # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
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 National Library of NZ
6
- # and the Department of Internal Affairs. http://digitalnz.org/supplejack
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
  require 'spec_helper'
9
11
 
@@ -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
+ require 'spec_helper'
11
+
12
+ module Supplejack
13
+ describe ItemRelation do
14
+ let(:supplejack_story) do
15
+ Supplejack::Story.new(
16
+ id: '1234567890',
17
+ user: {api_key: 'foobar'},
18
+ name: 'test',
19
+ contents: [
20
+ {id: 1, type: 'embed', sub_type: 'dnz', position: 1},
21
+ {id: 2, type: 'embed', sub_type: 'dnz', position: 2}
22
+ ]
23
+ )
24
+ end
25
+ let(:relation) { Supplejack::StoryItemRelation.new(supplejack_story) }
26
+
27
+ describe '#initialize' do
28
+ it 'assigns the story object as @story' do
29
+ expect(relation.story).to eq(supplejack_story)
30
+ end
31
+
32
+ it 'initializes an array of Supplejack::Items' do
33
+ expect(relation.all).to be_an Array
34
+ end
35
+
36
+ it 'returns an empty array of items when the user_set attributes records are nil' do
37
+ expect(supplejack_story).to receive(:attributes).and_return(nil)
38
+
39
+ expect(relation.all).to be_empty
40
+ end
41
+
42
+ it 'adds the story_id to the Supplejack::StoryItem object' do
43
+ expect(relation.all.first.story_id).to eq('1234567890')
44
+ end
45
+
46
+ it 'adds the api_key in the Story to the Supplejack::StoryItem' do
47
+ expect(relation.all.first.api_key).to eq('foobar')
48
+ end
49
+ end
50
+
51
+ describe '#all' do
52
+ it 'returns the array of @items' do
53
+ expect(supplejack_story.items.all).to be_an Array
54
+ end
55
+ end
56
+
57
+ describe '#find' do
58
+ it 'returns finds the item by record_id' do
59
+ item = relation.find(1)
60
+ expect(item).to be_a Supplejack::StoryItem
61
+ expect(item.id).to eq(1)
62
+ end
63
+
64
+ it 'finds the item by a string record_id' do
65
+ item = relation.find('1')
66
+
67
+ expect(item).to be_a Supplejack::StoryItem
68
+ expect(item.id).to eq(1)
69
+ end
70
+ end
71
+
72
+ describe '#build' do
73
+ it 'initializes a new item object with the user_set_id' do
74
+ item = relation.build
75
+
76
+ expect(item.story_id).to eq('1234567890')
77
+ end
78
+
79
+ it 'accepts a hash of attributes' do
80
+ item = relation.build(type: 'embed', sub_type: 'dnz')
81
+
82
+ expect(item.type).to eq('embed')
83
+ expect(item.sub_type).to eq('dnz')
84
+ end
85
+
86
+ it 'adds the Story api_key' do
87
+ item = relation.build
88
+
89
+ expect(item.api_key).to eq('foobar')
90
+ end
91
+ end
92
+
93
+ describe '#create' do
94
+ let(:item) { double(:item).as_null_object }
95
+
96
+ it 'builds and saves the item' do
97
+ expect(relation).to receive(:build) { item }
98
+ expect(item).to receive(:save)
99
+
100
+ relation.create
101
+ end
102
+
103
+ it 'passes the parameters along to the build method' do
104
+ expect(relation).to receive(:build).with(type: 'embed') { item }
105
+
106
+ relation.create(type: 'embed')
107
+ end
108
+ end
109
+
110
+ describe '#move_item' do
111
+ let(:item) { relation.all.first }
112
+
113
+ before do
114
+ expect(relation).to receive(:post).with(
115
+ "/stories/#{supplejack_story.id}/items/#{item.id}/moves",
116
+ {api_key: 'foobar'},
117
+ {item_id: 1, position: 2}
118
+ ).and_return([
119
+ {id: 2, type: 'embed', sub_type: 'dnz', position: 1},
120
+ {id: 1, type: 'embed', sub_type: 'dnz', position: 2}
121
+ ])
122
+ end
123
+
124
+ it 'calls the api move item endpoint with the new position' do
125
+ relation.move_item(item.id, 2)
126
+ end
127
+
128
+ it 'updates the items with the response' do
129
+ relation.move_item(item.id, 2)
130
+
131
+ expect(relation.all.first.id).to eq(2)
132
+ end
133
+ end
134
+
135
+ context 'items array behaviour' do
136
+ it 'executes array methods on the @items array' do
137
+ expect(relation.any?{|x| x.id == 1}).to eq(true)
138
+ end
139
+
140
+ it 'should be able to iterate through the items relation' do
141
+ relation.each do |item|
142
+ expect(item).to be_a Supplejack::StoryItem
143
+ end
144
+
145
+ expect(relation.size).to eq(2)
146
+ end
147
+ end
148
+ end
149
+ end