Sha256: 042efdeb9618f19df494378fba9abbe217846d2054f3a76b5de168761261ce4c

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Pwush
  class Client
    def initialize(options)
      @config = Config.new(options)
    end

    def create_message(*notifications)
      post(:createMessage, notifications: notifications)
    end
    alias push create_message

    def message_details(message)
      post(:getMessageDetails, message: message)
    end

    # enterprise api
    def message_stats(message)
      post(:getMsgStats, message: message)
    end

    # enterprise api
    def results(request_id)
      post(:getResults,  request_id: request_id)
    end

    # enterprise api
    def applications
      post(:getApplications)
    end

    # enterprise api
    def preset(preset_code)
      post(:getPreset, preset_code: preset_code)
    end

    private

    def get(action, payload = nil)
      perform_request(:get, action, payload)
    end

    def post(action, payload = nil)
      perform_request(:post, action, payload)
    end

    def perform_request(verb, action, payload)
      response = Http.request(
        verb, url(action), json: build_request(payload).body
      )
      parse_response(response)
    end

    def build_request(payload = nil)
      Request.new(auth: @config.auth, app: @config.app, payload: payload)
    end

    def parse_response(response)
      Oj.load(response.body)
    end

    def url(action)
      [@config.url, action].join('/')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pwush-0.0.0 lib/pwush/client.rb