Skip to content

Commit

Permalink
Implemented Notification with Contents, Headings an template_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Joliveau committed Jun 27, 2018
1 parent d8f9ac1 commit b506527
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Gemfile
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
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/stympy/faker.git
revision: 21c66bafbd1371b74c0e7a87a71658b08c607c7f
branch: master
specs:
faker (1.8.7)
i18n (>= 0.7)

PATH
remote: .
specs:
Expand All @@ -20,6 +28,8 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
dotenv (2.5.0)
factory_bot (4.10.0)
activesupport (>= 3.0.0)
faraday (0.15.2)
multipart-post (>= 1.2, < 3)
hashdiff (0.3.7)
Expand Down Expand Up @@ -58,6 +68,8 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.16)
dotenv (~> 2.5)
factory_bot (~> 4.10)
faker!
onesignal-ruby!
rake (~> 10.0)
rspec (~> 3.0)
Expand Down
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'onesignal/ruby'
require 'onesignal'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
6 changes: 4 additions & 2 deletions lib/onesignal.rb
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'
11 changes: 11 additions & 0 deletions lib/onesignal/auto_map.rb
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
5 changes: 5 additions & 0 deletions lib/onesignal/autoloader.rb
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
24 changes: 24 additions & 0 deletions lib/onesignal/extra.rb
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
22 changes: 22 additions & 0 deletions lib/onesignal/notification.rb
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
15 changes: 15 additions & 0 deletions lib/onesignal/notification/contents.rb
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
15 changes: 15 additions & 0 deletions lib/onesignal/notification/headings.rb
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
40 changes: 40 additions & 0 deletions lib/onesignal/responses/notification.rb
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
1 change: 1 addition & 0 deletions lib/onesignal/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module OneSignal
VERSION = '0.1.0'
API_VERSION = 'v1'
end
1 change: 1 addition & 0 deletions onesignal-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'dotenv', '~> 2.5'
spec.add_development_dependency 'factory_bot', '~> 4.10'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'vcr', '~> 4.0.0'
Expand Down
15 changes: 15 additions & 0 deletions spec/factories/notifications.rb
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
35 changes: 35 additions & 0 deletions spec/onesignal/notification/contents_spec.rb
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
33 changes: 33 additions & 0 deletions spec/onesignal/notification/headings_spec.rb
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
39 changes: 39 additions & 0 deletions spec/onesignal/notification_spec.rb
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
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'onesignal'
require 'vcr'
require 'onesignal'
require 'support/factory_bot'
require 'faker'

SPEC_ROOT = __dir__

Expand Down
9 changes: 9 additions & 0 deletions spec/support/factory_bot.rb
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

0 comments on commit b506527

Please sign in to comment.