Class: Fcmpush::Client

Inherits:
Object
  • Object
show all
Includes:
Batch
Defined in:
lib/fcmpush/client.rb

Constant Summary

Constants included from Batch

Batch::PART_BOUNDRY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Batch

#create_part, #make_batch_payload, #serialize_sub_request

Constructor Details

#initialize(domain, project_id, configuration, **options) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fcmpush/client.rb', line 17

def initialize(domain, project_id, configuration, **options)
  @domain = domain
  @project_id = project_id
  @path = V1_ENDPOINT_PREFIX + project_id.to_s + V1_ENDPOINT_SUFFIX
  @options = {}.merge(options)
  @configuration = configuration.dup
  access_token_response = v1_authorize
  @access_token = access_token_response['access_token']
  @access_token_expiry = Time.now.utc + access_token_response['expires_in']
  @server_key = configuration.server_key
  @connection = Net::HTTP::Persistent.new

  if !configuration.proxy
    # do nothing
  elsif configuration.proxy == :ENV
    @connection.proxy = :ENV
  elsif configuration.proxy && configuration.proxy[:uri]
    uri = URI(configuration.proxy[:uri])
    # user name must not be a empty string, password can
    if configuration.proxy[:user] && configuration.proxy[:user].strip != ''
      uri.user = configuration.proxy[:user]
      uri.password = configuration.proxy[:password] if configuration.proxy[:password]
    end
    @connection.proxy = uri
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def access_token
  @access_token
end

#access_token_expiryObject (readonly)

Returns the value of attribute access_token_expiry.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def access_token_expiry
  @access_token_expiry
end

#configurationObject (readonly)

Returns the value of attribute configuration.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def configuration
  @configuration
end

#connectionObject (readonly)

Returns the value of attribute connection.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def connection
  @connection
end

#domainObject (readonly)

Returns the value of attribute domain.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def domain
  @domain
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def path
  @path
end

#server_keyObject (readonly)

Returns the value of attribute server_key.



15
16
17
# File 'lib/fcmpush/client.rb', line 15

def server_key
  @server_key
end

Instance Method Details

#batch_push(messages, query: {}, headers: {}) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/fcmpush/client.rb', line 87

def batch_push(messages, query: {}, headers: {})
  uri, request = make_batch_request(messages, query, headers)
  response = exception_handler(connection.request(uri, request))
  BatchResponse.new(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
end

#push(body, query: {}, headers: {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/fcmpush/client.rb', line 63

def push(body, query: {}, headers: {})
  uri, request = make_push_request(body, query, headers)
  response = exception_handler(connection.request(uri, request))
  JsonResponse.new(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
end

#subscribe(topic, *instance_ids, query: {}, headers: {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/fcmpush/client.rb', line 71

def subscribe(topic, *instance_ids, query: {}, headers: {})
  uri, request = make_subscription_request(topic, *instance_ids, :subscribe, query, headers)
  response = exception_handler(connection.request(uri, request))
  JsonResponse.new(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
end

#unsubscribe(topic, *instance_ids, query: {}, headers: {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/fcmpush/client.rb', line 79

def unsubscribe(topic, *instance_ids, query: {}, headers: {})
  uri, request = make_subscription_request(topic, *instance_ids, :unsubscribe, query, headers)
  response = exception_handler(connection.request(uri, request))
  JsonResponse.new(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
end

#v1_authorizeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fcmpush/client.rb', line 44

def v1_authorize
  @auth ||= if configuration.json_key_io
              io = if configuration.json_key_io.respond_to?(:read)
                     configuration.json_key_io
                   else
                     File.open(configuration.json_key_io)
                   end
              io.rewind if io.respond_to?(:read)
              Google::Auth::ServiceAccountCredentials.make_creds(
                json_key_io: io,
                scope: configuration.scope
              )
            else
              # from ENV
              Google::Auth::ServiceAccountCredentials.make_creds(scope: configuration.scope)
            end
  @auth.fetch_access_token
end