Sha256: d8bf9d9a060732e18d1e2de4f4583fb72b4394c1a718db8352dbd809639851bf

Contents?: true

Size: 628 Bytes

Versions: 29

Compression:

Stored size: 628 Bytes

Contents

class <%= class_name %>::Responder
  Result = Struct.new(:success?, :response, :payload)
  attr_reader :response, :payload

  delegate :body, :status, to: :response

  def initialize(response, **args)
    @response = response
    @payload = args
  end

  def result
    error = !/2\d{2}/.match?(status.to_s)

    Result.new(!error, parsed_body, { status: }.merge(payload))
  rescue JSON::ParserError => e
    Result.new(false, e)
  end

  private

  def parsed_body
    return body if body.length.zero?

    case body
    when Hash then body
    else JSON.parse(body, symbolize_names: true, quirks_mode: true)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
kapellmeister-0.10.2 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.10.1 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.10.0 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.9.rc3 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.9.rc2 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.9.rc1 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.8.rc1 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.8.pre.rc1 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.8 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.7 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.6 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.5 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.4 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.3 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.2 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.1 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.9.0 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.8.0 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.7.3 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
kapellmeister-0.7.2 lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt