Class: Suj::Pusher::WPNSConnection

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/suj/pusher/wpns_connection.rb

Constant Summary collapse

ERRORS =
{
  0 => "No errors encountered",
  1 => "Processing error",
  2 => "Missing device token",
  3 => "Missing topic",
  4 => "Missing payload",
  5 => "Invalid token size",
  6 => "Invalid topic size",
  7 => "Invalid payload size",
  8 => "Invalid token",
  255 => "Unknown error"
}

Instance Method Summary collapse

Constructor Details

#initialize(pool, options = {}) ⇒ WPNSConnection

Returns a new instance of WPNSConnection.



24
25
26
27
28
29
30
# File 'lib/suj/pusher/wpns_connection.rb', line 24

def initialize(pool,options = {})
  @pool = pool 
  @options = options
  @cert_key = Digest::SHA1.hexdigest(@options[:secret])
  @cert_file = File.join(Suj::Pusher.config.certs_path, @cert_key)
  info "initialazing wns connection"
end

Instance Method Details

#deliver(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
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
80
81
82
83
84
85
86
# File 'lib/suj/pusher/wpns_connection.rb', line 33

def deliver(options)
  begin
      @options = options
    @notification = Suj::Pusher::WnsNotification.new(@options)
      info "WPNS delivering data"
#	    @Authorization  = "Bearer #{token["access_token"]}"
      @Content_type = "text/xml; charset=utf-8"
      @Content_length = @options[:data].size #@notification.data.size
      @X_MessageID = nil
      @X_MessageID = @options[:wpmid]
	    @X_WindowsPhone_Target = nil
      @X_WindowsPhone_Target = @options[:wptype]
      @X_NotificationClass = @options[:wpnotificationclass]
	    @options[:wpids].each do |id|
		info "atempting send notification to Id: #{id}"
		@header = Hash.new
		@header['Content-Type'] = @Content_type 
		@header['Accept'] = "application/*" 
	#	@header['Content-Length'] = @Content_length 
		@header['X-NotificationClass'] =  @X_NotificationClass
		if (@X_WindowsPhone_Target != nil)
  		@header['X-WindowsPhone-Target'] = @X_WindowsPhone_Target
		else
          end 
		if( @X_MessageID  != nil)
  			@header['X-MessageID'] = @X_MessageID 
		else
		end
		http = EventMachine::HttpRequest.new(id).post( :head => @header, :body => @options[:data])# @notification.data )
  	info http.inspect
  	http.errback do
    		error "WPNS-sending network error"
  	end
  	http.callback do
			if http.response_header.status != 200
      			error "WPNS-sending push error #{http.response_header.status}"
     			 error http.response
     			 error http.response_header
    		else
      			info "WPNS-push notification sent"
      			info http.response_header
    		end
  	end
      
      end

      @notification = nil
      info "WPNS delivered data"
  rescue => ex
    unbind
    error "WPNS notification error : #{ex}"
  end
	unbind 
end

#unbindObject



91
92
93
94
95
# File 'lib/suj/pusher/wpns_connection.rb', line 91

def unbind
  info "WPNS Connection closed..."
  FileUtils.rm_f(@cert_file)
  @pool.remove_connection(@cert_key)
end