diff --git a/Gemfile b/Gemfile index 2c9321e..a170a4b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ # frozen_string_literal: true -source 'http://rubygems.org' +source 'https://rubygems.org' # Specify your gem's dependencies in supplejack.gemspec gemspec diff --git a/lib/supplejack/moderation_record.rb b/lib/supplejack/moderation_record.rb new file mode 100644 index 0000000..0971174 --- /dev/null +++ b/lib/supplejack/moderation_record.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Supplejack + class ModerationRecord + attr_reader :id, :created_at, :updated_at, :state, :user + + def initialize(id:, created_at:, updated_at:, user:, state:) + @id = id + @created_at = Util.time(created_at) + @updated_at = Util.time(updated_at) + @user = User.new(user) + @state = state + end + end +end diff --git a/lib/supplejack/request.rb b/lib/supplejack/request.rb index a75f42f..19ef2ae 100644 --- a/lib/supplejack/request.rb +++ b/lib/supplejack/request.rb @@ -14,7 +14,14 @@ def get(path, params = {}, options = {}) payload = { path: path, params: params, options: options } begin - result = RestClient::Request.execute(url: url, method: :get, read_timeout: timeout(options), headers: { 'Authentication-Token': authentication_token(options) }) + result = RestClient::Request.execute( + url: url, + method: :get, + read_timeout: timeout(options), + headers: { + 'Authentication-Token': authentication_token(options) + } + ) result = JSON.parse(result) if result rescue RestClient::ServiceUnavailable => e retry unless (tries -= 1).zero? @@ -89,11 +96,17 @@ def patch(path, params = {}, payload = {}, options = {}) log_request(:patch, path, params, payload) do response = begin - RestClient::Request.execute(url: full_url(path, nil, params), - method: :patch, - payload: payload.to_json, - timeout: timeout(options), - headers: { content_type: :json, accept: :json, 'Authentication-Token': authentication_token(options) }) + RestClient::Request.execute( + url: full_url(path, nil, params), + method: :patch, + payload: payload.to_json, + timeout: timeout(options), + headers: { + content_type: :json, + accept: :json, + 'Authentication-Token': authentication_token(options) + } + ) rescue RestClient::ExceptionWithResponse => e e.response.body end @@ -125,7 +138,7 @@ def timeout(options = {}) end def authentication_token(options) - options[:authetication_token] || Supplejack.api_key + options[:authentication_token] || Supplejack.api_key end def log_request(method, path, params = {}, payload = {}) diff --git a/lib/supplejack/story.rb b/lib/supplejack/story.rb index e2de228..10ba786 100644 --- a/lib/supplejack/story.rb +++ b/lib/supplejack/story.rb @@ -55,9 +55,9 @@ def api_attributes # def save response = if new_record? - self.class.post('/stories', { user_key: api_key }, story: api_attributes) + self.class.post('/stories', { user_key: api_key }, { story: api_attributes }) else - self.class.patch("/stories/#{id}", { user_key: api_key }, story: api_attributes) + self.class.patch("/stories/#{id}", { user_key: api_key }, { story: api_attributes }) end Rails.cache.delete("/users/#{api_key}/stories") if Supplejack.enable_caching @@ -234,11 +234,38 @@ def viewable_by?(user) # all public UserSet objects. # # @return [ Array ] A array of Supplejack::UserSet objects - - def self.all_public_stories(options = {}) - response = get('/stories/moderations', options) + def self.all_public_stories(params = {}) + response = get('/stories/moderations', params) response['sets'].map! { |attrs| new(attrs) } || [] - options[:meta_included] ? response : response['sets'] + params[:meta_included] ? response : response['sets'] + end + + # Executes a GET request to the API /stories/#{id}/history endpoint to retrieve + # the history of changes on a specific story + # + # @return [ Array ] A array of Supplejack::ModerationRecord objects + def self.history(user_key:, id: nil, page: 1, per_page: 20) + id ||= attributes[:id] + response = get("/stories/#{id}/history", user_key: user_key, page: page, per_page: per_page) + response.map { |attributes| Supplejack::ModerationRecord.new(**attributes.deep_symbolize_keys) } + end + + def history(params) + self.class.history(id: id, **params) + end + + def send_event(event, api_token) + self.attributes = self.class.patch( + "/stories/#{id}/event", + { user_key: api_token }, + { event: event } + ) + + true + rescue StandardError => e + self.errors = e.message + + false end # Compares the api_key of the user and the api_key assigned to the Story diff --git a/lib/supplejack/user_story_relation.rb b/lib/supplejack/user_story_relation.rb index 09edb51..03651c3 100644 --- a/lib/supplejack/user_story_relation.rb +++ b/lib/supplejack/user_story_relation.rb @@ -100,7 +100,7 @@ def to_json(include_contents: true) end # Any method missing on this class is delegated to the Stories objects array - # so that the developer can easily execute any Array method on the UserSttoryRelation + # so that the developer can easily execute any Array method on the UserStoryRelation # # @example # user.stories.each .... => Iterate through the Story objects array @@ -117,14 +117,8 @@ def respond_to_missing?(_method, *_args, &_block) private def fetch_api_stories - params = {} - - if user.use_own_api_key? - path = '/stories' - params[:api_key] = user.api_key - else - path = "/users/#{user.api_key}/stories" - end + params = { user_key: user.api_key } + path = user.use_own_api_key? ? '/stories' : "/users/#{user.id}/stories" if Supplejack.enable_caching Rails.cache.fetch(path, expires_in: 1.day) do diff --git a/lib/supplejack/version.rb b/lib/supplejack/version.rb index 6ef1583..006ac12 100644 --- a/lib/supplejack/version.rb +++ b/lib/supplejack/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Supplejack - VERSION = '2.3.10' + VERSION = '2.3.11' end diff --git a/lib/supplejack_client.rb b/lib/supplejack_client.rb index 5cef34b..8f7a7c9 100644 --- a/lib/supplejack_client.rb +++ b/lib/supplejack_client.rb @@ -13,24 +13,27 @@ module Supplejack extend Config require 'supplejack/engine' + + # alphabetically ordered + require 'supplejack/concept' + require 'supplejack/controllers/helpers' require 'supplejack/exceptions' + require 'supplejack/facet' + require 'supplejack/item' + require 'supplejack/item_relation' require 'supplejack/log_subscriber' - require 'supplejack/record' + require 'supplejack/moderation_record' require 'supplejack/more_like_this_record' - require 'supplejack/concept' - require 'supplejack/request' require 'supplejack/paginated_collection' - require 'supplejack/controllers/helpers' - require 'supplejack/url_formats/item_hash' - require 'supplejack/util' - require 'supplejack/facet' - require 'supplejack/user_set' + require 'supplejack/record' + require 'supplejack/request' require 'supplejack/story' require 'supplejack/story_item' require 'supplejack/story_item_relation' - require 'supplejack/item_relation' - require 'supplejack/item' + require 'supplejack/url_formats/item_hash' require 'supplejack/user' + require 'supplejack/user_set' require 'supplejack/user_set_relation' require 'supplejack/user_story_relation' + require 'supplejack/util' end diff --git a/spec/supplejack/request_spec.rb b/spec/supplejack/request_spec.rb index dd97c13..2b24eaf 100644 --- a/spec/supplejack/request_spec.rb +++ b/spec/supplejack/request_spec.rb @@ -21,7 +21,7 @@ module Supplejack describe '#get' do before { allow(RestClient::Request).to receive(:execute).and_return(%( {"search": {}} )) } - context 'when authetication_token not passed in options' do + context 'when authentication_token not passed in options' do it 'serializes the parameters in the url' do expect(RestClient::Request).to receive(:execute) .with(url: "http://api.org/records.json?#{{ and: { name: 'John' } }.to_query}", @@ -79,11 +79,16 @@ module Supplejack end end - context 'when authetication_token is passed in options' do + context 'when authentication_token is passed in options' do it 'overrides the Supplejack.api_key' do - expect(RestClient::Request).to receive(:execute).with(hash_including(url: 'http://api.org/records.json', headers: { 'Authentication-Token': '456' })) + expect(RestClient::Request).to receive(:execute).with( + hash_including( + url: 'http://api.org/records.json', + headers: { 'Authentication-Token': '456' } + ) + ) - requester.get('/records', {}, { authetication_token: '456' }) + requester.get('/records', {}, { authentication_token: '456' }) end end end @@ -91,7 +96,7 @@ module Supplejack describe '#post' do before { allow(RestClient::Request).to receive(:execute).and_return(true) } - context 'when authetication_token not passed in options' do + context 'when authentication_token not passed in options' do it 'executes a post request' do expect(RestClient::Request).to receive(:execute).with(hash_including(method: :post)) @@ -130,7 +135,7 @@ module Supplejack end end - context 'when authetication_token is passed in options' do + context 'when authentication_token is passed in options' do it 'overrides the Supplejack.api_key' do expect(RestClient::Request).to receive(:execute).with( hash_including( @@ -139,7 +144,7 @@ module Supplejack ) ) - requester.post('/records/1/ucm', {}, { records: [{ record_id: 1 }] }, { authetication_token: '456' }) + requester.post('/records/1/ucm', {}, { records: [{ record_id: 1 }] }, { authentication_token: '456' }) end end end diff --git a/spec/supplejack/story_spec.rb b/spec/supplejack/story_spec.rb index aef92b9..d5b5aa9 100644 --- a/spec/supplejack/story_spec.rb +++ b/spec/supplejack/story_spec.rb @@ -150,7 +150,11 @@ module Supplejack let(:story) { described_class.new(attributes.merge(user: user)) } before do - allow(described_class).to receive(:post).with('/stories', { user_key: 'foobar' }, story: attributes) do + allow(described_class).to receive(:post).with( + '/stories', + { user_key: 'foobar' }, + { story: attributes } + ) do { 'id' => 'new-id', 'name' => attributes[:name], @@ -194,7 +198,11 @@ module Supplejack let(:story) { described_class.new(attributes.merge(user: user, id: '123')) } before do - allow(described_class).to receive(:patch).with('/stories/123', { user_key: user[:api_key] }, story: attributes) do + allow(described_class).to receive(:patch).with( + '/stories/123', + { user_key: user[:api_key] }, + { story: attributes } + ) do { 'id' => 'new-id', 'name' => attributes[:name], @@ -439,6 +447,35 @@ module Supplejack end end + describe '#history' do + let(:api_key) { '123456' } + let(:story) { described_class.new(id: 'story_1') } + let(:response) do + [ + { + id: 'moderation_record_id_1', + created_at: '2023-03-23T13:11:44.828+13:00', + updated_at: '2023-03-23T13:11:44.828+13:00', + user: { id: 'user_id_1', username: 'username_1' }, + state: 'Remoderate' + }, + { + id: 'moderation_record_id_2', + created_at: '2023-03-23T23:22:44.828+23:00', + updated_at: '2023-03-23T23:22:44.828+23:00', + user: { id: 'user_id_2', username: 'username_2' }, + state: 'Remoderate' + } + ] + end + + it 'returns an array with moderation records' do + allow(described_class).to receive(:get).and_return(response) + + expect(story.history(user_key: api_key).count).to eq(2) + end + end + describe '#find' do let(:attributes) do { diff --git a/spec/supplejack/user_story_relation_spec.rb b/spec/supplejack/user_story_relation_spec.rb index 7a27664..2c85b6a 100644 --- a/spec/supplejack/user_story_relation_spec.rb +++ b/spec/supplejack/user_story_relation_spec.rb @@ -4,7 +4,7 @@ module Supplejack describe UserStoryRelation do - let(:user) { Supplejack::User.new(authentication_token: '123abc') } + let(:user) { Supplejack::User.new(id: 'user_id_1', authentication_token: '123abc') } let(:relation) { described_class.new(user) } before do @@ -49,17 +49,17 @@ module Supplejack describe '#fetch' do context 'when use_own_api_key is false' do it 'fetches the users stories with the App api_key' do - expect(relation).to receive(:get).with('/users/123abc/stories', {}) + expect(relation).to receive(:get).with('/users/user_id_1/stories', { user_key: '123abc' }) relation.fetch end end context 'when use_own_api_key is true' do - let(:user) { Supplejack::User.new(api_key: '123abc', use_own_api_key: true) } + let(:user) { Supplejack::User.new(id: 'user_id_1', api_key: '123abc', use_own_api_key: true) } it 'fetches the users stories with their api_key' do - expect(relation).to receive(:get).with('/stories', { api_key: '123abc' }) + expect(relation).to receive(:get).with('/stories', { user_key: '123abc' }) relation.fetch end