Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump faraday dep version from >= 0.9.2 to >= 2 and migrate custom middleware. Fixes ylecuyer/survey-gizmo-ruby#121 #122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [2.4, 2.5, 2.6, 2.7, 3.0, head]
ruby: [2.6, 2.7, 3.0, head]

steps:
- name: Checkout repository
Expand Down
1 change: 0 additions & 1 deletion lib/survey-gizmo-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require 'awesome_print'
require 'digest/md5'
require 'faraday'
require 'faraday_middleware'
require 'logger'
require 'net/http'
require 'retriable'
Expand Down
30 changes: 17 additions & 13 deletions lib/survey_gizmo/faraday_middleware/parse_survey_gizmo.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'faraday_middleware/response_middleware'
require 'faraday'

module SurveyGizmo
class RateLimitExceededError < RuntimeError; end
class BadResponseError < RuntimeError; end

class ParseSurveyGizmo < FaradayMiddleware::ResponseMiddleware
class ParseSurveyGizmo < Faraday::Middleware
Faraday::Response.register_middleware(parse_survey_gizmo_data: self)

PAGINATION_FIELDS = [
Expand All @@ -22,20 +22,26 @@ class ParseSurveyGizmo < FaradayMiddleware::ResponseMiddleware
'modified_on'
]

def call(environment)
@app.call(environment).on_complete do |response|
fail RateLimitExceededError if response.status == 429
fail BadResponseError, "Bad response code #{response.status} in #{response.inspect}" unless response.status == 200
fail BadResponseError, response.body['message'] unless response.body['result_ok'] && response.body['result_ok'].to_s =~ /^true$/i
def on_complete(env)
fail RateLimitExceededError if env.status == 429
fail BadResponseError, "Bad response code #{env.status} in #{env.inspect}" unless env.status == 200
fail BadResponseError, env.body['message'] unless env.body['result_ok'] && env.body['result_ok'].to_s =~ /^true$/i

process_response(response)
end
process_response(env)
end

define_parser do |body|
private

def process_response(env)
env[:body] = parse(env[:body])
rescue Faraday::ParsingError => e
raise Faraday::ParsingError.new(e.wrapped_exception, env[:response])
end

def parse(body)
PAGINATION_FIELDS.each { |n| body[n] = body[n].to_i if body[n] }

next body unless body['data']
return body unless body['data']

# Handle really crappy [] notation in SG API, so far just in SurveyResponse
Array.wrap(body['data']).compact.each do |datum|
Expand Down Expand Up @@ -71,8 +77,6 @@ def call(environment)
body
end

private

def self.cleanup_attribute_name(attr)
attr.downcase.gsub(/[^[:alnum:]]+/, '_')
.gsub(/(url|variable|standard|shown)/, '')
Expand Down
5 changes: 2 additions & 3 deletions survey-gizmo-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ Gem::Specification.new do |gem|
gem.summary = 'Gem to use the SurveyGizmo.com REST API, v3+'
gem.homepage = 'http://github.com/jarthod/survey-gizmo-ruby'
gem.licenses = ['MIT']
gem.required_ruby_version = '>= 2.1'
gem.required_ruby_version = '>= 2.6'

gem.add_dependency 'activesupport', '>= 3.0'
gem.add_dependency 'addressable', '>= 2'
gem.add_dependency 'awesome_print', '>= 1'
gem.add_dependency 'faraday', '>= 0.9.1'
gem.add_dependency 'faraday_middleware'
gem.add_dependency 'faraday', '>= 2.0'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you remove ruby 2.4 and 2.5 from CI as faraday >2 requires at least tuby 2.6 and any way those version are already EOL

Copy link
Author

@nicduke38degrees nicduke38degrees Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 2.4 and 2.5
Also updated gemspec to set min ruby version to >=2.6
Awaiting maintainer to run the Test workflow

All <3 Ruby is EOL, would you like further adjustments to remove all 2.x versions?

gem.add_dependency 'retriable', '>= 2.0'
gem.add_dependency 'i18n'
gem.add_dependency 'virtus', '>= 1.0.0'
Expand Down