Class: Gilgamesh

Inherits:
Object
  • Object
show all
Defined in:
lib/gilgamesh.rb,
lib/gilgamesh/version.rb

Constant Summary collapse

VERSION =
"0.0.8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, default_icon:, api_url: "http://gcm-http.googleapis.com/gcm/send") ⇒ Gilgamesh

Returns a new instance of Gilgamesh.



9
10
11
12
13
# File 'lib/gilgamesh.rb', line 9

def initialize(api_key, default_icon:, api_url: "http://gcm-http.googleapis.com/gcm/send")
  @api_key = api_key
  @api_url = api_url
  @default_icon = default_icon
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/gilgamesh.rb', line 6

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



6
7
8
# File 'lib/gilgamesh.rb', line 6

def api_url
  @api_url
end

#connObject (readonly)

Returns the value of attribute conn.



6
7
8
# File 'lib/gilgamesh.rb', line 6

def conn
  @conn
end

#default_iconObject (readonly)

Returns the value of attribute default_icon.



6
7
8
# File 'lib/gilgamesh.rb', line 6

def default_icon
  @default_icon
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/gilgamesh.rb', line 7

def response
  @response
end

Instance Method Details

#push(registration_token, title:, body:, click_action: nil, icon: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gilgamesh.rb', line 21

def push(registration_token, title:, body:, click_action: nil, icon: nil)
  body = {
    to: registration_token,
    notification: {
      title: title,
      body: body,
      icon: icon || default_icon
    }
  }
  body[:notification].merge!(click_action: click_action) unless click_action.nil?

  @response = conn.post do |req|
    req.headers['Authorization'] = "key=#{api_key}"
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
  end

  case response.status
  when 200
    "Success"
  when 400
    "INVALID_REGISTRATION: registration_token is not valid"
  when 401
    "Unauthorized: API key is not valid"
  end
end