Sha256: 803adc0decaa1ad6716ed4d71065ed9c2654698d5a7713f3513b5b20b93b8626

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'washbullet/push'

module Washbullet
  class Pushable
    class MissingParameter < StandardError; end

    attr_reader :client, :receiver, :identifier, :params

    def self.push(client, receiver, identifier, params)
      response = new(client, receiver, identifier, params).push

      Push.new(response.body)
    end

    def initialize(client, receiver, identifier, params)
      @client     = client
      @receiver   = receiver
      @identifier = identifier
      @params     = params
    end

    def push
      raise MissingParameter unless params.keys == required_parameters

      payload = params.merge(type: type)

      payload = specify_receiver(payload)

      client.post('/v2/pushes', payload)
    end

    def type
      raise NotImplementedError
    end

    def required_parameters
      raise NotImplementedError
    end

    def specify_receiver(payload)
      if receiver && identifier
        payload.merge(receiver_type => identifier)
      else
        payload
      end
    end

    def receiver_type
      case receiver
      when :device  then :device_iden
      when :email   then receiver
      when :channel then :channel_tag
      when :client  then :client_iden
      else
        raise NotImplementedError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
washbullet-0.4.0 lib/washbullet/pushable.rb