Sha256: 6e6f9dc4e148c6f3ab9c8b125d9c892d1e3854014d367b5c56a2de0cf8fe4c75

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

require "json"

module Freno
  class Client
    class Result

      # https://github.com/github/freno/blob/master/doc/http.md#status-codes
      FRENO_STATUS_CODE_MEANINGS = {
        200 => :ok,
        404 => :not_found,
        417 => :expectation_failed,
        429 => :too_many_requests,
        500 => :internal_server_error
      }.freeze

      # these are included to add resiliency to freno-client
      ADDITIONAL_STATUS_CODE_MEANINGS = {
        408 => :request_timeout
      }.freeze

      CODE_MEANINGS = FRENO_STATUS_CODE_MEANINGS.merge(ADDITIONAL_STATUS_CODE_MEANINGS).freeze
      MEANING_CODES = CODE_MEANINGS.invert.freeze

      def self.from_faraday_response(response)
        new(response.status, response.body)
      end

      def self.from_meaning(meaning)
        new(MEANING_CODES[meaning] || 0)
      end

      attr_reader :code, :meaning, :raw_body

      def initialize(code, body = nil)
        @code = code
        @meaning = CODE_MEANINGS[code] || :unknown
        @raw_body = body
      end

      def ok?
        meaning == :ok
      end

      def failed?
        !ok?
      end

      def unkown?
        meaning == :unkown
      end

      def body
        @body ||= JSON.parse(raw_body) if raw_body
      end

      def ==(other)
        return meaning == other if other.is_a? Symbol
        code == other
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
freno-client-0.8.1 lib/freno/client/result.rb
freno-client-0.8.0 lib/freno/client/result.rb
freno-client-0.7.0 lib/freno/client/result.rb
freno-client-0.6.0 lib/freno/client/result.rb
freno-client-0.4.0 lib/freno/client/result.rb