Class: WhatCounts::OneOffMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/whatcounts/one_off_message.rb

Constant Summary collapse

REQUIRED_OPTIONS =
[:list_id,:to,:format]
IRREGULAR_OPTIONS =
[:data]
VALID_OPTIONS =
OPTIONS_TO_HTTP_PARAMS_MAP.keys

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OneOffMessage

Returns a new instance of OneOffMessage.



15
16
17
18
19
# File 'lib/whatcounts/one_off_message.rb', line 15

def initialize(options)
  options.each_key {|o| raise UnknownOptionError,"Unknown option #{o}" unless VALID_OPTIONS.include? o}
  REQUIRED_OPTIONS.each {|o| raise MissingRequiredOptionError,"Missing required option #{o}" unless options.has_key? o}
  options.each_pair {|k,v| self.send :"#{k}=",v}
end

Class Method Details

.send!(options) ⇒ Object



26
27
28
# File 'lib/whatcounts/one_off_message.rb', line 26

def send!(options)
  self.new(options).send!
end

Instance Method Details

#send!Object



21
22
23
# File 'lib/whatcounts/one_off_message.rb', line 21

def send!
  WhatCounts::HttpClient.execute('send',self)
end

#to_param_stringObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/whatcounts/one_off_message.rb', line 31

def to_param_string
  @param_string ||= begin
    pairs = (OPTIONS_TO_HTTP_PARAMS_MAP.keys-IRREGULAR_OPTIONS).inject([]) do |acc,k|
      val = self.send :"#{k}"
      acc << "#{OPTIONS_TO_HTTP_PARAMS_MAP[k]}=#{CGI.escape(val.to_s)}" unless val.nil?
      acc
    end
    unless self.data.nil?
      if self.data.kind_of? String
        pairs << "data=#{CGI.escape(self.data)}"
      elsif self.data.kind_of? Hash
        data_pairs = self.data.inject([[],[]]) do |acc,(k,v)| 
          acc[0] << CGI.escape(k.to_s)
          acc[1] << CGI.escape(v.to_s)
          acc
        end
        pairs << "data=#{data_pairs[0].join(',')}^#{data_pairs[1].join(',')}"
      else 
        # TODO i duno what to do here, probably error?  Maybe not since the param isn't required
      end
    end
    pairs
  end.join("&")
end