Sha256: b1cb8dec7f0b9368b949cedbcbafc7b9774d537cbcb3c2b22fc9c8c9cb7d871d

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module TryApi
  class ExampleResponse < TryApi::Base
    typesafe_accessor :code, Integer
    typesafe_accessor :response, String
    typesafe_accessor :type, String

    class << self
      def parse(hash:, project:)
        return nil if hash.blank?
        instance = self.new
        instance.code = hash[:code]
        instance.project = project
        instance.response = hash[:response]
        instance.type = hash[:type]
        instance
      end

      def descriptions
        {
            200 => :success,
            201 => :created,
            204 => :no_content,
            304 => :not_modified,
            400 => :bad_request,
            401 => :unauthorized,
            403 => :forbidden,
            404 => :not_found,
            408 => :request_timeout,
            422 => :unprocessable_entity,
            500 => :internal_server_error,
            502 => :bad_gateway,
            503 => :service_unavailable,
            504 => :gateway_timeout
        }
      end
    end

    def description
      self.class.descriptions[self.code].to_s.humanize
    end

    def color
      case self.code
        when 200
          'success'
        when 200...300
          'info'
        when 300...400
          'warning'
        when 400...500
          'warning'
        when 500
          'danger'
        else
          'default'
      end
    end

    def to_json
      super.merge color: color, description: description, isCollapsed: true
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
try_api-0.1.3 app/models/try_api/example_response.rb
try_api-0.1.2 app/models/try_api/example_response.rb
try_api-0.1.1 app/models/try_api/example_response.rb
try_api-0.1.0 app/models/try_api/example_response.rb