Sha256: b99ee7dc73a9e1dfeb23d3b1b57db88c3b62bd8ae07c2c2bbfd4d2476c597e58

Contents?: true

Size: 816 Bytes

Versions: 1

Compression:

Stored size: 816 Bytes

Contents

module InkFilePicker
  # Public: Simple decorator class for response.
  #
  # Decorates the response with hash like access to the
  # parsed body, which is expected to be JSON.
  class Response
    DELEGATE_TO_RESPONSE = %w[
      success? status headers body finished?
    ]

    DELEGATE_TO_PARSED_BODY = %w[
      to_hash each
    ]

    attr_reader :http_response

    delegate *DELEGATE_TO_RESPONSE, to: :http_response
    delegate *DELEGATE_TO_PARSED_BODY, to: :parsed_body


    def initialize(http_response)
      @http_response = http_response
    end

    def [](key)
      parsed_body[key.to_s]
    end

    def parsed_body
      @parsed_body ||= JSON.parse http_response.body
    end

    def valid?
      body == 'success' ||
      parsed_body
    rescue JSON::ParserError
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ink_file_picker-0.0.3 lib/ink_file_picker/response.rb