Class: Gilgamesh

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

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Gilgamesh.



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

def initialize(api_key, api_url: "http://gcm-http.googleapis.com/gcm/send")
  @api_key = api_key
  @api_url = api_url
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

#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:) ⇒ Object



20
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
# File 'lib/gilgamesh.rb', line 20

def push(registration_token, title:, body:)
  body = {
    to: registration_token,
    notification: {
      title: title,
      body: body,
      icon: 'ic_launcher',
      click_action: 'android.intent.action.MAIN'
    }
  }

  @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