Sha256: 761ff262cbaaaad0a333dfa8d20f09172d9aeed2fd747f07df9c15151c6172d0

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

require 'json'

module ConnectClient
  class EventPushResponse
    attr_reader :data
    attr_reader :http_status_code

    def initialize(code, content_type, response_body, events_pushed)
      @http_status_code = code.to_s

      if content_type.include? 'application/json'
        body = response_body
        body = '{}' if response_body.to_s.empty?
        parse_body(body, events_pushed)
      else
        @data = response_body
      end
    end

    def success?
      @http_status_code.start_with? '2'
    end

    def to_s
      %{
        Status: #{@http_status_code}
        Successful: #{success?}
        Data: #{data}
      }
    end

    private

    def parse_body(body, events_pushed)
      @data = JSON.parse(body, :symbolize_names => true)

      if (events_pushed.is_a?(Hash) && @data.is_a?(Hash))
        @data.merge!(events_pushed) do |collection_name, responses, events|
          responses.zip(events).map do |response, event|
            response[:event] = event.data 
            response
          end
        end
      else
        @data[:event] = events_pushed.data
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
connect_client-0.3.1 lib/connect_client/event_push_response.rb
connect_client-0.1.1 lib/connect_client/event_push_response.rb
connect_client-0.1.0 lib/connect_client/event_push_response.rb