From 72ed2ae4bcc84c951ead0e6d724f87632eb0b868 Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Thu, 13 Feb 2025 11:45:34 +0100 Subject: [PATCH 1/4] Disable ActiveJob logging --- spec/support/active_job.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/support/active_job.rb b/spec/support/active_job.rb index a8e352f..00758d9 100644 --- a/spec/support/active_job.rb +++ b/spec/support/active_job.rb @@ -1,3 +1,4 @@ require 'active_job' ActiveJob::Base.queue_adapter = :test +ActiveJob::Base.logger = Logger.new(nil) From 4653243d0538ad39a68bf9e120b1c95a8353655c Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Thu, 13 Feb 2025 11:47:03 +0100 Subject: [PATCH 2/4] Adds basic Push::Message endpoint --- lib/userlist/push.rb | 11 +++++++- lib/userlist/push/message.rb | 19 ++++++++++++++ spec/userlist/push/message_spec.rb | 42 ++++++++++++++++++++++++++++++ spec/userlist/push_spec.rb | 32 +++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 lib/userlist/push/message.rb create mode 100644 spec/userlist/push/message_spec.rb diff --git a/lib/userlist/push.rb b/lib/userlist/push.rb index 80ff428..74e1aa9 100644 --- a/lib/userlist/push.rb +++ b/lib/userlist/push.rb @@ -12,13 +12,14 @@ require 'userlist/push/company' require 'userlist/push/relationship' require 'userlist/push/event' +require 'userlist/push/message' require 'userlist/push/serializer' module Userlist class Push class << self - [:event, :track, :user, :identify, :company, :users, :events, :companies, :relationships].each do |method| + [:event, :track, :user, :identify, :company, :message, :users, :events, :companies, :relationships, :messages].each do |method| define_method(method) { |*args| default_push_instance.send(method, *args) } end @@ -52,6 +53,10 @@ def relationships @relationships ||= Relation.new(self, Relationship, [Operations::Create, Operations::Delete]) end + def messages + @messages ||= Relation.new(self, Message, [Operations::Create]) + end + def event(payload = {}) events.create(payload) end @@ -64,6 +69,10 @@ def company(payload = {}) companies.create(payload) end + def message(payload = {}) + messages.create(payload) + end + alias track event alias identify user end diff --git a/lib/userlist/push/message.rb b/lib/userlist/push/message.rb new file mode 100644 index 0000000..3c01c72 --- /dev/null +++ b/lib/userlist/push/message.rb @@ -0,0 +1,19 @@ +module Userlist + class Push + class Message < Resource + include Operations::Create + + has_one :user, type: 'Userlist::Push::User' + + def initialize(payload = {}, config = Userlist.config) + raise Userlist::ArgumentError, 'Missing required payload' unless payload + + super + end + + def push? + super && (user.nil? || user.push?) + end + end + end +end diff --git a/spec/userlist/push/message_spec.rb b/spec/userlist/push/message_spec.rb new file mode 100644 index 0000000..8c18076 --- /dev/null +++ b/spec/userlist/push/message_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +RSpec.describe Userlist::Push::Message do + let(:payload) do + { + template: 'template-identifier', + user: 'user-identifier', + properties: { + value: '$100.00' + } + } + end + + subject { described_class.new(payload) } + + it 'should raise an error when no payload is given' do + expect { described_class.new(nil) }.to raise_error(Userlist::ArgumentError, /payload/) + end + + it 'should be pushable' do + expect(subject.push?).to be_truthy + end + + context 'when a user hash is given' do + let(:payload) do + super().merge( + user: { + identifier: 'user-identifier', + email: 'foo@example.com' + } + ) + end + + it 'should convert it into a user object' do + expect(subject.user).to be_kind_of(Userlist::Push::User) + end + + it 'should be pushable' do + expect(subject.push?).to be_truthy + end + end +end diff --git a/spec/userlist/push_spec.rb b/spec/userlist/push_spec.rb index 37757dd..6693c7c 100644 --- a/spec/userlist/push_spec.rb +++ b/spec/userlist/push_spec.rb @@ -160,4 +160,36 @@ subject.company(payload) end end + + describe '#messages' do + let(:relation) { subject.messages } + + it 'should return a relation' do + expect(relation).to be_an_instance_of(Userlist::Push::Relation) + expect(relation.type).to eq(Userlist::Push::Message) + end + + it 'should support the create operation' do + expect(relation).to be_kind_of(Userlist::Push::Operations::Create::ClassMethods) + end + end + + describe '#message' do + let(:payload) do + { + template: 'template-identifier', + user: 'user-identifier', + properties: { + value: '$100.00' + } + } + end + + let(:relation) { subject.messages } + + it 'should delegate the call to the relation\'s create method' do + expect(relation).to receive(:create).with(payload) + subject.message(payload) + end + end end From 1e0ba822bf4e157a8b99d7401f7bddfd543f87bf Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Thu, 13 Feb 2025 11:47:44 +0100 Subject: [PATCH 3/4] Adds irb to the Gemfile --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 81a8879..74bfbf2 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,4 @@ gem 'rubocop', '~> 1.45' gem 'sidekiq' gem 'activejob' gem 'uri' +gem 'irb' From ea979f1b0222d143f474f4bafdb6dbe830446417 Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Thu, 13 Feb 2025 13:33:33 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de952ab..fe6b772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Updates ActiveJob Worker to retry on errors with polynomially longer wait times, up to 10 attempts - Improve internal error handling to rely on exceptions +- Adds support for messages ## v0.9.0 (2024-03-19)