This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Bajena/8-open_graph
Add UI::FBOpenGraphTemplate
- Loading branch information
Showing
13 changed files
with
165 additions
and
61 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
|
||
module UI | ||
class BaseUiElement | ||
# Sends the valid JSON to Messenger API | ||
def send(user) | ||
formed = build(user) | ||
Bot.deliver(formed, access_token: ENV['ACCESS_TOKEN']) | ||
end | ||
|
||
# Use this method to return a valid hash and save it for later | ||
def build(user) | ||
@template[:recipient][:id] = user.id | ||
@template | ||
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module UI | ||
module Common | ||
module HasButtons | ||
private | ||
|
||
def parse_buttons(buttons) | ||
return [] if buttons.nil? || buttons.empty? | ||
buttons.map do |button| | ||
button[:type] = button[:type].to_s | ||
button | ||
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
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,42 @@ | ||
# rubocop:disable Metrics/MethodLength | ||
|
||
module UI | ||
########################### OPEN GRAPH TEMPLATE ############################# | ||
# https://developers.facebook.com/docs/messenger-platform/send-messages/template/open-graph | ||
class FBOpenGraphTemplate < UI::BaseUiElement | ||
include UI::Common::HasButtons | ||
|
||
def initialize(url, buttons = []) | ||
@url = url | ||
@buttons = buttons | ||
|
||
@template = { | ||
recipient: { | ||
id: nil | ||
}, | ||
message: { | ||
attachment: { | ||
type: 'template', | ||
payload: { | ||
template_type: 'open_graph', | ||
elements: elements | ||
} | ||
} | ||
} | ||
} | ||
end | ||
|
||
private | ||
|
||
attr_reader :url, :buttons | ||
|
||
def elements | ||
res = { url: url } | ||
|
||
buttons_payload = parse_buttons(buttons) | ||
res[:buttons] = buttons_payload if buttons_payload.any? | ||
|
||
[res] | ||
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
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :user, class: User do | ||
skip_create | ||
initialize_with { new(attributes[:id].to_s) } | ||
|
||
sequence(:id) { |n| n } | ||
|
||
trait :with_commands do | ||
transient do | ||
commands { [] } | ||
end | ||
|
||
after(:build) do |user, evaluator| | ||
evaluator.commands.each { |c| user.assign_command(c) } | ||
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
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe UI::FBOpenGraphTemplate do | ||
let(:url) { 'https://github.com/progapandist/rubotnik' } | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
describe 'build' do | ||
let(:result) { described_class.new(url).build(user) } | ||
let(:elements) { result[:message][:attachment][:payload][:elements].first } | ||
let(:recipient_id) { result[:recipient][:id] } | ||
|
||
it 'returns correct template' do | ||
expect(recipient_id).to eq(user.id) | ||
expect(elements.length).to eq(1) | ||
expect(elements).to eq(url: url) | ||
end | ||
|
||
context 'when buttons passed' do | ||
let(:buttons) do | ||
[ | ||
{ | ||
type: 'web_url', | ||
url: url, | ||
title: 'abc' | ||
} | ||
] | ||
end | ||
let(:result) { described_class.new(url, buttons).build(user) } | ||
|
||
it 'adds buttons to template' do | ||
expect(elements[:buttons].first[:type]).to eq('web_url') | ||
expect(elements[:buttons].first[:title]).to eq('abc') | ||
expect(elements[:buttons].first[:url]).to eq(url) | ||
end | ||
end | ||
end | ||
|
||
describe 'send' do | ||
it 'sends the element' do | ||
expect(Bot).to receive(:deliver) | ||
|
||
described_class.new(url).send(user) | ||
end | ||
end | ||
end |