class Pilfer::Server

Attributes

token[RW]
uri[RW]

Public Class Methods

new(uri, token, options = {}) click to toggle source
# File lib/pilfer/server.rb, line 10
def initialize(uri, token, options = {})
  @uri   = URI.parse(uri)
  @token = token
  @async = options[:async] || true
  @on_error = options[:on_error] || lambda { |ex| $stdout.puts "Pilfer::Server Exception: #{ex.class}: #{ex.message}:\n#{ex.backtrace.join("\n\t")}" }
end

Public Instance Methods

write(profile_data, profile_start, description, options = {}) click to toggle source
# File lib/pilfer/server.rb, line 17
def write(profile_data, profile_start, description, options = {})
  async   = (options[:submit] || :sync) == :async
  details = { 'hostname'     => Socket.gethostname,
              'pid'          => Process.pid,
              'description'  => description,
              'file_sources' => file_sources_for_profile(profile_data) }

  payload = RbLineProfFormat.
              profile_to_json(profile_data, profile_start).
              merge(details)

  if async
    Thread.new(payload) do |payload|
      submit_profile payload
    end
  else
    submit_profile payload
  end
end

Private Instance Methods

file_sources_for_profile(profile_data) click to toggle source
# File lib/pilfer/server.rb, line 64
def file_sources_for_profile(profile_data)
  profile_data.each_with_object({}) {|(file, _), sources|
    sources[file] = File.exists?(file) ? File.read(file) : nil
  }
end
submit_profile(payload) click to toggle source
# File lib/pilfer/server.rb, line 39
def submit_profile(payload)
  request = Net::HTTP::Post.new('/api/profiles')
  request.content_type = 'application/json'
  request['Authorization'] = %{Token token="#{token}"}
  request.body = JSON.generate(payload)

  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.use_ssl = true
    store = OpenSSL::X509::Store.new
    store.set_default_paths
    http.cert_store = store
  end

  case (response = http.start {|http| http.request(request) })
  when Net::HTTPSuccess, Net::HTTPRedirection
  else
    response.error!
  end
rescue Exception => ex
  @on_error.call(ex)
end