Sha256: 8d265c10f8138d8f7966a495a1454a969d25d50347551a7111c84834eadecf80

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module UniqueResponse
  require 'net/http'
  require 'net/https'
  require 'json'

  class Client
    attr_reader :successful, :data

    HTTP_HEADERS = {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'Accept-Charset' => 'utf-8',
        'User-Agent' => 'unique_response_ruby'
    }

    def initialize
      @account_id = ENV['UNIQUE_RESPONSE_ACCOUNT_ID']
      @auth_token = ENV['UNIQUE_RESPONSE_AUTH_TOKEN']
      if @account_id.nil? || @auth_token.nil?
        raise ArgumentError, 'Account SID and auth token are required'
      end
    end

    def save_response(response)
      @uri = URI(ENV['UNIQUE_RESPONSE_ENDPOINT'])
      @http_request = Net::HTTP::Post.new @uri.path, HTTP_HEADERS
      @http_request.body = response.to_h.to_json
      perform_request
    end

    private
      def perform_request
        http_response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
          http.request @http_request
        end

        case http_response
        when Net::HTTPSuccess, Net::HTTPRedirection
          body = JSON.parse http_response.body
          if @successful = body["successful"] || false
            @data = body["data"]
          else
            @data = "Error saving response: #{body["data"].to_json}"
          end
        else
          @data = "#{http_response.code}: #{http_response.body}"
          @successful = false
        end

        return @successful
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unique_response_ruby-0.1.0 lib/unique_response_ruby/client.rb