Sha256: a3150af69a21e31470ee94ef088751be80d857e3d4d0ed4583627e6d357c70b9

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

require 'net/http'
require 'net/https'
require 'uri'
require 'json'
require 'rack/utils'

module Appsignal
  class Transmitter
    attr_reader :endpoint, :action, :api_key

    def initialize(endpoint, action, api_key, logger=nil)
      @endpoint = endpoint
      @action = action
      @api_key = api_key
    end

    def uri
      @uri ||= URI("#{@endpoint}/#{@action}").tap do |uri|
        uri.query = Rack::Utils.build_query({
          :api_key => api_key,
          :hostname => Socket.gethostname,
          :gem_version => Appsignal::VERSION
        })
      end
    end

    def transmit(payload)
      http_client.request(http_post(payload)).code
    end

    protected

    def ca_file_path
      File.expand_path(File.join(__FILE__, '../../../resources/cacert.pem'))
    end

    def http_post(payload)
      Net::HTTP::Post.new(uri.request_uri).tap do |post|
        post['Content-Type'] = 'application/json; charset=UTF-8'
        post.body = JSON.generate(payload)
      end
    end

    def http_client
      Net::HTTP.new(uri.host, uri.port).tap do |http|
        if uri.scheme == 'https'
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_PEER
          http.ca_file = ca_file_path
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
appsignal-0.5.5 lib/appsignal/transmitter.rb
appsignal-0.5.3 lib/appsignal/transmitter.rb
appsignal-0.5.1 lib/appsignal/transmitter.rb
appsignal-0.5.0 lib/appsignal/transmitter.rb
appsignal-0.4.7 lib/appsignal/transmitter.rb
appsignal-0.4.6 lib/appsignal/transmitter.rb
appsignal-0.4.5 lib/appsignal/transmitter.rb
appsignal-0.4.4 lib/appsignal/transmitter.rb
appsignal-0.4.3 lib/appsignal/transmitter.rb