Class: Pusher::Request

Inherits:
Object
  • Object
show all
Includes:
QueryEncoder
Defined in:
lib/pusher/request.rb

Defined Under Namespace

Modules: QueryEncoder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryEncoder

#bytesize, #encode_param, #encode_query, #escape

Constructor Details

#initialize(verb, uri, params, body = nil, token = nil, client = Pusher) ⇒ Request

Returns a new instance of Request.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pusher/request.rb', line 57

def initialize(verb, uri, params, body = nil, token = nil, client = Pusher)
  @verb = verb
  @uri = uri
  @client = client

  if body
    @body = body
    params[:body_md5] = Digest::MD5.hexdigest(body)
  end

  request = Signature::Request.new(verb.to_s.upcase, uri.path, params)
  auth_hash = request.sign(token || @client.authentication_token)
  @params = params.merge(auth_hash)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



55
56
57
# File 'lib/pusher/request.rb', line 55

def body
  @body
end

#paramsObject (readonly)

Returns the value of attribute params.



55
56
57
# File 'lib/pusher/request.rb', line 55

def params
  @params
end

Instance Method Details

#send_asyncObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pusher/request.rb', line 109

def send_async
  unless defined?(EventMachine) && EventMachine.reactor_running?
    raise Error, "In order to use trigger_async you must be running inside an eventmachine loop"
  end
  require 'em-http' unless defined?(EventMachine::HttpRequest)

  deferrable = EM::DefaultDeferrable.new

  http = EventMachine::HttpRequest.new(@uri).post({
    :query => @params, :timeout => 5, :body => @body,
    :head => {'Content-Type'=> 'application/json'}
  })
  http.callback {
    begin
      handle_response(http.response_header.status, http.response.chomp)
      deferrable.succeed
    rescue => e
      deferrable.fail(e)
    end
  }
  http.errback {
    Pusher.logger.debug("Network error connecting to pusher: #{http.inspect}")
    deferrable.fail(Error.new("Network error connecting to pusher"))
  }

  deferrable
end

#send_syncObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pusher/request.rb', line 72

def send_sync
  require 'net/http' unless defined?(Net::HTTP)
  require 'net/https' if (ssl? && !defined?(Net::HTTPS))

  @http_sync ||= begin
    http = Net::HTTP.new(@uri.host, @uri.port)
    http.use_ssl = true if ssl?
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ssl?
    http
  end

  begin
    case @verb
    when :post
      response = @http_sync.post(encode_query(@uri, @params), @body, {
        'Content-Type'=> 'application/json'
      })
    when :get
      response = @http_sync.get(encode_query(@uri, @params), {
        'Content-Type'=> 'application/json'
      })
    else
      raise "Unknown verb"
    end
  rescue Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED,
         Errno::ETIMEDOUT, Errno::EHOSTUNREACH, Errno::ECONNRESET,
         Timeout::Error, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
         Net::ProtocolError => e
    error = Pusher::HTTPError.new("#{e.message} (#{e.class})")
    error.original_error = e
    raise error
  end

  return handle_response(response.code.to_i, response.body.chomp)
end