Sha256: acd9031670cbc062b05bef640d3774c5ef9054f757493a8e28f7ac98c92e3e88

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

require 'json'
require 'ostruct'

module Apphunk
  module Proxy
    class << self
      
      PROXY_API_URL = "http://127.0.0.1:8212/api/messages"
      
      def send_message_to_apphunkd(message, options)
        if options[:environments] && options[:environment] && !options[:environments].include?(options[:environment])
          return false
        end
        
        payload = prepare_payload(message, options)
        result = Apphunk::Remote.post(PROXY_API_URL, payload, 3)
        return process_response(result)
      end

      def prepare_payload(message, options)
        {
          :message      => message,
          :token        => options[:token],
          :environment  => options[:environment],
          :tags         => options[:tags],
          :trails       => (options[:trails].to_json if options[:trails])
        }
      end
      
      def process_response(result)
        if result.status == :ok
          if result.response.code == '201'
            return true
          else
            Apphunk::Logger.error "The Apphunkd-Proxy couldn't store the message: #{result.response.code} / #{result.response.body}"
            return false
          end
        else
          Apphunk::Logger.error "Connection Error: Could not get a response from local Apphunkd-Proxy in time"
          return false
        end
      end
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apphunk-0.4.1 lib/apphunk/proxy.rb
apphunk-0.4.0 lib/apphunk/proxy.rb
apphunk-0.3.1 lib/apphunk/proxy.rb
apphunk-0.3.0 lib/apphunk/proxy.rb