Class: HiGCM::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/higcm/sender.rb

Constant Summary collapse

OPTIONAL_OPTIONS =
{
  :collapse_key     => String,
  :data             => Hash,
  :delay_while_idle => [true, false],
  :time_to_live     => Fixnum
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Sender

Returns a new instance of Sender.

Raises:



17
18
19
20
# File 'lib/higcm/sender.rb', line 17

def initialize(api_key)
  @api_key  = api_key
  raise SenderError.new("api_key is necessary for #{self.class}") if api_key.nil? || api_key.empty?
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/higcm/sender.rb', line 8

def api_key
  @api_key
end

#api_statusObject

Returns the value of attribute api_status.



8
9
10
# File 'lib/higcm/sender.rb', line 8

def api_status
  @api_status
end

#hydraObject

Returns the value of attribute hydra.



8
9
10
# File 'lib/higcm/sender.rb', line 8

def hydra
  @hydra
end

#requestsObject

Returns the value of attribute requests.



8
9
10
# File 'lib/higcm/sender.rb', line 8

def requests
  @requests
end

Instance Method Details

#convert_hash(hash) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/higcm/sender.rb', line 90

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

#send(registration_ids, opts = {}, handler = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/higcm/sender.rb', line 22

def send(registration_ids, opts={}, handler=nil)
  handler = HiGCM::Handler.new if handler.nil?
  request = send_async(registration_ids, opts, handler)
  send_async_run
  request.handled_response
end

#send_async(registration_ids, opts = {}, handler = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/higcm/sender.rb', line 30

def send_async(registration_ids, opts={}, handler=nil)

  headers = {
    'Content-Type'  => 'application/json',
    'Authorization' => sprintf("key=%s", @api_key)
  }

  body = {
    'registration_ids' => registration_ids,
  }

  #fill up option
  OPTIONAL_OPTIONS.each do | key, type |
    if opts.key?(key)
      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(
    'https://android.googleapis.com/gcm/send',
    :headers         => headers,
    :method          => :post,
    :body            => body.to_json,
    :follow_location => true
  )

  @hydra ||= Typhoeus::Hydra.new

  request.on_complete do | response |
    handler.handle(registration_ids, opts, response)
    response
  end

  @hydra.queue(request)

  request
end

#send_async_runObject



85
86
87
88
# File 'lib/higcm/sender.rb', line 85

def send_async_run
  # handle response according to http://developer.android.com/guide/google/gcm/gcm.html#response
  @hydra.run
end