|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'fcm/error' |
| 4 | + |
| 5 | +module Fcm |
| 6 | + # Custome Fcm response |
| 7 | + module Response |
| 8 | + class << self |
| 9 | + # Converts the faraday response into a custom fcm response |
| 10 | + # |
| 11 | + # @param response [Faraday::Response] a faraday response object |
| 12 | + # @return [Hash] a custom fcm response hash |
| 13 | + def build_fcm_response(response, registration_ids = []) |
| 14 | + return success_response(response, registration_ids) if response.success? |
| 15 | + |
| 16 | + failure_response(response) |
| 17 | + end |
| 18 | + |
| 19 | + private |
| 20 | + |
| 21 | + def success_response(response, registration_ids) |
| 22 | + body = response.body || {} |
| 23 | + response_hash = { |
| 24 | + body: body, |
| 25 | + headers: response.headers, |
| 26 | + status_code: response.status, |
| 27 | + response: 'success' |
| 28 | + } |
| 29 | + return response_hash if registration_ids.empty? |
| 30 | + |
| 31 | + body = JSON.parse(body) unless body.empty? |
| 32 | + response_hash[:canonical_ids] = build_canonical_ids( |
| 33 | + body, registration_ids |
| 34 | + ) |
| 35 | + response_hash[:not_registered_ids] = build_not_registered_ids( |
| 36 | + body, registration_ids |
| 37 | + ) |
| 38 | + response_hash |
| 39 | + end |
| 40 | + |
| 41 | + def failure_response(response) |
| 42 | + body = response.body || {} |
| 43 | + response_hash = { |
| 44 | + body: body, |
| 45 | + headers: response.headers, |
| 46 | + status_code: response.status |
| 47 | + } |
| 48 | + |
| 49 | + response_hash[:response] = case response.status.to_i |
| 50 | + when 400 |
| 51 | + Fcm::Error::ERROR_RESPONSE_400 |
| 52 | + when 401 |
| 53 | + Fcm::Error::ERROR_RESPONSE_401 |
| 54 | + when 503 |
| 55 | + Fcm::Error::ERROR_RESPONSE_503 |
| 56 | + when 500..599 |
| 57 | + Fcm::Error::ERROR_RESPONSE_50X |
| 58 | + end |
| 59 | + response_hash |
| 60 | + end |
| 61 | + |
| 62 | + def build_canonical_ids(body, registration_ids) |
| 63 | + canonical_ids = [] |
| 64 | + return canonical_ids if body.empty? || body['canonical_ids'] <= 0 |
| 65 | + |
| 66 | + body['results'].each_with_index do |result, index| |
| 67 | + return canonical_ids unless canonical_id?(result) |
| 68 | + |
| 69 | + canonical_ids << { |
| 70 | + old: registration_ids[index], new: result['registration_id'] |
| 71 | + } |
| 72 | + end |
| 73 | + canonical_ids |
| 74 | + end |
| 75 | + |
| 76 | + def build_not_registered_ids(body, registration_id) |
| 77 | + not_registered_ids = [] |
| 78 | + return not_registered_ids if body.empty? |
| 79 | + |
| 80 | + if body['failure'].positive? |
| 81 | + body['results'].each_with_index do |result, index| |
| 82 | + not_registered_ids << registration_id[index] if not_registered?(result) |
| 83 | + end |
| 84 | + end |
| 85 | + not_registered_ids |
| 86 | + end |
| 87 | + |
| 88 | + def canonical_id?(result) |
| 89 | + !result['registration_id'].nil? |
| 90 | + end |
| 91 | + |
| 92 | + def not_registered?(result) |
| 93 | + result['error'] == 'NotRegistered' |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments