Module: AppilixNotifications
- Defined in:
- lib/appilix_notifications.rb,
lib/appilix_notifications/version.rb
Constant Summary collapse
- BASE_URL =
"https://appilix.com/api"
- PUSH_NOTIFICATION_URL =
"push-notification"
- TOKEN_LIST_URL =
"push_notification_token_list.php"
- VERSION =
"0.1.0"
Class Method Summary collapse
- .appilix_get_registered_user(app_key, api_key, page) ⇒ Object
- .appilix_send_notifications(app_key, api_key, title, body, user_identity = nil, open_link_url = nil) ⇒ Object
- .handle_response(response) ⇒ Object
- .parse_body_safe(body) ⇒ Object
Class Method Details
.appilix_get_registered_user(app_key, api_key, page) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/appilix_notifications.rb', line 36 def appilix_get_registered_user(app_key, api_key, page) raise ArgumentError, "Page parameter is mandatory" unless page uri = URI("#{BASE_URL}/#{TOKEN_LIST_URL}") uri.query = URI.encode_www_form( 'app_key' => app_key, 'api_key' => api_key, 'page' => page ) request = Net::HTTP::Get.new(uri) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end handle_response(response) end |
.appilix_send_notifications(app_key, api_key, title, body, user_identity = nil, open_link_url = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/appilix_notifications.rb', line 16 def appilix_send_notifications(app_key, api_key, title, body, user_identity = nil, open_link_url = nil) uri = URI("#{BASE_URL}/#{PUSH_NOTIFICATION_URL}") uri.query = URI.encode_www_form( 'app_key' => app_key, 'api_key' => api_key, 'notification_title' => title, 'notification_body' => body, 'user_identity' => user_identity, 'open_link_url' => open_link_url ) request = Net::HTTP::Get.new(uri) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end handle_response(response) end |
.handle_response(response) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/appilix_notifications.rb', line 55 def handle_response(response) case response when Net::HTTPSuccess JSON.parse(response.body) else { 'error' => response., 'code' => response.code, 'body' => parse_body_safe(response.body) } end rescue JSON::ParserError { 'error' => 'Invalid JSON response', 'body' => response.body } end |
.parse_body_safe(body) ⇒ Object
66 67 68 69 70 |
# File 'lib/appilix_notifications.rb', line 66 def parse_body_safe(body) JSON.parse(body) rescue JSON::ParserError body end |