Skip to content
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
66 changes: 54 additions & 12 deletions lib/faraday/raise_http_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
module FaradayMiddleware
# @private
class RaiseHttpException < Faraday::Middleware
TOKEN_EXPIRED_ERROR_CODES = {
'190' => 'Access token has expired',
'460' => 'Password Changed',
'463' => 'Login status or access token has expired, been revoked, or is otherwise invalid',
'467' => 'Access token has expired, been revoked, or is otherwise invalid',
'492' => 'Invalid Session'
}

def call(env)
@app.call(env).on_complete do |response|
case response[:status].to_i
when 400
raise Instagram::BadRequest, error_message_400(response)
if access_token_expired?(response)
raise Instagram::AccessTokenExpired, error_message_400(response, token_expired_message(response))
else
raise Instagram::BadRequest, error_message_400(response, 'Invalid OAuth access token')
end
when 404
raise Instagram::NotFound, error_message_400(response)
when 429
Expand All @@ -32,28 +44,58 @@ def initialize(app)

private

def error_message_400(response)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
def token_expired_message(response)
TOKEN_EXPIRED_ERROR_CODES[error_code(response)]
end

def access_token_expired?(response)
body = error_body(response[:body])

if body.present?
code = error_code(response)
if code.present?
return TOKEN_EXPIRED_ERROR_CODES[code.to_s].present?
end
end

false
end

def error_message_400(response, body=nil)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', error_body_message(response), body].compact.join(' ')}"
end

def error_message_500(response, body=nil)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', error_body_message(response), body].compact.join(' ')}"
end

def error_body(body)
# body gets passed as a string, not sure if it is passed as something else from other spots?
if not body.nil? and not body.empty? and body.kind_of?(String)
# removed multi_json thanks to wesnolte's commit
body = ::JSON.parse(body)
body = ::JSON.parse(body) rescue body
end

body
end

def error_code(response)
body = error_body(response[:body])
body.dig('error', 'code')
rescue
response.dig(:status)
end

def error_body_message(response)
body = error_body(response['body'])

if body.nil?
nil
elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
": #{body['meta']['error_message']}"
elsif body['error_message'] and not body['error_message'].empty?
": #{body['error_type']}: #{body['error_message']}"
elsif body.is_a?(Hash) && (msg = body.dig('error', 'message')).present?
": #{msg}"
else
": #{body.to_s}"
end
end

def error_message_500(response, body=nil)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
end
end
end
7 changes: 0 additions & 7 deletions lib/instagram/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ class Client < API

include Instagram::Client::Users
include Instagram::Client::Media
include Instagram::Client::Locations
include Instagram::Client::Geographies
include Instagram::Client::Tags
include Instagram::Client::Comments
include Instagram::Client::Likes
include Instagram::Client::Subscriptions
include Instagram::Client::Embedding
end
end
62 changes: 0 additions & 62 deletions lib/instagram/client/comments.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/instagram/client/embedding.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/instagram/client/geographies.rb

This file was deleted.

58 changes: 0 additions & 58 deletions lib/instagram/client/likes.rb

This file was deleted.

75 changes: 0 additions & 75 deletions lib/instagram/client/locations.rb

This file was deleted.

Loading