Sha256: 5c3d53db0afcb04e71ee79a76a4c238c4f4b02a2a842619060863f0a4db297a2

Contents?: true

Size: 953 Bytes

Versions: 8

Compression:

Stored size: 953 Bytes

Contents

module Lol
  class InvalidAPIResponse < StandardError
    attr_reader :raw

    def initialize url, response
      @raw = extract_raw_response_info response
      super "#{raw[:status]} calling #{url}"
    end

    private

    def extract_raw_response_info response
      {
        headers: extract_raw_headers(response),
        body:    extract_raw_body(response),
        status:  extract_raw_status(response)
      }
    end

    def extract_raw_headers response
      response.respond_to?(:headers) && response.headers || {}
    end

    def extract_raw_body response
      if response.respond_to?(:parsed_response)
        response.parsed_response
      elsif response.respond_to?(:body)
        response.body
      else
        response
      end
    end

    def extract_raw_status response
      if response.is_a?(Hash) && response['status']
        response['status']['message']
      else
        "Unknown Error"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ruby-lol-0.11.3 lib/lol/invalid_api_response.rb
ruby-lol-0.11.2 lib/lol/invalid_api_response.rb
ruby-lol-0.11.1 lib/lol/invalid_api_response.rb
ruby-lol-0.11.0 lib/lol/invalid_api_response.rb
ruby-lol-0.10.0 lib/lol/invalid_api_response.rb
ruby-lol-0.9.19.1 lib/lol/invalid_api_response.rb
ruby-lol-0.9.19 lib/lol/invalid_api_response.rb
ruby-lol-0.9.18 lib/lol/invalid_api_response.rb