Skip to content

Commit

Permalink
feat: added history and events to the story object
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-mesnilgrente committed Apr 3, 2023
1 parent 77e3c82 commit 90fe21a
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/supplejack/moderation_record.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 20 additions & 7 deletions lib/supplejack/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {})
Expand Down
39 changes: 33 additions & 6 deletions lib/supplejack/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions lib/supplejack/user_story_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/supplejack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Supplejack
VERSION = '2.3.10'
VERSION = '2.3.11'
end
23 changes: 13 additions & 10 deletions lib/supplejack_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 12 additions & 7 deletions spec/supplejack/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -79,19 +79,24 @@ 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

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))

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
41 changes: 39 additions & 2 deletions spec/supplejack/story_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
{
Expand Down
8 changes: 4 additions & 4 deletions spec/supplejack/user_story_relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 90fe21a

Please sign in to comment.