Class: KakaoPush::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kakao_push/client.rb

Constant Summary collapse

API_HOST =
"https://kapi.kakao.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_api_key: ENV['KAKAO_PUSH_CLIENT_ID']) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/kakao_push/client.rb', line 7

def initialize(rest_api_key: ENV['KAKAO_PUSH_CLIENT_ID'])
  self.rest_api_key = rest_api_key
  self.connection = build_connection
end

Instance Attribute Details

#rest_api_keyObject

Returns the value of attribute rest_api_key.



5
6
7
# File 'lib/kakao_push/client.rb', line 5

def rest_api_key
  @rest_api_key
end

Instance Method Details

#build_connection(&block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kakao_push/client.rb', line 67

def build_connection(&block)
  Faraday.new(faraday_client_options) do |builder|
    builder.request :retry,
                    max: 2,
                    interval: 0.05,
                    interval_randomness: 0.5,
                    backoff_factor: 2,
                    methods: [:delete, :get, :head, :options, :put, :post],
                    exceptions: [
                      ::Errno::ETIMEDOUT,
                      'Timeout::Error',
                      ::Faraday::Error::TimeoutError,
                      'Net::OpenTimeout',
                      'Net::ReadTimeout',
                      ::Faraday::TimeoutError
                    ]
    builder.request :url_encoded
    if block.nil?
      builder.adapter Faraday.default_adapter
    else
      block.call(builder)
    end
  end
end

#deregister(uuid:, push_type:, device_id:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kakao_push/client.rb', line 28

def deregister(uuid:, push_type:, device_id:)
  body = {
    'uuid' => uuid,
    'device_id' => device_id,
    'push_type' => push_type
  }

  KakaoPush::Response.new(
    connection.post do |req|
      req.url url_for('/deregister')
      req.body = body
    end
  )
end

#register(uuid:, push_type:, device_id:, push_token:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kakao_push/client.rb', line 12

def register(uuid:, push_type:, device_id:, push_token:)
  body = {
    'uuid' => uuid,
    'device_id' => device_id,
    'push_type' => push_type,
    'push_token' => push_token
  }

  KakaoPush::Response.new(
    connection.post do |req|
      req.url url_for('/register')
      req.body = body
    end
  )
end

#send(uuids: [], apns:, gcm:, bypass: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kakao_push/client.rb', line 52

def send(uuids: [], apns:, gcm:, bypass: false)
  body = {
    'uuids' => uuids.to_json,
    'push_message' => build_message(apns: apns, gcm: gcm).to_json,
    'bypass' => bypass
  }

  KakaoPush::Response.new(
    connection.post do |req|
      req.url url_for('/send')
      req.body = body
    end
  )
end

#tokens(uuid:) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/kakao_push/client.rb', line 43

def tokens(uuid:)
  KakaoPush::Response.new(
    connection.get do |req|
      req.url url_for('/tokens')
      req.params['uuid'] = uuid
    end
  )
end