Sha256: 5a41ca1961ee7da9c2a2093f9f9257c00c7f2cb9eedcd11705f03c733c58bb94

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'faraday'

module FiveMobilePush
  class Client

    DEFAULT_ENDPOINT = 'https://push.fivemobile.com/rest'

    attr_accessor :application_uid, :api_token

    def initialize(options={})
      self.application_uid = options[:application_uid] || FiveMobilePush.application_uid
      self.api_token       = options[:api_token]       || FiveMobilePush.api_token
    end

    def get(path, options={})
      perform_request(:get, path, options)
    end

    def post(path, options={})
      perform_request(:post, path, options)
    end

    def device(device_uid, device_token=nil)
      FiveMobilePush::Device.new(self, device_uid, device_token)
    end

    def notifier
      FiveMobilePush::Notifier.new(self)
    end

    def tag(device_uid, device_token)
      FiveMobilePush::Tag.new(self, device_uid, device_token)
    end

    private

      def perform_request(method, path, options={})
        options.merge!(
          :api_token      => options[:api_token] || api_token,
          :application_id => application_uid
        )

        conn = Faraday.new(:url => DEFAULT_ENDPOINT)
        resp = conn.send(method) do |req|
          req.url path, options
        end

        # TODO Add error processor here.
        # Basic error checking
        if resp.status == 400
          raise InvalidToken if resp.body =~ /Invalid API token/i
        end

        resp
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
five_mobile_push-0.4.7 lib/five_mobile_push/client.rb