Class: Pusher::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pusher/request.rb', line 9

def initialize(client, verb, uri, params, body = nil)
  @client, @verb, @uri = client, verb, uri
  @head = {
    'X-Pusher-Library' => 'pusher-http-ruby ' + Pusher::VERSION
  }

  @body = body
  if body
    params[:body_md5] = Digest::MD5.hexdigest(body)
    @head['Content-Type'] = 'application/json'
  end

  request = Pusher::Signature::Request.new(verb.to_s.upcase, uri.path, params)
  request.sign(client.authentication_token)
  @params = request.signed_params
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


7
8
9
# File 'lib/pusher/request.rb', line 7

def body
  @body
end

#paramsObject (readonly)

Returns the value of attribute params


7
8
9
# File 'lib/pusher/request.rb', line 7

def params
  @params
end

Instance Method Details

#send_asyncObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pusher/request.rb', line 43

def send_async
  if defined?(EventMachine) && EventMachine.reactor_running?
    http_client = @client.em_http_client(@uri)
    df = EM::DefaultDeferrable.new

    http = case @verb
    when :post
      http_client.post({
        :query => @params, :body => @body, :head => @head
      })
    when :get
      http_client.get({
        :query => @params, :head => @head
      })
    else
      raise "Unsupported verb"
    end
    http.callback {
      begin
        df.succeed(handle_response(http.response_header.status, http.response.chomp))
      rescue => e
        df.fail(e)
      end
    }
    http.errback { |e|
      message = "Network error connecting to pusher (#{http.error})"
      Pusher.logger.debug(message)
      df.fail(Error.new(message))
    }

    return df
  else
    http = @client.sync_http_client

    return http.request_async(@verb, @uri, @params, @body, @head)
  end
end

#send_syncObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pusher/request.rb', line 26

def send_sync
  http = @client.sync_http_client

  begin
    response = http.request(@verb, @uri, @params, @body, @head)
  rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError,
         SocketError, Errno::ECONNREFUSED => e
    error = Pusher::HTTPError.new("#{e.message} (#{e.class})")
    error.original_error = e
    raise error
  end

  body = response.body ? response.body.chomp : nil

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