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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4dab73234e69432fb7307710644e9334d8390d5
4
- data.tar.gz: 8e7a12fc595c18b2effeaecc3c7f1dbabc39f886
3
+ metadata.gz: 607c91386fe82d2b35fc9e1afdaa111f931c5689
4
+ data.tar.gz: 9ed5ba9435d54de0192afd8760f2e60f30b57e54
5
5
  SHA512:
6
- metadata.gz: 07fdb8b8380510afd16e72e7833f031323e9986ebc6690a643e239b55c2106637d21eafa1f1a032ad32fa901485704eeda8b1685d65abb9007fcc77c76052a7a
7
- data.tar.gz: 5a36fb4d103c64dab9127e2942c1dcc3e04db2d58432d72a00f40e2cdc88d5b0aacd6a761be84a3224abc059660a3879f1cca3e9995d645652cd2a34e847f821
6
+ metadata.gz: 138a2ed70f1791ebf0759347558a21b0f008c890ee4f417c2c62968e055521be90fc58f1121bef8cd3399a606e6d3971d79d6e406611a1d35d8a9ef467c8e9ee
7
+ data.tar.gz: 86b874fbfeb3fcf178ec8c3d345405ba970d028dd028d193a913218ccc017d746814e3578500cada6140274229176d721ecb28e0d449049614fd62f55dbba2ee
data/.gitignore CHANGED
@@ -22,6 +22,6 @@ tmp
22
22
  .rvmrc
23
23
 
24
24
  #RBENV CONFIG
25
- .rbenv-version
25
+ # .rbenv-version
26
26
  supplejack_client.sublime-project
27
- supplejack_client.sublime-workspace
27
+ supplejack_client.sublime-workspace
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.1.2
3
+ - 2.1.9
4
+ - 2.3.1
5
5
  cache: bundler
6
6
  bundler_args: --without production
7
7
  sudo: false
data/Gemfile CHANGED
@@ -11,8 +11,10 @@ source 'http://rubygems.org'
11
11
  gemspec
12
12
 
13
13
  group :development do
14
- gem 'guard'
15
- gem 'guard-rspec'
14
+ if RUBY_VERSION >= '2.2.5'
15
+ gem 'guard'
16
+ gem 'guard-rspec'
17
+ end
16
18
  end
17
19
 
18
20
  group :test do
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Supplejack Client
1
+ # Supplejack Client [![Gem Version](https://badge.fury.io/rb/supplejack_client.svg)](https://badge.fury.io/rb/supplejack_client)
2
2
 
3
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
4
 
