class Eventflit::NativeNotification::Client

Constants

API_PREFIX

Attributes

app_id[R]
host[R]

Public Class Methods

new(app_id, host, scheme, eventflit_client) click to toggle source
# File lib/eventflit/native_notification/client.rb, line 8
def initialize(app_id, host, scheme, eventflit_client)
  @app_id = app_id
  @host = host
  @scheme = scheme
  @eventflit_client = eventflit_client
end

Public Instance Methods

notify(interests, data = {}) click to toggle source

Send a notification via the native notifications API

# File lib/eventflit/native_notification/client.rb, line 16
def notify(interests, data = {})
  Request.new(
    @eventflit_client,
    :post,
    url("/publishes"),
    {},
    payload(interests, data)
  ).send_sync
end

Private Instance Methods

deep_symbolize_keys!(hash) click to toggle source

Symbolize all keys in the hash recursively

# File lib/eventflit/native_notification/client.rb, line 57
def deep_symbolize_keys!(hash)
  hash.keys.each do |k|
    ks = k.respond_to?(:to_sym) ? k.to_sym : k
    hash[ks] = hash.delete(k)
    deep_symbolize_keys!(hash[ks]) if hash[ks].kind_of?(Hash)
  end

  hash
end
payload(interests, data) click to toggle source

{

interests: [Array of interests],
apns: {
  See https://docs.eventflit.com/push_notifications/ios/server
},
gcm: {
  See https://docs.eventflit.com/push_notifications/android/server
}

}

@raise [Eventflit::Error] if the interests array is empty @return [String]

# File lib/eventflit/native_notification/client.rb, line 40
def payload(interests, data)
  interests = Array(interests).map(&:to_s)

  raise Eventflit::Error, "Interests array must not be empty" if interests.length == 0

  data = deep_symbolize_keys!(data)

  data.merge!(interests: interests)

  MultiJson.encode(data)
end
url(path = nil) click to toggle source
# File lib/eventflit/native_notification/client.rb, line 52
def url(path = nil)
  URI.parse("#{@scheme}://#{@host}/#{API_PREFIX}/#{@app_id}#{path}")
end