Permalink
Browse files

rubocoppin πŸš”πŸš¨

  • Loading branch information...
stevenosloan committed Nov 23, 2017
1 parent 273d798 commit d656b239241c53687b6179e7524259285bedddc7
@@ -6,35 +6,23 @@ AllCops:
- 'Rakefile'
- 'bin/test'

# Naming ------------------------------------

# Style ------------------------------------

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/FileName:
Naming/FileName:
Exclude:
- lib/slack-notifier.rb
- spec/lib/slack-notifier_spec.rb
- slack-notifier.gemspec
- Gemfile

Style/MultilineOperationIndentation:
EnforcedStyle: aligned
# Style ------------------------------------

Style/MultilineMethodCallIndentation:
EnforcedStyle: aligned
Style/StringLiterals:
EnforcedStyle: double_quotes

Style/MethodDefParentheses:
EnforcedStyle: require_no_parentheses_except_multiline

Style/SpaceAroundEqualsInParameterDefault:
EnforcedStyle: no_space

Style/IndentationConsistency:
EnforcedStyle: rails

Style/MultilineOperationIndentation:
Enabled: false

Style/Documentation:
Enabled: false

@@ -48,6 +36,22 @@ Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%w': '[]'

# Layout ----------------------------------

Layout/MultilineOperationIndentation:
EnforcedStyle: aligned

Layout/MultilineMethodCallIndentation:
EnforcedStyle: aligned

Layout/MultilineOperationIndentation:
Enabled: false

Layout/SpaceAroundEqualsInParameterDefault:
EnforcedStyle: no_space

Layout/IndentationConsistency:
EnforcedStyle: rails

# Metrics ----------------------------------

@@ -60,6 +64,9 @@ Metrics/AbcSize:
Metrics/MethodLength:
Enabled: false

Metrics/BlockLength:
Exclude:
- spec/**/*

# Lint -------------------------------------

@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
@@ -7,4 +7,4 @@ rubocop = RuboCop::RakeTask.new
rubocop.fail_on_error = false
RSpec::Core::RakeTask.new(:spec)

task default: [:rubocop, :spec]
task default: %i[rubocop spec]
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require "uri"
require "json"

@@ -1,15 +1,16 @@
# frozen_string_literal: true

module Slack
class Notifier
class Config
def initialize
@http_client = Util::HTTPClient
@defaults = {}
@middleware = [
:format_message,
:format_attachments,
:at,
:channels,
@middleware = %i[
format_message
format_attachments
at
channels
]
end

@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
class PayloadMiddleware
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
class PayloadMiddleware
@@ -1,16 +1,17 @@
# frozen_string_literal: true

module Slack
class Notifier
class PayloadMiddleware
class FormatAttachments < Base
middleware_name :format_attachments

options formats: [:html, :markdown]
options formats: %i[html markdown]

def call payload={}
payload = payload.dup
attachments = payload.delete(:attachments)
attachments = payload.delete("attachments") unless attachments
attachments ||= payload.delete("attachments")

attachments = wrap_array(attachments).map do |attachment|
["text", :text].each do |key|
@@ -1,11 +1,12 @@
# frozen_string_literal: true

module Slack
class Notifier
class PayloadMiddleware
class FormatMessage < Base
middleware_name :format_message

options formats: [:html, :markdown]
options formats: %i[html markdown]

def call payload={}
return payload unless payload[:text]
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
class PayloadMiddleware
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
module Util
@@ -2,7 +2,6 @@

require "net/http"


module Slack
class Notifier
class APIError < StandardError; end
@@ -23,6 +22,7 @@ def initialize uri, params
@params = params
end

# rubocop:disable Layout/IndentHeredoc
def call
http_obj.request(request_obj).tap do |response|
unless response.is_a?(Net::HTTPSuccess)
@@ -33,6 +33,7 @@ def call
end
end
end
# rubocop:enable Layout/IndentHeredoc

private

@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
module Util
@@ -26,7 +27,6 @@ class LinkFormatter
(?! [#{VALID_PATH_CHARS}]* \) )
}x


class << self
def format string, opts={}
LinkFormatter.new(string, opts).formatted
@@ -35,12 +35,12 @@ def format string, opts={}

attr_reader :formats

def initialize string, formats: [:html, :markdown]
def initialize string, formats: %i[html markdown]
@formats = formats
@orig = string.respond_to?(:scrub) ? string.scrub : string
end

# rubocop:disable Style/GuardClause
# rubocop:disable Style/GuardClause, Lint/RescueWithoutErrorClass
def formatted
return @orig unless @orig.respond_to?(:gsub)

@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slack
class Notifier
VERSION = "2.3.1".freeze # rubocop:disable Style/RedundantFreeze
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path("../lib/slack-notifier/version", __FILE__)

Gem::Specification.new do |s|
@@ -1,5 +1,6 @@
# frozen_string_literal: true
# encoding: utf-8

require "spec_helper"

RSpec.describe Slack::Notifier do
@@ -54,15 +55,15 @@
text: "attachment message [hodor](http://winterfell.com)",
fallback: "fallback message" } } =>
{ payload: { attachments: [{ color: "#000",
text: "attachment message <http://winterfell.com|hodor>",
fallback: "fallback message" }] } },
text: "attachment message <http://winterfell.com|hodor>",
fallback: "fallback message" }] } },

