-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Notification with Contents, Headings an template_id
- Loading branch information
Matteo Joliveau
committed
Jun 27, 2018
1 parent
d8f9ac1
commit b506527
Showing
19 changed files
with
292 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
source "https://rubygems.org" | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } | ||
|
||
# Specify your gem's dependencies in onesignal-ruby.gemspec | ||
gemspec | ||
|
||
group :test do | ||
gem 'faker', git: 'https://github.com/stympy/faker.git', branch: 'master' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/core_ext/string/inflections' | ||
require 'active_support/json' | ||
require 'onesignal/version' | ||
require 'onesignal/extra' | ||
|
||
module OneSignal | ||
# Your code goes here... | ||
end | ||
|
||
require 'onesignal/client' | ||
require 'onesignal/autoloader' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module OneSignal | ||
module AutoMap | ||
def create_readers **attributes | ||
attributes.keys.each do |k| | ||
self.class.define_method k do | ||
attributes[k] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Dir["#{File.expand_path('..', __FILE__)}/*.rb"].each do |file| | ||
filename = File.basename file | ||
classname = filename.split('.rb').first.camelize | ||
OneSignal.autoload classname, File.expand_path("../#{filename}", __FILE__) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class Class | ||
def auto_attr_reader *readers | ||
readers.each do |r| | ||
define_method r do | ||
@attributes[r] | ||
end | ||
end | ||
end | ||
|
||
def auto_attr_writer *writers | ||
writers.each do |w| | ||
define_method "#{w}=" do |val| | ||
@attributes[w] = val | ||
end | ||
end | ||
end | ||
|
||
def auto_attr_accessor *accessors | ||
auto_attr_reader accessors | ||
auto_attr_writer accessors | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'onesignal/notification/contents' | ||
require 'onesignal/notification/headings' | ||
|
||
module OneSignal | ||
class Notification | ||
attr_reader :contents, :headings, :template_id | ||
|
||
def initialize **params | ||
unless params.include?(:contents) || params.include?(:template_id) | ||
raise ArgumentError, 'missing contents or template_id' | ||
end | ||
|
||
@contents = params[:contents] | ||
@headings = params[:headings] | ||
@template_id = params[:template_id] | ||
end | ||
|
||
def as_json(options={}) | ||
super(options).reject { |k, v| v.nil? } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module OneSignal | ||
class Notification | ||
class Contents | ||
include OneSignal::AutoMap | ||
extend Forwardable | ||
|
||
def_delegators :@content, :as_json, :to_json | ||
|
||
def initialize en:, **content | ||
@content = content.merge(en: en) | ||
create_readers @content | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module OneSignal | ||
class Notification | ||
class Headings | ||
include OneSignal::AutoMap | ||
extend Forwardable | ||
|
||
def_delegators :@headings, :as_json, :to_json | ||
|
||
def initialize en:, **headings | ||
@headings = headings.merge(en: en) | ||
create_readers @headings | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module OneSignal | ||
module Responses | ||
# Example JSON | ||
# { | ||
# "id": '481a2734-6b7d-11e4-a6ea-4b53294fa671', | ||
# "successful": 15, | ||
# "failed": 1, | ||
# "converted": 3, | ||
# "remaining": 0, | ||
# "queued_at": 1_415_914_655, | ||
# "send_after": 1_415_914_655, | ||
# "completed_at": 1_415_914_656, | ||
# "url": 'https://yourWebsiteToOpen.com', | ||
# "data": { | ||
# "foo": 'bar', | ||
# "your": 'custom metadata' | ||
# }, | ||
# "canceled": false, | ||
# "headings": { | ||
# "en": 'English and default language heading', | ||
# "es": 'Spanish language heading' | ||
# }, | ||
# "contents": { | ||
# "en": 'English language content', | ||
# "es": 'Hola' | ||
# } | ||
# } | ||
class Notification | ||
auto_attr_reader :id, :successful, :failed, :converted, :remaining, | ||
:queued_at, :send_after, :completed_at, :url, :data, | ||
:canceled, :headings, :contents | ||
|
||
def initialize attributes = {} | ||
@attributes = attributes | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
module OneSignal | ||
VERSION = '0.1.0' | ||
API_VERSION = 'v1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :contents, class: OneSignal::Notification::Contents do | ||
initialize_with { new(en: Faker::Fallout.quote) } | ||
end | ||
|
||
factory :headings, class: OneSignal::Notification::Headings do | ||
initialize_with { new(en: Faker::Fallout.quote) } | ||
end | ||
|
||
factory :notification, class: OneSignal::Notification do | ||
initialize_with { new(contents: build(:contents), headings: build(:headings)) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'onesignal' | ||
include OneSignal | ||
|
||
describe Notification::Contents do | ||
it 'requires at least an english content' do | ||
expect { described_class.new }.to raise_error ArgumentError, 'missing keyword: en' | ||
end | ||
|
||
it 'creates a new Content with only english' do | ||
expect(described_class.new(en: 'Test').en).to eq 'Test' | ||
end | ||
|
||
it 'can have content in multiple languages' do | ||
content = described_class.new(en: 'Test', it: 'Prova', fr: 'Essai') | ||
expect(content.en).to eq 'Test' | ||
expect(content.it).to eq 'Prova' | ||
expect(content.fr).to eq 'Essai' | ||
expect { content.de }.to raise_error NoMethodError | ||
end | ||
|
||
context 'json' do | ||
subject { build :contents } | ||
|
||
it 'serializes as json' do | ||
expect(subject.as_json).to eq('en' => subject.en) | ||
end | ||
|
||
it 'serializes to json' do | ||
expect(subject.to_json).to eq "{\"en\":\"#{subject.en}\"}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'spec_helper' | ||
require 'onesignal' | ||
include OneSignal | ||
|
||
describe Notification::Headings do | ||
it 'requires at least an english content' do | ||
expect { described_class.new }.to raise_error ArgumentError, 'missing keyword: en' | ||
end | ||
|
||
it 'creates a new Content with only english' do | ||
expect(described_class.new(en: 'Test').en).to eq 'Test' | ||
end | ||
|
||
it 'can have content in multiple languages' do | ||
content = described_class.new(en: 'Test', it: 'Prova', fr: 'Essai') | ||
expect(content.en).to eq 'Test' | ||
expect(content.it).to eq 'Prova' | ||
expect(content.fr).to eq 'Essai' | ||
expect { content.de }.to raise_error NoMethodError | ||
end | ||
|
||
context 'json' do | ||
subject { build :contents } | ||
|
||
it 'serializes as json' do | ||
expect(subject.as_json).to eq('en' => subject.en) | ||
end | ||
|
||
it 'serializes to json' do | ||
expect(subject.to_json).to eq "{\"en\":\"#{subject.en}\"}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'onesignal' | ||
include OneSignal | ||
|
||
describe Notification do | ||
let(:contents) { build :contents } | ||
let(:headings) { build :headings } | ||
|
||
it 'requires at least some contents' do | ||
expect { described_class.new }.to raise_error ArgumentError, 'missing contents or template_id' | ||
end | ||
|
||
it 'creates a new notification' do | ||
expect(described_class.new(contents: contents, headings: headings)).to be_instance_of Notification | ||
end | ||
|
||
context 'json' do | ||
subject { build :notification } | ||
|
||
it 'serializes as json' do | ||
expect(subject.as_json).to include( | ||
'contents' => { | ||
'en' => subject.contents.en | ||
}, | ||
'headings' => { | ||
'en' => subject.headings.en | ||
} | ||
) | ||
end | ||
|
||
it 'serializes to json' do | ||
result = "{\"contents\":{\"en\":\"#{subject.contents.en}\"},"\ | ||
"\"headings\":{\"en\":\"#{subject.headings.en}\"}}" | ||
expect(subject.to_json).to eq result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'factory_bot' | ||
|
||
RSpec.configure do |config| | ||
config.include FactoryBot::Syntax::Methods | ||
|
||
config.before(:suite) do | ||
FactoryBot.find_definitions | ||
end | ||
end |