Sha256: 783657dbf7556401a813b084f831887608a7dbdcdaf976b0533311066ee15b25

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module PartyBus
  class Client
    def self.post(
      body:,
      path:,
      timestamp: Time.now
    )
      return { success: true } unless PartyBus.configuration.enabled

      response = HTTParty.post(
        "#{PartyBus.configuration.api_url}#{path}",
        body: body.to_json,
        default_timeout: 30,
        headers: headers(timestamp, body)
      )

      parse_response(response)
    end

    def self.parse_response(response)
      if response.success?
        {
          success: true,
          serialized_result: response.parsed_response
        }
      else
        {
          success: false,
          errors: [response.parsed_response]
        }
      end
    end

    def self.headers(timestamp, body)
      {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Party-Signature': {
          t: timestamp.to_i,
          v1: OpenSSL::HMAC.hexdigest(
            "SHA256",
            PartyBus.configuration.secret,
            "#{timestamp.to_i}.#{body.to_json}"
          )
        }.map { |key| key.join('=') }.join(',')
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
party_bus-0.4.0 lib/party_bus/client.rb
party_bus-0.3.0 lib/party_bus/client.rb
party_bus-0.2.0 lib/party_bus/client.rb