{ attachments: { color: "#000",
text: nil,
fallback: "fallback message" } } =>
{ payload: { attachments: [{ color: "#000",
text: nil,
fallback: "fallback message" }] } },
text: nil,
fallback: "fallback message" }] } },

{ text: "hello", http_options: { timeout: 5 } } =>
{ http_options: { timeout: 5 }, payload: { text: "hello" } }
@@ -1,5 +1,6 @@
# frozen_string_literal: true
# encoding: utf-8

require_relative "../../lib/slack-notifier"

ruby = if defined?(JRUBY_VERSION)
@@ -46,17 +46,17 @@
it "is [:format_message, :format_attachments, :at] if not set" do
subject = described_class.new

expect(subject.middleware).to eq [:format_message, :format_attachments, :at, :channels]
expect(subject.middleware).to eq %i[format_message format_attachments at channels]
end

it "takes an array or a splat of args" do
subject = described_class.new

subject.middleware :layer, :two
expect(subject.middleware).to eq [:layer, :two]
expect(subject.middleware).to eq %i[layer two]

subject.middleware [:one, :layer]
expect(subject.middleware).to eq [:one, :layer]
subject.middleware %i[one layer]
expect(subject.middleware).to eq %i[one layer]
end

it "allows passing options to middleware stack" do
@@ -3,7 +3,7 @@
RSpec.describe Slack::Notifier::PayloadMiddleware::At do
it "can handle array at" do
subject = described_class.new(:notifier)
payload = { text: "hello", at: [:john, :ken, :here] }
payload = { text: "hello", at: %i[john ken here] }

expect(subject.call(payload)).to eq text: "<@john> <@ken> <!here> hello"
end
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe Slack::Notifier::PayloadMiddleware::Base do
before(:each) do
@registry_backup = Slack::Notifier::PayloadMiddleware.registry.dup
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe Slack::Notifier::PayloadMiddleware::FormatAttachments do
it "passes the text of attachments through linkformatter with options[:formats]" do
subject = described_class.new(:notifier, formats: [:html])
@@ -10,30 +11,30 @@
it "searches through string or symbol keys" do
subject = described_class.new(:notifier)
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
.with("hello", formats: [:html, :markdown])
.with("hello", formats: %i[html markdown])
subject.call("attachments" => [{ "text" => "hello" }])

subject = described_class.new(:notifier)
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
.with("hello", formats: [:html, :markdown])
.with("hello", formats: %i[html markdown])
subject.call(attachments: [{ text: "hello" }])
end

it "can handle a single attachment" do
subject = described_class.new(:notifier)
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
.with("hello", formats: [:html, :markdown])
.with("hello", formats: %i[html markdown])
subject.call(attachments: { text: "hello" })
end

it "wraps attachment into array if given as a single hash" do
params = {
params = {
attachments: { text: "hello" }
}
payload = {
attachments: [{ text: "hello" }]
}
subject = described_class.new(:notifier);
subject = described_class.new(:notifier)

expect(subject.call(params)).to eq payload
end
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe Slack::Notifier::PayloadMiddleware::FormatMessage do
it "passes the text through linkformatter with options[:formats]" do
subject = described_class.new(:notifier, formats: [:html])
@@ -8,7 +9,7 @@

subject = described_class.new(:notifier)
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
.with("hello", formats: [:html, :markdown])
.with("hello", formats: %i[html markdown])
subject.call(text: "hello")

subject = described_class.new(:notifier, formats: [:markdown])
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
let(:return_one) do
double(call: 1)
@@ -113,7 +114,6 @@
subject.set(:return_one_twice, :return_one_twice, :return_two)

expect(subject.call(5)).to eq [2, 2, 2, 2]

end
end
end
@@ -1,4 +1,5 @@
# frozen_string_literal: true

RSpec.describe Slack::Notifier::PayloadMiddleware do
before(:each) do
@registry_backup = described_class.registry.dup
Oops, something went wrong.

0 comments on commit d656b23

Please sign in to comment.