Skip to content

Commit 0c8b5df

Browse files
Refactor (#186)
* Eliminate test warnings Each of these changes were causing a longer version of the below warning > WARNING: Using the `raise_error` matcher without providing a specific error > or message risks false positives There are definitely better refactors for each of these, but I'm optimizing for cleaning up the test output, not for improving the code interface or possibly introducing breaking changes * Ecosystem Updates - Add support for Ruby 3.2 and 3.3 - Require Faraday 2.0+ * Add Verbose option This change turns off request and response logging by default and allows users to turn that back on by setting a new `verbose` option to true. * Clean up GitHub Actions script - only build on some pushes, but all pull requests - support manual builds via workflow_dispatch - change publish to be triggered by a GitHub release action - update to latest actions/checkout script * BREAKING: remove `compress` option This change removes the `compress` option from the client entirely. We use the default faraday adapter, which is `net/http` from stdlib. If you read [the documentation for that module][net-http-docs], you'll come across this: > `Net::HTTP` automatically adds Accept-Encoding for compression of > response bodies and automatically decompresses gzip and deflate > responses unless a Range header was sent. In other words, by default, ruby's net/http module will request gzipped content and automatically uncompress it if that's what the response contains. If instead one sets the `Accept-Encoding` header on a request that's made with `net/http`, that default behavior is ignored and we're left to deal with unpacking hte response ourselves as was done in the `FaradayMiddleware` class. In other words, whether the `compress` option is used or not, the behavior has been that we're asking for compressed responses and the option is effectively ignored. I went back to ruby 1.9.2 and the same behavior existed there. Given that, I'm confident this option has always been either ignored or redundant. [net-http-docs]: https://ruby-doc.org/stdlib-3.0.0/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Compression * Remove highline dependency Highline is only used within this cli provider, and only to capture and do some minor formatting on the user input. This changeset removes highline entirely and replaces it with equivalent ruby * remove broken Code Climate badge * Update reso_faraday_middleware.rb The new Faraday::Middleware class does not have a `body` method from the response and requires us to retrieve it from the `env` variable. --------- Co-authored-by: Cody Gustafson <cgustafson@fbsdata.com>
1 parent 5d7bb8b commit 0c8b5df

20 files changed

+64
-123
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
name: CI
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches: 'master'
6+
pull_request:
7+
workflow_dispatch:
8+
release:
9+
types: published
10+
411

512
jobs:
613
build:
@@ -9,11 +16,11 @@ jobs:
916
runs-on: ubuntu-latest
1017
strategy:
1118
matrix:
12-
ruby: [ '2.7', '3.0', '3.1' ]
19+
ruby: [ '3.1', '3.2', '3.3' ]
1320

1421
steps:
1522
- name: repo checkout
16-
uses: actions/checkout@v2
23+
uses: actions/checkout@v4
1724

1825
- name: Set up Ruby ${{ matrix.ruby }}
1926
# https://github.com/ruby/setup-ruby
@@ -22,7 +29,7 @@ jobs:
2229
ruby-version: ${{ matrix.ruby }}
2330

2431
- name: Set up Bundler
25-
run: gem install bundler -v 2.1.4
32+
run: gem install bundler
2633

2734

2835
- name: bundle install
@@ -36,19 +43,19 @@ jobs:
3643
runs-on: ubuntu-latest
3744

3845
# only run if we pushed a tag
39-
if: startsWith(github.ref, 'refs/tags/v')
46+
if: github.event_name == 'release'
4047

4148
# require that the build matrix passed
4249
needs: build
4350

4451
steps:
4552
- name: repo checkout
46-
uses: actions/checkout@v2
53+
uses: actions/checkout@v4
4754

4855
- name: Set up Ruby
4956
uses: ruby/setup-ruby@v1
5057
with:
51-
ruby-version: '2.7'
58+
ruby-version: '3.3'
5259

5360
- name: bundle install
5461
run: |

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.2
1+
3.2.3

CHANGELOG

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
v2.0.0
2+
- Drop support for ruby 2.7
3+
- Add support for ruby 3.2+
4+
- Require minimum version of faraday 2.0
5+
- Disabled request/response logging by default. Old behavior can be restored
6+
by setting a new `verbose` configuration option to true
7+
- BREAKING: remove `compress` from configuration. gzip compression is now
8+
always enabled
9+
110
v1.6.3
211
- Add support for sending end user ip address in request headers
312

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Spark API
22
=====================
3-
![CI](https://github.com/sparkapi/spark_api/workflows/CI/badge.svg) ![Code Climate](https://codeclimate.com/badge.png)
3+
![CI](https://github.com/sparkapi/spark_api/workflows/CI/badge.svg)
44

55
A Ruby wrapper for the Spark REST API. Loosely based on ActiveResource to provide models to interact with remote services.
66

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.6.3
1+
2.0.0

lib/spark_api/authentication/oauth2_impl/cli_provider.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "highline"
2-
31
module SparkApi
42
module Authentication
53
module OAuth2Impl
@@ -18,9 +16,11 @@ def initialize(credentials)
1816
def redirect(url)
1917
puts "Missing OAuth2 session, redirecting..."
2018
puts "Please visit #{url}, login as a user, and paste the authorization code here:"
21-
self.code = HighLine.ask("Authorization code?") do |q|
22-
q.whitespace = :strip_and_collapse
23-
q.validate = /^\w+$/
19+
puts "Authorization code?"
20+
raw_code = gets.strip
21+
22+
unless raw_code.match?(/^\w+$/)
23+
raise "Invalid authorization code. Please try again."
2424
end
2525
end
2626

lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module OAuth2Impl
66

77
#==OAuth2 Faraday response middleware
88
# HTTP Response after filter to package oauth2 responses and bubble up basic api errors.
9-
class FaradayMiddleware < Faraday::Response::Middleware
9+
class FaradayMiddleware < Faraday::Middleware
1010

1111
def initialize(app)
1212
super(app)
@@ -42,7 +42,7 @@ def on_complete(env)
4242

4343
#==OAuth2 Faraday response middleware
4444
# HTTP Response after filter to package oauth2 responses and bubble up basic api errors.
45-
class SparkbarFaradayMiddleware < Faraday::Response::Middleware
45+
class SparkbarFaradayMiddleware < Faraday::Middleware
4646

4747
def initialize(app)
4848
super(app)

lib/spark_api/configuration.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Configuration
99
end
1010

1111
# valid configuration options
12-
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
13-
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14-
:auth_endpoint, :callback, :compress, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address].freeze
12+
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
13+
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14+
:auth_endpoint, :callback, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address, :verbose].freeze
1515
OAUTH2_KEYS = [:authorization_uri, :access_uri, :client_id, :client_secret,
1616
# Requirements for authorization_code grant type
1717
:redirect_uri,
@@ -41,12 +41,12 @@ module Configuration
4141
DEFAULT_SSL = true
4242
DEFAULT_SSL_VERIFY = true
4343
DEFAULT_OAUTH2 = nil
44-
DEFAULT_COMPRESS = false
4544
DEFAULT_TIMEOUT = 5 # seconds
4645
DEFAULT_MIDDLEWARE = 'spark_api'
4746
DEFAULT_DICTIONARY_VERSION = nil
4847
DEFAULT_REQUEST_ID_CHAIN = nil
4948
DEFAULT_USER_IP_ADDRESS = nil
49+
DEFAULT_VERBOSE = false
5050

5151
X_SPARK_API_USER_AGENT = "X-SparkApi-User-Agent"
5252
X_USER_IP_ADDRESS = "X-User-IP-Address"
@@ -79,12 +79,12 @@ def reset_configuration
7979
self.ssl = DEFAULT_SSL
8080
self.ssl_verify = DEFAULT_SSL_VERIFY
8181
self.version = DEFAULT_VERSION
82-
self.compress = DEFAULT_COMPRESS
8382
self.timeout = DEFAULT_TIMEOUT
8483
self.middleware = DEFAULT_MIDDLEWARE
8584
self.dictionary_version = DEFAULT_DICTIONARY_VERSION
8685
self.request_id_chain = DEFAULT_REQUEST_ID_CHAIN
8786
self.user_ip_address = DEFAULT_USER_IP_ADDRESS
87+
self.verbose = DEFAULT_VERBOSE
8888
self
8989
end
9090
end

lib/spark_api/configuration/yaml.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def self.config_keys()
6060
end
6161

6262
def self.exists?(name)
63-
File.exists? "#{config_path}/#{name}.yml"
63+
File.exist? "#{config_path}/#{name}.yml"
6464
end
6565

6666
def self.build(name)

lib/spark_api/connection.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Connection
1010
HTTP_SCHEME = 'http:'
1111
HTTPS_SCHEME = 'https:'
1212
ACCEPT_ENCODING = 'Accept-Encoding'
13-
COMPRESS_ACCEPT_ENCODING = 'gzip, deflate'
1413
X_REQUEST_ID_CHAIN = 'X-Request-Id-Chain'
1514
MIME_JSON = 'application/json'
1615
MIME_RESO = 'application/json, application/xml'
@@ -27,10 +26,6 @@ def connection(force_ssl = false)
2726
opts[:url] = @endpoint.sub REG_HTTPS, HTTP_SCHEME
2827
end
2928

30-
if self.compress
31-
opts[:headers][ACCEPT_ENCODING] = COMPRESS_ACCEPT_ENCODING
32-
end
33-
3429
if request_id_chain
3530
opts[:headers][X_REQUEST_ID_CHAIN] = request_id_chain
3631
end
@@ -40,7 +35,9 @@ def connection(force_ssl = false)
4035
conn.options[:timeout] = self.timeout
4136
conn.adapter Faraday.default_adapter
4237
end
43-
SparkApi.logger.debug { "Connection: #{conn.inspect}" }
38+
if self.verbose
39+
SparkApi.logger.debug { "Connection: #{conn.inspect}" }
40+
end
4441
conn
4542
end
4643

0 commit comments

Comments
 (0)