Skip to content

Instantly share code, notes, and snippets.

@serradura
Last active March 11, 2016 19:22
Show Gist options
  • Save serradura/236f0cd806fd15029e4d to your computer and use it in GitHub Desktop.
Save serradura/236f0cd806fd15029e4d to your computer and use it in GitHub Desktop.

Links

Template Method

https://en.wikipedia.org/wiki/Template_method_pattern

Exemplo em coffeescript:

class Template
  constructor: (@name) ->

  greet: -> throw "NotImplementedError"

command = new Template("foo")
command.greet()

class Polite extends Template
  greet: -> "Hello #{@name}"

command = new Polite("foo")
alert command.greet()
# app/lib/slash_command.rb
module SlashCommand
Invoke.setup help: Commands::Help,
default: Commands::Default,
unknown: Commands::Unknown,
available: [
Commands::Sum, # NEW COMMAND
Commands::In,
Commands::Out,
Commands::Now,
Commands::Edit,
Commands::Kill,
Commands::Resume,
Commands::Display,
Commands::Today,
Commands::Yesterday
]
end
# app/lib/slash_command/commands/sum.rb
# frozen_string_literal: true
module SlashCommand
module Commands
class Sum < Template
NAME = "sum"
DESC = "Sum two numbers"
HELP = "e.g: sum 1 1"
def call
response.result = data.split.map(&:to_i).reduce(&:+)
end
end
end
end
# spec/requests/api/v1/commands/invoke/sum_command_spec.rb
# Running the test: rspec spec/requests/api/v1/commands/invoke/sum_command_spec.rb
require "rails_helper"
RSpec.describe "POST /api/v1/commands/invoke", type: :request do
describe "sum command" do
let(:payload) { create(:slack_payload, text: "sum 1 2 3") }
before do
post api_v1_commands_invoke_path, payload.to_h
end
it "responds with a valid sum" do
expect(response).to have_http_status(200)
expect(response.body).to be == "6"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment