Skip to content

Commit 81f406d

Browse files
author
Will Myers
authored
Integrate Linting (#8)
* Integrate rubocop in rake test + lint updates + is_webhook_authentic rename * Updates
1 parent d7554a1 commit 81f406d

12 files changed

Lines changed: 58 additions & 27 deletions

File tree

.rubocop.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Metrics/AbcSize:
2+
Max: 40
3+
4+
Metrics/MethodLength:
5+
Max: 80
6+
7+
Metrics/ClassLength:
8+
Max: 200
9+
10+
Metrics/CyclomaticComplexity:
11+
Max: 10
12+
13+
Metrics/PerceivedComplexity:
14+
Max: 10
15+
16+
Metrics/LineLength:
17+
Max: 120

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development do
6+
gem 'addressable', '2.4.0'
67
gem 'rake', '10.4.2'
8+
gem 'rubocop', '0.41.2'
79
gem 'test-unit', '3.1.5'
8-
gem 'addressable', '2.4.0'
910
gem 'webmock', '2.1.0'
1011
end

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ puts response
144144

145145
Utils houses generic helpers useful in a Button Integration.
146146

147-
### #is_webhook_authentic
147+
### #webhook_authentic?
148148

149149
Used to verify that requests sent to a webhook endpoint are from Button and that their payload can be trusted. Returns `true` if a webhook request body matches the sent signature and `false` otherwise. See [Webhook Security](https://www.usebutton.com/developers/webhooks/#security) for more details.
150150

151151
```ruby
152152
require 'button'
153153

154-
Button::Utils::is_webhook_authentic(
154+
Button::Utils::webhook_authentic?(
155155
ENV['WEBHOOK_SECRET'],
156156
request_body,
157157
request_headers.fetch('X-Button-Signature')
@@ -163,4 +163,5 @@ Button::Utils::is_webhook_authentic(
163163
* Building the gem: `gem build button.gemspec`
164164
* Installing locally: `gem install ./button-X.Y.Z.gem`
165165
* Installing development dependencies: `bundle install`
166+
* Running linter: `rake lint`
166167
* Running tests: `bundle exec rake`

Rakefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require 'rake/testtask'
2+
require 'rubocop/rake_task'
23

3-
task(default: [:test])
4+
task default: [:test]
5+
task test: [:unit, :lint]
46

5-
Rake::TestTask.new do |t|
7+
Rake::TestTask.new(:unit) do |t|
68
t.pattern = './test/**/*_test.rb'
79
end
10+
11+
RuboCop::RakeTask.new(:lint)

bin/console

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env ruby
22

3+
lib = File.expand_path('../../lib', __FILE__)
4+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5+
36
require 'irb'
47
require "#{File.expand_path('../../lib', __FILE__)}/button.rb"
58

lib/button/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'button/errors'
33

44
NO_API_KEY_MESSAGE = 'Must provide a Button API key. Find yours at '\
5-
'https://app.usebutton.com/settings/organization'
5+
'https://app.usebutton.com/settings/organization'.freeze
66

77
module Button
88
# Client is the top-level interface for the Button API. It exposes one
@@ -29,7 +29,7 @@ def initialize(api_key, config = {})
2929
def merge_defaults(config)
3030
secure = config.fetch(:secure, true)
3131

32-
return {
32+
{
3333
secure: secure,
3434
timeout: config.fetch(:timeout, nil),
3535
hostname: config.fetch(:hostname, 'api.usebutton.com'),

lib/button/resources/resource.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ def initialize(api_key, config)
3232
@http = Net::HTTP.new(config[:hostname], config[:port])
3333
@http.use_ssl = config[:secure]
3434

35-
if not config[:timeout].nil?
36-
@http.read_timeout = config[:timeout]
37-
end
35+
return if config[:timeout].nil?
36+
@http.read_timeout = config[:timeout]
3837
end
3938

4039
def timeout

lib/button/utils.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
require 'openssl'
22

33
module Button
4+
# Generally handy functions for various aspects of a Button integration
5+
#
46
module Utils
57
# Used to verify that requests sent to a webhook endpoint are from Button
68
# and that their payload can be trusted. Returns true if a webhook request
79
# body matches the sent signature and false otherwise.
810
#
9-
def is_webhook_authentic(webhook_secret, request_body, sent_signature)
11+
def webhook_authentic?(webhook_secret, request_body, sent_signature)
1012
computed_signature = OpenSSL::HMAC.hexdigest(
1113
OpenSSL::Digest.new('sha256'),
1214
webhook_secret,
@@ -16,6 +18,6 @@ def is_webhook_authentic(webhook_secret, request_body, sent_signature)
1618
sent_signature == computed_signature
1719
end
1820

19-
module_function :is_webhook_authentic
21+
module_function :webhook_authentic?
2022
end
2123
end

test/button/client_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ def test_exposes_orders
2222

2323
def test_sets_default_config_parameters
2424
client = Button::Client.new('sk-XXX')
25-
assert_equal(client.orders.config, {
25+
assert_equal(
26+
client.orders.config,
2627
hostname: 'api.usebutton.com',
2728
port: 443,
2829
secure: true,
2930
timeout: nil
30-
})
31+
)
3132
end
3233

3334
def test_allows_config_overrides

test/button/resources/orders_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
class OrdersTest < Test::Unit::TestCase
44
def setup
5-
@orders = Button::Orders.new('sk-XXX', {
5+
@orders = Button::Orders.new(
6+
'sk-XXX',
67
secure: true,
78
timeout: nil,
89
hostname: 'api.usebutton.com',
910
port: 443
10-
})
11+
)
1112

1213
@headers = {
1314
'Authorization' => 'Basic c2stWFhYOg==',

0 commit comments

Comments
 (0)