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

Adds special handling and logger when the API response is malformed #1

Closed
wants to merge 6 commits into
base: hsume2/v5.14.0
from
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+84 −30
Diff settings

Always

Just for now

Copy path View file
@@ -1,6 +1,3 @@
Metrics/AbcSize:
Enabled: false

Metrics/BlockNesting:
Max: 2

@@ -10,7 +7,10 @@ Metrics/LineLength:

Metrics/MethodLength:
CountComments: false
Max: 15
Max: 10

Metrics/ModuleLength:
Max: 150 # TODO: Lower to 100

Metrics/ParameterLists:
Max: 4
@@ -20,38 +20,28 @@ Style/AccessModifierIndentation:
EnforcedStyle: outdent

Style/CollectionMethods:
Enabled: true
PreferredMethods:
map: 'collect'
map!: 'collect!'
reduce: 'inject'
find: 'detect'
find_all: 'select'

Style/Documentation:
Enabled: false

Style/DotPosition:
EnforcedStyle: trailing

Style/DoubleNegation:
Enabled: false

Style/EachWithObject:
Enabled: false
Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/Encoding:
Enabled: false
Style/TrailingComma:
EnforcedStyleForMultiline: 'comma'

Style/HashSyntax:
EnforcedStyle: hash_rockets

Style/Lambda:
Style/ParallelAssignment:
Enabled: false

Style/RaiseArgs:
EnforcedStyle: compact

Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/TrailingComma:
EnforcedStyleForMultiline: 'comma'
Copy path View file
@@ -1,22 +1,28 @@
bundler_args: --without development
env:
global:
- JRUBY_OPTS="$JRUBY_OPTS --debug"
cache: bundler

language: ruby

rvm:
- 1.8.7
- 1.9.3
- 2.0.0
- 2.1
- jruby-18mode
- 2.2
- jruby-19mode
- jruby-head
- rbx-2
- ruby-head

sudo: false

bundler_args: --without development --retry=3 --jobs=3

env:
global:
- JRUBY_OPTS="$JRUBY_OPTS --debug"

matrix:
allow_failures:
- rvm: jruby-head
- rvm: rbx-2
- rvm: ruby-head
fast_finish: true
sudo: false
Copy path View file
@@ -34,4 +34,4 @@ Yardstick::Rake::Verify.new do |verify|
verify.threshold = 59.5
end

task :default => [:spec, :rubocop, :verify_measurements]
task :default => [:spec, :verify_measurements]
Copy path View file
@@ -37,6 +37,13 @@ def perform
raise(Twitter::Error.new(error))
end
@rate_limit = Twitter::RateLimit.new(response.response_headers)
if response.body.nil?
@logger ||= begin
require 'logger'
::Logger.new(STDOUT)
end
@logger.info "[#{self.class.name}##{__method__}] #{@request_method}, #{@uri.to_s}, #{@options.inspect}, #{response.status}"
end
response.body
end
end
@@ -17,7 +17,15 @@ def parse(body)
end

def on_complete(response)
original_response_body = response.body
response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
if response.body.nil? || response.body.is_a?(Symbol)
@logger ||= begin
require 'logger'
::Logger.new(STDOUT)
end
@logger.info "[#{self.class.name}##{__method__}] #{original_response_body.inspect}, #{response.body.inspect}"
end
end

def unparsable_status_codes
Copy path View file
@@ -93,7 +93,17 @@ def perform_post_with_objects(path, options, klass)
# @param options [Hash]
# @param klass [Class]
def perform_request_with_objects(request_method, path, options, klass)
perform_request(request_method, path, options).collect do |element|
result = perform_request(request_method, path, options)

This comment has been minimized.

@hashwin

hashwin Jun 23, 2015

Collaborator

I think we should have additional logging inside this method as well, to get a clearer idea of what exactly the response was as soon as we got it without the twitter gem playing around with it.

unless result.respond_to?(:collect)
@logger ||= begin
require 'logger'
::Logger.new(STDOUT)
end
@logger.info "[#{self.class.name}##{__method__}] #{request_method}, #{path}, #{options.inspect}, #{result.inspect}"

return []
end
result.collect do |element|
klass.new(element)
end
end
@@ -50,6 +50,34 @@
expect(favorites.first.user.id).to eq(7_505_382)
end
end
context 'response body is nil' do
before do
stub_get('/1.1/favorites/list.json').to_return(:body => nil, :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.favorites
expect(a_get('/1.1/favorites/list.json')).to have_been_made
end
it 'returns an empty array' do
favorites = @client.favorites
expect(favorites).to be_an Array
expect(favorites).to be_empty
end
end
context 'response body is all whitespace' do
before do
stub_get('/1.1/favorites/list.json').to_return(:body => ' ', :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.favorites
expect(a_get('/1.1/favorites/list.json')).to have_been_made
end
it 'returns an empty array' do
favorites = @client.favorites
expect(favorites).to be_an Array
expect(favorites).to be_empty
end
end
end

describe '#unfavorite' do
@@ -33,5 +33,10 @@
allow(@client).to receive(:connection).and_raise(JSON::ParserError.new('unexpected token'))
expect { @request.perform }.to raise_error(Twitter::Error)
end
it 'raises error for invalid status code' do
stub_post('/1.1/statuses/update.json').with(:body => {:status => 'Update'}).to_return(:headers => {:content_type => 'application/json; charset=utf-8'}, :status => 303)
expect { @client.update('Update') }.to raise_error
expect(a_post('/1.1/statuses/update.json').with(:body => {:status => 'Update'})).to have_been_made
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.