Class: CarbonPms::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/carbon_pms/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = CarbonPms.host, auth_token = CarbonPms.auth_token, version = CarbonPms.version) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/carbon_pms/client.rb', line 8

def initialize(host=CarbonPms.host, auth_token=CarbonPms.auth_token, version=CarbonPms.version)
  if host.downcase.start_with?('http://', 'https://')
    self.class.base_uri "#{host}/api"
  else
    self.class.base_uri "https://#{host}/api"
  end
  self.class.headers('Authorization' => %Q[Token token="#{auth_token}"]) if auth_token
  self.class.headers('Accept' => "application/vnd.carbon.pms.v#{version}") if version
end

Instance Method Details

#apns_bulk_notificaton(options = {}) ⇒ Object Also known as: apns_bulk_notification

Sends bulk push notifications to the APNS

Parameters:

  • options (Hash) (defaults to: {})

    hash to construct a notification

Options Hash (options):

  • :device_token (Array)

    array of device token to receive this push notification

  • :app (String)

    the name of the app

  • :environment (String)

    the environment this app is running in _(production, development, sandbox)_

  • :alert (String)

    the message to send

  • :data (Hash)

    any custom attributes to send

  • :badge (Integer)

    sets the badge number to the specified number

  • :sound (String)

    play the specified sound when the notification is received.

  • :content_available (Integer)

    notify the device that new content is available



74
75
76
# File 'lib/carbon_pms/client.rb', line 74

def apns_bulk_notificaton(options={})
  return post '/push/apns_bulk', body: options
end

#apns_notificaton(options = {}) ⇒ Object Also known as: apns_notification

Sends a push notification to the APNS

Parameters:

  • options (Hash) (defaults to: {})

    hash to construct a notification

Options Hash (options):

  • :device_token (String)

    the device token to receive this push notification

  • :app (String)

    the name of the app

  • :environment (String)

    the environment this app is running in _(production, development, sandbox)_

  • :alert (String)

    the message to send

  • :data (Hash)

    any custom attributes to send

  • :badge (Integer)

    sets the badge number to the specified number

  • :sound (String)

    play the specified sound when the notification is received.

  • :content_available (Integer)

    notify the device that new content is available



57
58
59
# File 'lib/carbon_pms/client.rb', line 57

def apns_notificaton(options={})
  return post '/push/apns', body: options
end

#create_app(options = {}) ⇒ HTTParty::Response

Creates either a GCM or APNS app to receive push messages. You must create an app before you can send push notifications.

Parameters:

  • options (Hash) (defaults to: {})

    hash to construct an app. Must include :name and (:certificate or :auth_key)

Options Hash (options):

  • :name (String)

    A name for this app. The name must be unique within the environment and app type

  • :environment (String)

    can be one of the following:

    • production

    • development

    • sandbox

    Defaults to development

  • :auth_key (String)

    Setting this key will create a GCM app. Your Google API key goes here. This option is mutually exclusive with :certificate

  • :certificate (String)

    Setting this key will create an APNS app. Your certificate needs to be in pem format. This option is mutually exclusive with :auth_key

  • :password (String)

    The password to decrypt the certificate if it was encrypted.

Returns:

  • (HTTParty::Response)

    The response from the server. The following properties may be interesting:

    • body : the parsed response body

    • code : the status code

    • message : the status message

    • headers : the response headers

Raises:

  • (ArgumentError)

    if anything is amiss



38
39
40
41
42
43
44
# File 'lib/carbon_pms/client.rb', line 38

def create_app(options={})
  raise ArguementError, 'You must provide an app name' unless options[:name]
  options[:environment] = 'development' unless %w{production sandbox}.include? options[:environment]
  return post('/app/apns', body: options) if options.has_key? :certificate
  return post('/app/gcm', body: options) if options.has_key? :auth_key
  raise ArguementError, 'You must provide a :certificate or :auth_key'
end

#gcm_notification(options = {}) ⇒ Object

Sends a push notification to GCM

Parameters:

  • options (Hash) (defaults to: {})

    hash to construct a notification

Options Hash (options):

  • :registration_ids (String)

    array of registration ids to receive this notification. You can send up to 1000 at once.

  • :app (String)

    the name of the app

  • :environment (String)

    the environment this app is running in _(production, development, sandbox)_

  • :data (Hash)

    any custom attributes to send

  • :collapse_key (String)

    signifies that this is a send-to-sync message. You can use up to 4 different collapse_keys at a time

  • :expiry (Integer)

    set the time to live of the message. Can be from 0 to 2,419,200 seconds.



89
90
91
# File 'lib/carbon_pms/client.rb', line 89

def gcm_notification(options={})
  return post '/push/gcm', body: options
end