Skip to content

Commit c3c7204

Browse files
authored
Merge pull request #171 from bigcommerce/RUBY-18
RUBY-18: Fix various Rubocop warnings and declare dotenv as a dev dependency
2 parents 4fd92e8 + 8257485 commit c3c7204

11 files changed

+94
-58
lines changed

.rubocop.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,7 @@ Naming/VariableNumber:
3333
########################################################################################################################
3434
# TO FIX
3535
########################################################################################################################
36-
Layout/EmptyLineAfterGuardClause:
37-
Enabled: false
38-
39-
Lint/MissingSuper:
40-
Enabled: false
41-
42-
Performance/MethodObjectAsBlock:
43-
Exclude:
44-
- lib/bigcommerce.rb
45-
46-
Lint/NonDeterministicRequireOrder:
47-
Exclude:
48-
- lib/bigcommerce.rb
4936

5037
# we should deprecate Ruby < 2.7 support ASAP
5138
Gemspec/RequiredRubyVersion:
5239
Enabled: false
53-
54-
Style/ExplicitBlockArgument:
55-
Exclude:
56-
- lib/bigcommerce.rb

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Next Release
22
Your contribution here.
33

4+
* [#170](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/170): Fix various Rubocop warnings and declare dotenv as a dev dependency - [@splittingred](https://github.com/splittingred)
45
* [#169](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/169): Add frozen_string_literal magic comment to all files - [@splittingred](https://github.com/splittingred)
56
* [#168](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/168): Move to CircleCI off of TravisCI - [@splittingred](https://github.com/splittingred)
67
* [#168](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/168): Explicitly declare development dependencies in gemspec - [@splittingred](https://github.com/splittingred)

bigcommerce.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
2121

2222
s.add_development_dependency 'bundler'
2323
s.add_development_dependency 'bundler-audit', '~> 0.6'
24+
s.add_development_dependency 'dotenv', '>= 2.0'
2425
s.add_development_dependency 'pry', '>= 0.12'
2526
s.add_development_dependency 'rake', '>= 12.0'
2627
s.add_development_dependency 'rspec', '~> 3.8'

bin/console

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'bundler/setup'
4+
require 'bigcommerce'
5+
require 'pry'
6+
Pry.start

lib/bigcommerce.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
require 'hashie'
44
require 'faraday_middleware'
5-
require 'bigcommerce/version'
6-
require 'bigcommerce/config'
7-
require 'bigcommerce/connection'
8-
require 'bigcommerce/middleware/auth'
9-
require 'bigcommerce/middleware/http_exception'
10-
require 'bigcommerce/resources/resource'
5+
require_relative 'bigcommerce/version'
6+
require_relative 'bigcommerce/config'
7+
require_relative 'bigcommerce/connection'
8+
require_relative 'bigcommerce/path_builder'
9+
require_relative 'bigcommerce/middleware/auth'
10+
require_relative 'bigcommerce/middleware/http_exception'
11+
require_relative 'bigcommerce/resources/resource'
1112

1213
module Bigcommerce
1314
resources = File.join(File.dirname(__FILE__), 'bigcommerce', 'resources', '**', '*.rb')
14-
Dir.glob(resources, &method(:require))
15+
Dir.glob(resources).sort.each { |r| require r }
1516

1617
class << self
17-
attr_reader :api, :config
18+
# @!attribute [r] api
19+
# @return [::Faraday::Connection]
20+
attr_reader :api
21+
# @!attribute [r] config
22+
# @return [::Bigcommerce::Config]
23+
attr_reader :config
1824

19-
def configure
20-
@config = Bigcommerce::Config.new.tap { |h| yield(h) }
25+
def configure(&block)
26+
@config = Bigcommerce::Config.new.tap(&block)
2127
@api = Bigcommerce::Connection.build(@config)
2228
end
2329
end

lib/bigcommerce/exception.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class HttpError < StandardError
66

77
def initialize(headers)
88
@response_headers = headers
9+
super()
910
end
1011
end
1112

lib/bigcommerce/middleware/auth.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
module Bigcommerce
44
module Middleware
55
class Auth < Faraday::Middleware
6+
X_AUTH_CLIENT_HEADER = 'X-Auth-Client'
7+
X_AUTH_TOKEN_HEADER = 'X-Auth-Token'
8+
69
def initialize(app, options = {})
710
@app = app
811
@options = options
12+
super(app)
913
end
1014

1115
def call(env)
12-
env[:request_headers]['X-Auth-Client'] = @options[:client_id]
13-
env[:request_headers]['X-Auth-Token'] = @options[:access_token]
16+
env[:request_headers][X_AUTH_CLIENT_HEADER] = @options[:client_id]
17+
env[:request_headers][X_AUTH_TOKEN_HEADER] = @options[:access_token]
1418
@app.call env
1519
end
1620
end

lib/bigcommerce/path_builder.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module Bigcommerce
4+
class PathBuilder
5+
# @!attribute [r] uri
6+
# @return [String] The URI of the path to build
7+
attr_reader :uri
8+
9+
##
10+
# @param [String] uri
11+
#
12+
def initialize(uri)
13+
@uri = uri
14+
end
15+
16+
##
17+
# This takes the @uri and inserts the keys to form a path.
18+
# To start we make sure that for nil/numeric values, we wrap those into an
19+
# array. We then scan the string for %d and %s to find the number of times
20+
# we possibly need to insert keys into the URI. Next, we check the size of
21+
# the keys array, if the keys size is less than the number of possible keys
22+
# in the URI, we will remove the trailing %d or %s, then remove the
23+
# trailing /. We then pass the keys into the uri to form the path.
24+
# ex. foo/%d/bar/%d => foo/1/bar/2
25+
#
26+
# @param [Array] keys
27+
# @return [String]
28+
#
29+
def build(keys = [])
30+
keys = [] if keys.nil?
31+
keys = [keys] if keys.is_a? Numeric
32+
ids = uri.scan('%d').count + uri.scan('%s').count
33+
str = ids > keys.size ? uri.chomp('%d').chomp('%s').chomp('/') : uri
34+
(str % keys).chomp('/')
35+
end
36+
37+
##
38+
# @return [String]
39+
#
40+
def to_s
41+
@uri
42+
end
43+
end
44+
end

lib/bigcommerce/request.rb

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,15 @@
33
require 'json'
44

55
module Bigcommerce
6-
class PathBuilder
7-
attr_reader :uri
8-
9-
def initialize(uri)
10-
@uri = uri
11-
end
12-
13-
# This takes the @uri and inserts the keys to form a path.
14-
# To start we make sure that for nil/numeric values, we wrap those into an
15-
# array. We then scan the string for %d and %s to find the number of times
16-
# we possibly need to insert keys into the URI. Next, we check the size of
17-
# the keys array, if the keys size is less than the number of possible keys
18-
# in the URI, we will remove the trailing %d or %s, then remove the
19-
# trailing /. We then pass the keys into the uri to form the path.
20-
# ex. foo/%d/bar/%d => foo/1/bar/2
21-
def build(keys = [])
22-
keys = [] if keys.nil?
23-
keys = [keys] if keys.is_a? Numeric
24-
ids = uri.scan('%d').count + uri.scan('%s').count
25-
str = ids > keys.size ? uri.chomp('%d').chomp('%s').chomp('/') : uri
26-
(str % keys).chomp('/')
27-
end
28-
29-
def to_s
30-
@uri
31-
end
32-
end
33-
346
class Request < Module
357
def initialize(uri)
368
@uri = uri
9+
super()
3710
end
3811

3912
def included(base)
4013
base.extend ClassMethods
41-
path_builder = PathBuilder.new @uri
14+
path_builder = ::Bigcommerce::PathBuilder.new(@uri)
4215
base.define_singleton_method :path do
4316
path_builder
4417
end
@@ -81,8 +54,13 @@ def build_response_object(response)
8154
end
8255
end
8356

57+
##
58+
# @return [Hash]
59+
# @return [Array]
60+
#
8461
def parse(json)
8562
return [] if json.empty?
63+
8664
JSON.parse(json, symbolize_names: true)
8765
end
8866
end

lib/bigcommerce/resource_actions.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Bigcommerce
44
class ResourceActions < Module
5+
# @!attribute [r] options
6+
# @return [Hash] Options passed to this module
57
attr_reader :options
68

79
def initialize(options = {})
@@ -11,6 +13,7 @@ def initialize(options = {})
1113
mod.options
1214
end
1315
end
16+
super()
1417
end
1518

1619
def included(base)
@@ -28,6 +31,7 @@ def all(params = {})
2831

2932
def find(resource_id, params = {})
3033
raise ArgumentError if resource_id.nil?
34+
3135
get path.build(resource_id), params
3236
end
3337

@@ -37,11 +41,13 @@ def create(params = {})
3741

3842
def update(resource_id, params = {})
3943
raise ArgumentError if resource_id.nil?
44+
4045
put path.build(resource_id), params
4146
end
4247

4348
def destroy(resource_id, params = {})
4449
raise ArgumentError if resource_id.nil?
50+
4551
delete path.build(resource_id), params
4652
end
4753

lib/bigcommerce/subresource_actions.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,37 @@ def included(base)
1313
module ClassMethods
1414
def all(parent_id, params = {})
1515
raise ArgumentError if parent_id.nil?
16+
1617
get path.build(parent_id), params
1718
end
1819

1920
def find(parent_id, resource_id, params = {})
2021
raise ArgumentError if [parent_id, resource_id].any?(&:nil?)
22+
2123
get path.build([parent_id, resource_id]), params
2224
end
2325

2426
def create(parent_id, params = {})
2527
raise ArgumentError if parent_id.nil?
28+
2629
post path.build(parent_id), params
2730
end
2831

2932
def update(parent_id, resource_id, params = {})
3033
raise ArgumentError if [parent_id, resource_id].any?(&:nil?)
34+
3135
put path.build([parent_id, resource_id]), params
3236
end
3337

3438
def destroy(parent_id, resource_id, params = {})
3539
raise ArgumentError if [parent_id, resource_id].any?(&:nil?)
40+
3641
delete path.build([parent_id, resource_id]), params
3742
end
3843

3944
def destroy_all(parent_id, params = {})
4045
raise ArgumentError if parent_id.nil?
46+
4147
delete path.build(parent_id), params
4248
end
4349
end

0 commit comments

Comments
 (0)