lib/higcm/sender.rb in higcm-0.0.3 vs lib/higcm/sender.rb in higcm-0.0.4
- old
+ new
@@ -6,13 +6,13 @@
class Sender
attr_accessor :api_key, :api_status, :hydra, :requests
OPTIONAL_OPTIONS = {
- :collapse_key => String,
- :data => Hash,
- :delay_while_idle => Fixnum,
+ :collapse_key => String,
+ :data => Hash,
+ :delay_while_idle => [true, false],
:time_to_live => Fixnum
}
def initialize(api_key)
@api_key = api_key
@@ -39,11 +39,27 @@
}
#fill up option
OPTIONAL_OPTIONS.each do | key, type |
if opts.key?(key)
- raise SenderError.new("#{key} should be Type #{type}") unless opts[key].is_a?(type)
+ if type.is_a?(Array)
+ @valid_value = false
+ type.each do | v |
+ if opts[key] == v
+ @valid_value = true
+ break
+ end
+ end
+ raise SenderError.new("#{key} should be Type #{type}") unless @valid_value
+ else
+ raise SenderError.new("#{key} should be Type #{type}") unless opts[key].is_a?(type)
+ end
+ # convert payload data to String for issue #3
+ case key
+ when :data
+ opts[key] = convert_hash(opts[key])
+ end
body[key] = opts[key]
end
end
request = Typhoeus::Request.new(
@@ -66,9 +82,23 @@
end
def send_async_run
# handle response according to http://developer.android.com/guide/google/gcm/gcm.html#response
@hydra.run
+ end
+
+ def convert_hash(hash)
+ hash.each do | k, v |
+ if v.is_a?(Hash)
+ hash[k] = convert_hash(v)
+ else
+ if v.respond_to?(:to_s)
+ hash[k] = v.to_s
+ else
+ raise SenderError "data value must respond to to_s function for converting to String"
+ end
+ end
+ end
end
end
end