Sha256: 8c22bfd0ecbf2641eac4525ba05b6a26b988381de68c195cfc79f4337dc19264

Contents?: true

Size: 867 Bytes

Versions: 4

Compression:

Stored size: 867 Bytes

Contents

require 'faraday'

# @private
module Faraday
  # @private
  class Response::RaiseHttp4xx < Response::Middleware
    def self.register_on_complete(env)
      env[:response].on_complete do |response|
        case response[:status].to_i
        when 400
          raise Instagram::BadRequest, error_message(response)
        when 404
          raise Instagram::NotFound, error_message(response)
        end
      end
    end

    def initialize(app)
      super
      @parser = nil
    end

    private

    def self.error_message(response)
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
    end

    def self.error_body(body)
      if body.nil?
        nil
      elsif body['meta'] and not body['meta']['error_message'].blank?
        ": #{body['meta']['error_message']}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
instagram-0.6.2 lib/faraday/raise_http_4xx.rb
localist-instagvram-0.6.2 lib/faraday/raise_http_4xx.rb
instagram-0.6.1 lib/faraday/raise_http_4xx.rb
instagram-0.6 lib/faraday/raise_http_4xx.rb