Class: SimpleGCM::ResultMiddleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/simple_gcm/result_middleware.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_gcm/result_middleware.rb', line 3

def on_complete(env)
  case env[:status]
  when 200
    values = response_values(env)
    if values[:error]
      if Error.constants.include?(values[:error].to_sym)
        raise Error.const_get(values[:error]), values
      else
        raise Error::Unkown, values
      end
    else
      result = Result.new
      result.message_id = values[:id] if values[:id]
      result.registration_id = values[:registration_id] if values[:registration_id]
      env[:gcm_result] = result
    end
  when 401
    raise Error::AuthenticationError, response_values(env)
  when 500,503
    raise Error::ServerUnavailable, response_values(env)
  when 400...600
    raise Error::Unkown, response_values(env)
  end
end

#response_values(env) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/simple_gcm/result_middleware.rb', line 28

def response_values(env)
  body = env[:body]
  base = {:status => env[:status], :headers => env[:response_headers], :body => body}
  response_body = Array(body.lines.map{ |l| Hash[l.scan(/(.*)=(.*)/)] }.inject(&:merge)).inject({}) do |m,(k,v)|
    m[k.downcase.to_sym] = v
    m
  end
  response_body.merge(base)
end