Sha256: 8f16e405ce16c7b2821720d9dd273bef0436613eddfab1009c3f82231d951f33

Contents?: true

Size: 727 Bytes

Versions: 3

Compression:

Stored size: 727 Bytes

Contents

module BeeiqAPI
  class Response
    attr_accessor :response

    def initialize(response)
      @response = response
    end

    def body
      if success?
        { success: true }
      else
        format_hash(JSON.parse(response.body, :quirks_mode => true))
      end
    end

    def raw_body
      response.body
    end

    def success?
      [200, 204].include? response.code
    end

    def code
      response.code
    end

    def headers
      response.headers
    end

    private

    def format_hash(h)
      if h.kind_of? Hash
        Hash[
          h.map do |k, v|
            [k.respond_to?(:to_sym) ? k.to_sym : k, format_hash(v)]
          end
        ]
      else
        h
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
beeiq_api-0.2.0 lib/beeiq_api/response.rb
beeiq_api-0.1.1 lib/beeiq_api/response.rb
beeiq_api-0.1.0 lib/beeiq_api/response.rb