class OneSignal::Client

Public Class Methods

new(app_id, api_key, api_url) click to toggle source
# File lib/onesignal/client.rb, line 9
def initialize app_id, api_key, api_url
  @app_id = app_id
  @api_key = api_key
  @api_url = api_url
  @conn = ::Faraday.new(url: api_url) do |faraday|
    # faraday.response :logger do |logger|
    #   logger.filter(/(api_key=)(\w+)/, '\1[REMOVED]')
    #   logger.filter(/(Basic )(\w+)/, '\1[REMOVED]')
    # end
    faraday.adapter Faraday.default_adapter
  end
end

Public Instance Methods

create_notification(notification) click to toggle source
# File lib/onesignal/client.rb, line 22
def create_notification notification
  post 'notifications', notification
end
csv_export(extra_fields: nil, last_active_since: nil, segment_name: nil) click to toggle source
# File lib/onesignal/client.rb, line 38
def csv_export extra_fields: nil, last_active_since: nil, segment_name: nil
  post "players/csv_export?app_id=#{@app_id}", 
    extra_fields: extra_fields, 
    last_active_since: last_active_since&.to_i&.to_s, 
    segment_name: segment_name
end
fetch_notification(notification_id) click to toggle source
# File lib/onesignal/client.rb, line 26
def fetch_notification notification_id
  get "notifications/#{notification_id}"
end
fetch_player(player_id) click to toggle source
# File lib/onesignal/client.rb, line 34
def fetch_player player_id
  get "players/#{player_id}"
end
fetch_players() click to toggle source
# File lib/onesignal/client.rb, line 30
def fetch_players
  get 'players'
end

Private Instance Methods

create_body(payload) click to toggle source
# File lib/onesignal/client.rb, line 47
def create_body payload
  body = payload.as_json.delete_if { |_, v| v.nil? }
  body['app_id'] = @app_id
  body
end
get(url) click to toggle source
# File lib/onesignal/client.rb, line 64
def get url
  res = @conn.get do |req|
    req.url url, app_id: @app_id
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{@api_key}"
  end

  handle_errors res
end
handle_errors(res) click to toggle source
# File lib/onesignal/client.rb, line 74
def handle_errors res
  errors = JSON.parse(res.body).fetch 'errors', []
  raise ApiError, (errors.first || "Error code #{res.status}") if res.status > 399 || errors.any?

  res
end
post(url, body) click to toggle source
# File lib/onesignal/client.rb, line 53
def post url, body
  res = @conn.post do |req|
    req.url url
    req.body = create_body(body).to_json
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{@api_key}"
  end

  handle_errors res
end