class Azure::Push::Message

Public Class Methods

new(namespace, hub, access_key, key_name: 'DefaultFullSharedAccessSignature', sig_lifetime: 10) click to toggle source
# File lib/azure/push/message.rb, line 11
def initialize(namespace, hub, access_key,
    key_name: 'DefaultFullSharedAccessSignature',
    sig_lifetime: 10)
  @access_key = access_key
  @key_name = key_name
  @namespace = namespace
  @hub = hub
  @sig_lifetime = sig_lifetime
end

Public Instance Methods

send(payload, tags = nil, format: 'apple', additional_headers: {}) click to toggle source
# File lib/azure/push/message.rb, line 21
def send(payload, tags = nil, format: 'apple', additional_headers: {})
  raise ArgumentError unless ['apple', 'gcm', 'template', 'windows', 'windowsphone'].include? format
  raise ArgumentError unless additional_headers.instance_of?(Hash)
  if tags.instance_of?(Array)
    tags = tags.join(' || ')
  end
  uri = URI(url)
  content_type = ['apple', 'gcm', 'template'].include?(format) ? 'application/json' : 'application/xml;charset=utf-8'
  headers = {
    'Content-Type' => content_type,
    'Authorization' => Azure::Push::Sas.sas_token(url, @key_name, @access_key, lifetime: @sig_lifetime),
    'ServiceBusNotification-Format' => format
  }.merge(additional_headers)

  headers['ServiceBusNotification-Tags'] = tags if tags

  http = Net::HTTP.new(uri.host,uri.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(uri.path, initheader = headers)
  req.body = payload.to_json
  res = http.request(req)
  res.is_a?(Net::HTTPSuccess) ? true : res
end

Private Instance Methods

url() click to toggle source
# File lib/azure/push/message.rb, line 47
def url
  "https://#{@namespace}.servicebus.windows.net/#{@hub}/messages"
end