Skip to content

Commit

Permalink
Fixes ylecuyer#121. Bump faraday dep version from >= 0.9.2 to >= 2 an…
Browse files Browse the repository at this point in the history
…d migrate custom middleware
  • Loading branch information
nicduke38degrees committed Aug 2, 2023
1 parent 9d6fc82 commit f769b6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
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 #{environment.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
3 changes: 1 addition & 2 deletions survey-gizmo-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Gem::Specification.new do |gem|
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'
gem.add_dependency 'retriable', '>= 2.0'
gem.add_dependency 'i18n'
gem.add_dependency 'virtus', '>= 1.0.0'
Expand Down

0 comments on commit f769b6f

Please sign in to comment.