class Opium::Push

Attributes

channels[RW]
criteria[RW]
criteria=[RW]
data[RW]
expiration_interval[RW]
expires_at[RW]
push_at[RW]
where[RW]

Public Class Methods

new( attributes = {} ) click to toggle source
# File lib/opium/push.rb, line 33
def initialize( attributes = {} )
  self.channels = []
  self.data = {}.with_indifferent_access
  attributes.each {|k, v| self.send( "#{k}=", v )}
end

Private Class Methods

attr_hash_accessor( hash_name, method_name ) click to toggle source
# File lib/opium/push.rb, line 16
def attr_hash_accessor( hash_name, method_name )
  unless respond_to?( method_name )
    define_method method_name do
      self.send( hash_name )[ method_name ]
    end
  end
  setter = "#{ method_name }="
  unless respond_to?( setter )
    define_method setter do |value|
      self.send( hash_name )[ method_name ] = value
    end
  end
end
attr_hash_accessors( hash_name, *methods ) click to toggle source
# File lib/opium/push.rb, line 10
def attr_hash_accessors( hash_name, *methods )
  methods.each do |method_name|
    attr_hash_accessor( hash_name, method_name )
  end
end

Public Instance Methods

create() click to toggle source
# File lib/opium/push.rb, line 46
def create
  self.class.as_resource(:push) do
    result = self.class.http_post post_data
    result[:result]
  end
end

Private Instance Methods

post_data() click to toggle source
# File lib/opium/push.rb, line 55
def post_data
  {}.tap do |pd|
    targetize!( pd )
    schedulize!( pd )
    pd[:data] = data
  end
end
schedulize!( hash ) click to toggle source
# File lib/opium/push.rb, line 76
def schedulize!( hash )
  fail ArgumentError, 'No scheduled time for #push_at specified!' if expiration_interval && !push_at
  if push_at
    fail ArgumentError, 'Can only schedule a push up to 2 weeks in advance!' if push_at > ( Time.now + ( 2 * 604800 ) )
    fail ArgumentError, 'Cannot schedule pushes in the past... unless you are the Doctor' if push_at < Time.now
    hash[:push_time] = push_at.iso8601
    hash[:expiration_interval] = expiration_interval
  elsif expires_at
    fail ArgumentError, 'Cannot schedule expiration in the past... unless you have a TARDIS' if expires_at < Time.now
    hash[:expiration_time] = expires_at.iso8601
  end
end
targetize!( hash ) click to toggle source
# File lib/opium/push.rb, line 63
def targetize!( hash )
  if criteria
    c = criteria
    c = Installation.where( c ) unless c.is_a?( Opium::Model::Criteria )
    c = c.and( channels: channels ) unless channels.empty?
    hash[:where] = c.constraints[:where]
  elsif !channels.empty?
    hash[:channels] = channels
  else
    fail ArgumentError, 'No channels or criteria were specified!'
  end
end