@@ -9,7 +9,7 @@ For more information on how to configure and use this application refer to the [
9
9
  Add it to your Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'supplejack_client', github: 'git@github.com:DigitalNZ/supplejack_client.git'
12
+ gem 'supplejack_client', git: 'git@github.com:DigitalNZ/supplejack_client.git'
13
13
  ```
14
14
 
15
15
  Run bundle install:
@@ -150,4 +150,4 @@ This program is free software: you can redistribute it and/or modify it under th
150
150
 
151
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
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

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

@@ -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
  en:
9
11
  supplejack_client:
@@ -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 Generators
@@ -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
  Supplejack.configure do |config|
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 'supplejack/search'
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
  module Supplejack
9
11
  module Config
@@ -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 'rails_autolink'
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 'supplejack_client'
9
11
  require 'rails'
@@ -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
  class RecordNotFound < StandardError
@@ -15,6 +17,9 @@ module Supplejack
15
17
  class SetNotFound < StandardError
16
18
  end
17
19
 
20
+ class StoryNotFound < StandardError
21
+ end
22
+
18
23
  class MalformedRequest < StandardError
19
24
  end
20
25
  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
  class Facet
@@ -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
 
@@ -62,7 +64,7 @@ module Supplejack
62
64
  Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching
63
65
  return true
64
66
  rescue StandardError => e
65
- self.errors = e.inspect
67
+ self.errors = e.message
66
68
  return false
67
69
  end
68
70
  end
@@ -77,7 +79,7 @@ module Supplejack
77
79
  Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching
78
80
  return true
79
81
  rescue StandardError => e
80
- self.errors = e.inspect
82
+ self.errors = e.message
81
83
  return false
82
84
  end
83
85
  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
 
@@ -58,6 +60,7 @@ module Supplejack
58
60
  #
59
61
  def create(attributes={})
60
62
  item = self.build(attributes)
63
+
61
64
  item.save
62
65
  end
63
66
 
@@ -67,7 +70,7 @@ module Supplejack
67
70
  # @example
68
71
  # user_set.items.each .... => Iterate through the Item objects array
69
72
  # user_set.items.size => Get the size of the Item objects array
70
- #
73
+ #
71
74
  def method_missing(method, *args, &block)
72
75
  @items.send(method, *args, &block)
73
76
  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
  class LogSubscriber < ActiveSupport::LogSubscriber
@@ -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
  class PaginatedCollection
@@ -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 'supplejack/search'
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 'rest-client'
9
11
 
@@ -27,7 +29,7 @@ module Supplejack
27
29
  raise e
28
30
  ensure
29
31
  duration = (Time.now - started)*1000 # Convert to miliseconds
30
- solr_request_params = result["search"]['solr_request_params'] if result && result['search']
32
+ solr_request_params = result["search"]['solr_request_params'] if result && result.is_a?(Hash) && result['search']
31
33
  @subscriber = Supplejack::LogSubscriber.new
32
34
  @subscriber.log_request(duration, payload, solr_request_params)
33
35
  end
@@ -66,6 +68,18 @@ module Supplejack
66
68
  end
67
69
  end
68
70
 
71
+ def patch(path, params: {}, payload: {}, options: {})
72
+ payload ||= {}
73
+ log_request(:patch, path, params, payload) do
74
+ response = RestClient::Request.execute(:url => full_url(path, nil, params),
75
+ :method => :patch,
76
+ :payload => payload.to_json,
77
+ :timeout => timeout(options),
78
+ :headers => {:content_type => :json, :accept => :json})
79
+ JSON.parse(response) rescue {}.to_json
80
+ end
81
+ end
82
+
69
83
  private
70
84
 
71
85
  def full_url(path, format=nil, params={})
@@ -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 'supplejack/request'
9
11
  require 'digest/md5'
@@ -0,0 +1,232 @@
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
+ class Story
13
+ extend Supplejack::Request
14
+ extend ActiveModel::Naming
15
+ include ActiveModel::Conversion
16
+
17
+ MODIFIABLE_ATTRIBUTES = [:name, :description, :privacy, :copyright, :featured, :approved, :tags, :record_ids].freeze
18
+ UNMODIFIABLE_ATTRIBUTES = [:id, :created_at, :updated_at, :number_of_items, :contents, :cover_thumbnail].freeze
19
+ ATTRIBUTES = (MODIFIABLE_ATTRIBUTES + UNMODIFIABLE_ATTRIBUTES).freeze
20
+
21
+ attr_accessor *ATTRIBUTES
22
+ attr_accessor :user, :errors, :api_key
23
+
24
+ # Define setter methods for both created_at and updated_at so that
25
+ # they always return a Time object.
26
+ #
27
+ [:created_at, :updated_at].each do |attribute|
28
+ define_method("#{attribute}=") do |time|
29
+ self.instance_variable_set("@#{attribute}", Util.time(time))
30
+ end
31
+ end
32
+
33
+ def initialize(attributes={})
34
+ @attributes = attributes.try(:symbolize_keys) || {}
35
+ @user = Supplejack::User.new(@attributes[:user])
36
+ self.attributes = @attributes
37
+ end
38
+
39
+ def items
40
+ @items ||= StoryItemRelation.new(self)
41
+ end
42
+
43
+ # Returns if the Story is persisted to the API or not.
44
+ #
45
+ # @return [ true, false ] True if the UserSet is persisted to the API, false if not.
46
+ #
47
+ def new_record?
48
+ self.id.blank?
49
+ end
50
+
51
+ def attributes
52
+ retrieve_attributes(ATTRIBUTES)
53
+ end
54
+
55
+ def api_attributes
56
+ retrieve_attributes(MODIFIABLE_ATTRIBUTES)
57
+ end
58
+
59
+ # Executes a POST request when the Story hasn't been persisted to the API, otherwise it executes
60
+ # a PATCH request.
61
+ #
62
+ # When the API returns a error response, the errors are available through the Story#errors
63
+ # virtual attribute.
64
+ #
65
+ # @return [ true, false ] True if the API response was successful, false if not.
66
+ #
67
+ def save
68
+ begin
69
+ if self.new_record?
70
+ self.attributes = self.class.post("/stories", {api_key: self.api_key}, {story: self.api_attributes})
71
+ else
72
+ self.attributes = self.class.patch("/stories/#{self.id}", payload: {story: self.api_attributes})
73
+ end
74
+
75
+ Rails.cache.delete("/users/#{self.api_key}/stories") if Supplejack.enable_caching
76
+
77
+ true
78
+ rescue StandardError => e
79
+ self.errors = e.message
80
+
81
+ false
82
+ end
83
+ end
84
+
85
+ # Executes a DELETE request to the API with the Story ID and the user's api_key
86
+ #
87
+ # When the API returns a error response, the errors are available through the Story#errors
88
+ # virtual attribute.
89
+ #
90
+ # @return [ true, false ] True if the API response was successful, false if not.
91
+ #
92
+ def destroy
93
+ return false if self.new_record?
94
+
95
+ begin
96
+ self.class.delete("/stories/#{self.id}")
97
+
98
+ Rails.cache.delete("/users/#{self.api_key}/stories") if Supplejack.enable_caching
99
+
100
+ true
101
+ rescue StandardError => e
102
+ self.errors = e.message
103
+
104
+ false
105
+ end
106
+ end
107
+
108
+ # Fetches the Story information from the API again, in case it had changed.
109
+ #
110
+ # This can be useful if they items for a Story changed and you want the relation
111
+ # to have the most up to date items.
112
+ #
113
+ def reload
114
+ begin
115
+ self.attributes = self.class.get("/stories/#{self.id}")
116
+ @items = nil
117
+ rescue RestClient::ResourceNotFound
118
+ raise Supplejack::StoryNotFound, "Story with ID #{id} was not found"
119
+ end
120
+ end
121
+
122
+ # Executes a GET request with the provided Story ID and initializes
123
+ # a Story object with the response from the API.
124
+ #
125
+ # @return [ Story ] A Story object
126
+ #
127
+ def self.find(id, api_key: nil, params: {})
128
+ begin
129
+ response = get("/stories/#{id}", params)
130
+ attributes = response || {}
131
+
132
+ story = new(attributes)
133
+ story.api_key = api_key if api_key.present?
134
+
135
+ story
136
+ rescue RestClient::ResourceNotFound
137
+ raise Supplejack::StoryNotFound, "Story with ID #{id} was not found"
138
+ end
139
+ end
140
+
141
+ # Assigns the provided attributes to the Story object
142
+ #
143
+ def attributes=(attributes)
144
+ attributes = attributes.try(:symbolize_keys) || {}
145
+ attributes.each do |attr, value|
146
+ self.send("#{attr}=", value) if ATTRIBUTES.include?(attr)
147
+ end
148
+ end
149
+
150
+ # Updates the Story attributes and persists it to the API
151
+ #
152
+ # @return [ true, false ] True if the API response was successful, false if not.
153
+ #
154
+ def update_attributes(attributes={})
155
+ self.attributes = attributes
156
+
157
+ self.save
158
+ end
159
+
160
+ # Returns the ApiKey of the User this Story belongs to
161
+ # If the api_key is set on the Story directly it uses that one instead
162
+ #
163
+ # @return [String] User ApiKey
164
+ def api_key
165
+ @api_key || @user.api_key
166
+ end
167
+
168
+ # Returns a comma separated list of tags for this Story
169
+ #
170
+ def tag_list
171
+ self.tags.join(', ') if self.tags
172
+ end
173
+
174
+ def private?
175
+ self.privacy == "private"
176
+ end
177
+
178
+ def public?
179
+ self.privacy == "public"
180
+ end
181
+
182
+ def hidden?
183
+ self.privacy == "hidden"
184
+ end
185
+
186
+ # A Story is viewable by anyone if it's public or hidden
187
+ # When the Story is private it's only viewable by the owner.
188
+ #
189
+ # @param [ Supplejack::User ] User to check if they can see it
190
+ #
191
+ # @return [ true, false ] True if the user can view the current Story, false if not.
192
+ #
193
+ def viewable_by?(user)
194
+ return true if self.public? || self.hidden?
195
+
196
+ self.owned_by?(user)
197
+ end
198
+
199
+ # Compares the api_key of the user and the api_key assigned to the Story
200
+ # to find out if the user passed is the owner of the story.
201
+ #
202
+ # @param [ Supplejack::User ] User to check if they own it
203
+ #
204
+ # @return [ true, false ] True if the user owns the current Story, false if not.
205
+ #
206
+ def owned_by?(user)
207
+ return false if user.try(:api_key).blank? || self.api_key.blank?
208
+
209
+ user.try(:api_key) == self.api_key
210
+ end
211
+
212
+ def as_json(include_contents: true)
213
+ include_contents ? attributes : attributes.except(:contents)
214
+ end
215
+
216
+ def to_json(include_contents: true)
217
+ as_json(include_contents: include_contents).to_json
218
+ end
219
+
220
+ private
221
+
222
+ def retrieve_attributes(attributes_list)
223
+ {}.tap do |attributes|
224
+ attributes_list.each do |attribute|
225
+ value = self.send(attribute)
226
+ attributes[attribute] = value
227
+ end
228
+ end
229
+ end
230
+
231
+ end
232
+ end