hsquare 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc918c686257ac4d8b60bcc048478af856fc2c5d
4
- data.tar.gz: 384c6efac79fedb378ff6cc33cc8b4f577e90059
3
+ metadata.gz: a6accecebc76281dd41587f399dcbbf5da0a7716
4
+ data.tar.gz: 76d95e3e9a90d33098b110812b735a97365e7351
5
5
  SHA512:
6
- metadata.gz: 9cb102be70277076f0b11bf91538e0f68eb61e17f25df3ba74df3e461c1ac2a0e3d03d3edbdfd54b333b42a755a62bc9c3a00a25b4360df13d52c6b0973e2dc0
7
- data.tar.gz: 81ada066c79ce2686a76b73b3e8994e0fb0d76b68d8b0bd1133f3a60e8fbabb8d8c1f4ea62805b7b7beb2dc538d305629061ff87eddfba4fe5f8314b6c242a4e
6
+ metadata.gz: b3ec2cb67d65ea50ee6e4a3cfc90a0fc5de57595729d8a75abc40ef3dd95ddc37c40be588052eacf3c21180f69e2c9f7100d2fc7bdd67ad77b96a60f0cc3f93b
7
+ data.tar.gz: f719ef6fab4063965563dbf9b9a453895caadd65f7e3c8d5f4a8bbf567b372ed240e6fa9c1e3bc36c12117ab5f6a60366e9dfa2fcf4ac5662465751ee1a2f224
data/README.md CHANGED
@@ -41,6 +41,26 @@ end
41
41
  Admin 키는 외부에 노출되지 않도록 보안에 주의를 기울여야 합니다. 환경 변수
42
42
  등으로 소스 컨트롤에서 분리하여 설정하도록 처리하는 것을 추천합니다.
43
43
 
44
+ #### 여러 애플리케이션 동시에 사용하기
45
+
46
+ 어떤 클라이언트 애플리케이션의 패키징이 여러 가지로 달라질 수 있는 경우에
47
+ 여러 애플리케이션을 등록하고 사용할 수 있습니다. 이러한 경우 아래와 같이
48
+ 해당 애플리케이션들의 Admin key를 설정합니다.
49
+
50
+ ```ruby
51
+ # In config/initializers/hsquare.rb
52
+
53
+ Hsquare.config do |config|
54
+ config.application(:main) do |application|
55
+ application.admin_key = 'Main 애플리케이션의 Admin 키'
56
+ end
57
+
58
+ config.application(:beta) do |application|
59
+ application.admin_key = 'Beta 애플리케이션의 Admin 키'
60
+ end
61
+ end
62
+ ```
63
+
44
64
  ### 푸시 알림
45
65
 
46
66
  #### 기기의 푸시 토큰 등록
@@ -50,10 +70,11 @@ Admin 키는 외부에 노출되지 않도록 보안에 주의를 기울여야
50
70
 
51
71
  ```ruby
52
72
  device = Hsquare::Device.new(
53
- id: (애플리케이션 내 기기 ID),
54
- user_id: (애플리케이션 사용자 ID),
55
- token: '푸시 토큰',
56
- type: '(apns 또는 gcm)'
73
+ id: 'device-1', # 애플리케이션 내 기기 ID
74
+ user_id: 1, # 애플리케이션 사용자 ID
75
+ token: 'apns-push-token', # 푸시 토큰
76
+ type: 'apns' # apns 또는 gcm
77
+ application: nil # 애플리케이션 종류, 주어지지 않은 경우 첫 (혹은 default) 애플리케이션
57
78
  )
58
79
  ```
59
80
 
@@ -61,7 +82,7 @@ device = Hsquare::Device.new(
61
82
  있습니다.
62
83
 
63
84
  ```ruby
64
- device.register # Open Platform API 호출됩니다.
85
+ device.register # 기기를 등록하는 Open Platform API 호출합니다.
65
86
  ```
66
87
 
67
88
  클라이언트에서 푸시 토큰을 등록하도록 처리되어 있다면 이 과정은 거치지 않아도
@@ -1,6 +1,7 @@
1
1
  require 'httparty'
2
2
 
3
3
  require 'hsquare/version'
4
+ require 'hsquare/application'
4
5
  require 'hsquare/configuration'
5
6
  require 'hsquare/client'
6
7
  require 'hsquare/device'
@@ -31,14 +32,27 @@ module Hsquare
31
32
  #
32
33
  # Returns nothing.
33
34
  def self.apply(configuration)
34
- Hsquare::Client::Admin.admin_key = configuration.admin_key if configuration.admin_key
35
-
36
35
  if configuration.http_proxy
37
36
  http_proxy_uri = URI.parse(configuration.http_proxy)
38
37
 
39
38
  Hsquare::Client.http_proxy http_proxy_uri.host, http_proxy_uri.port
40
39
  end
41
40
 
41
+ configuration.applications.each(&:refresh_admin_client)
42
+
42
43
  @configuration = configuration
43
44
  end
45
+
46
+ # Public: Finds application with given label.
47
+ #
48
+ # label - Label of the applcation.
49
+ #
50
+ # Returns registered Hsquare::Application object.
51
+ def self.application(label = nil)
52
+ if label
53
+ @configuration.applications.detect { |application| application.label == label.to_sym }
54
+ else
55
+ @configuration.default_application
56
+ end
57
+ end
44
58
  end
@@ -0,0 +1,40 @@
1
+ module Hsquare
2
+ # Public: Application registered on developers.kakao.com
3
+ class Application
4
+ # Public: Distinctive label of the application.
5
+ attr_accessor :label
6
+
7
+ # Public: Admin access key of the application.
8
+ attr_accessor :admin_key
9
+
10
+ # Public: Initializes new application object.
11
+ #
12
+ # attributes - Attributes for the new application.
13
+ # :label - Label of the application.
14
+ # :admin_key - Admin API key for the application.
15
+ #
16
+ # Returns newly initialized application object.
17
+ def initialize(attributes = {})
18
+ @label = attributes[:label]
19
+ @admin_key = attributes[:admin_key]
20
+ end
21
+
22
+ # Public: Client with admin key associated with the application.
23
+ #
24
+ # Returns client class for the application inherits Hsquare::Client.
25
+ def admin_client
26
+ @admin_client ||= Class.new(Hsquare::Client::Admin).tap do |klass|
27
+ klass.admin_key = admin_key if admin_key
28
+ end
29
+ end
30
+
31
+ # Public: Clears previously cached admin client.
32
+ #
33
+ # Returns newly created admin client.
34
+ def refresh_admin_client
35
+ @admin_client = nil
36
+
37
+ admin_client
38
+ end
39
+ end
40
+ end
@@ -1,10 +1,48 @@
1
1
  module Hsquare
2
2
  # Public: Configuration object for Hsquare.
3
3
  class Configuration
4
- # Public: Admin access key of Kakao application.
5
- attr_accessor :admin_key
6
-
7
4
  # Public: HTTP proxy URI.
8
5
  attr_accessor :http_proxy
6
+
7
+ # Public: Sets admin key for default application.
8
+ #
9
+ # admin_key - Admin key to set.
10
+ #
11
+ # Returns newly set admin key.
12
+ def admin_key=(admin_key)
13
+ default_application.admin_key = admin_key
14
+ end
15
+
16
+ # Public: Retrieves default application.
17
+ #
18
+ # Returns Hsquare::Application object for default application.
19
+ def default_application
20
+ if @default_application
21
+ @default_application
22
+ elsif @applications && !@applications.empty?
23
+ @default_application ||= @applications.first
24
+ else
25
+ @default_application ||= Hsquare::Application.new
26
+ end
27
+ end
28
+
29
+ # Public: Retrieves list of registered applications.
30
+ #
31
+ # Returns Array of registered applications.
32
+ def applications
33
+ @applications ||= [default_application]
34
+ end
35
+
36
+ # Public: Adds a new application.
37
+ #
38
+ # Returns nothing.
39
+ def application(label = nil, &block)
40
+ new_application = Hsquare::Application.new(label: label)
41
+
42
+ @default_application ||= new_application
43
+ applications.push(new_application) unless applications.include?(new_application)
44
+
45
+ yield new_application
46
+ end
9
47
  end
10
48
  end
@@ -16,11 +16,13 @@ module Hsquare
16
16
  # Public: Fetch devices for given user.
17
17
  #
18
18
  # user_or_user_id - User or ID of user.
19
+ # options - Further options
20
+ # :application - Label of the application.
19
21
  #
20
22
  # Returns Array of HSquare::Device objects retrieved from the server.
21
- def self.find_by_user_id(user_or_user_id)
23
+ def self.find_by_user_id(user_or_user_id, options = {})
22
24
  user_id = user_or_user_id.respond_to?(:id) ? user_or_user_id.id : user_or_user_id
23
- response = Hsquare::Client::Admin.get('/v1/push/tokens', query: { uuid: user_id })
25
+ response = Hsquare.application(options[:application]).admin_client.get('/v1/push/tokens', query: { uuid: user_id })
24
26
 
25
27
  response.parsed_response.map do |device_response|
26
28
  new(device_response.symbolize_keys)
@@ -65,13 +67,14 @@ module Hsquare
65
67
  @user_id = attributes[:user_id] || attributes[:uuid]
66
68
  @token = attributes[:token] || attributes[:push_token]
67
69
  @type = attributes[:type] || attributes[:push_type]
70
+ @application = attributes[:application]
68
71
  end
69
72
 
70
73
  # Public: Registers the device via Kakao API.
71
74
  #
72
75
  # Returns Number of days before invalidation.
73
76
  def register
74
- Hsquare::Client::Admin.post('/v1/push/register', body: {
77
+ client.post('/v1/push/register', body: {
75
78
  uuid: user_id, device_id: id, push_type: type, push_token: token.gsub(/\s/, '')
76
79
  })
77
80
  end
@@ -80,9 +83,18 @@ module Hsquare
80
83
  #
81
84
  # Returns Number of days before invalidation.
82
85
  def unregister
83
- Hsquare::Client::Admin.post('/v1/push/deregister', body: {
86
+ client.post('/v1/push/deregister', body: {
84
87
  uuid: user_id, device_id: id, push_type: type
85
88
  })
86
89
  end
90
+
91
+ protected
92
+
93
+ # Internal: Client to use for API request.
94
+ #
95
+ # Returns Hsquare::Client class for associated application.
96
+ def client
97
+ Hsquare.application(@application).admin_client
98
+ end
87
99
  end
88
100
  end
@@ -65,7 +65,9 @@ module Hsquare
65
65
  #
66
66
  # Returns nothing.
67
67
  def deliver
68
- Hsquare::Client::Admin.post('/v1/push/send', body: payload)
68
+ Hsquare.config.applications.each do |application|
69
+ application.admin_client.post('/v1/push/send', body: payload)
70
+ end
69
71
  end
70
72
 
71
73
  protected
@@ -1,3 +1,3 @@
1
1
  module Hsquare
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Hsquare::Application do
4
+ describe '#admin_client' do
5
+ let(:application) { Hsquare::Application.new }
6
+
7
+ context 'when admin key is set' do
8
+ let(:admin_key) { 'admin-key' }
9
+ let(:application) { Hsquare::Application.new admin_key: admin_key }
10
+
11
+ it { expect(application.admin_client.default_options).not_to be_empty }
12
+ it { expect(application.admin_client.default_options[:headers]).not_to be_empty }
13
+ it { expect(application.admin_client.default_options[:headers]['Authorization']).to eq("KakaoAK #{admin_key}") }
14
+ end
15
+
16
+ context 'when proxy is set' do
17
+ let(:configuration) { Hsquare::Configuration.new.tap { |c| c.http_proxy = 'http://proxy.com:80' } }
18
+
19
+ before { Hsquare.apply(configuration) }
20
+
21
+ it { expect(application.admin_client.default_options).not_to be_empty }
22
+ it { expect(application.admin_client.default_options[:http_proxyaddr]).to eq('proxy.com') }
23
+ end
24
+ end
25
+ end
@@ -5,6 +5,8 @@ RSpec.describe Hsquare::Client do
5
5
  describe '#admin_key=' do
6
6
  let(:admin_key) { 'admin-key' }
7
7
 
8
+ before { Hsquare::Client::Admin.admin_key = nil }
9
+
8
10
  it { expect { Hsquare::Client::Admin.admin_key = admin_key }.to change { Hsquare::Client::Admin.default_options } }
9
11
  it { expect { Hsquare::Client::Admin.admin_key = admin_key }.not_to change { Hsquare::Client.default_options } }
10
12
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Hsquare::Configuration do
4
+ describe '#application' do
5
+ let(:configuration) { Hsquare::Configuration.new }
6
+
7
+ context 'when it is first invocation' do
8
+ let(:admin_key) { 'adminkey' }
9
+
10
+ it { configuration.application { |a| a.admin_key = admin_key }; expect(configuration.default_application.admin_key).to eq(admin_key) }
11
+ end
12
+
13
+ context 'when it is second invocation' do
14
+ before { configuration.application { |a| a.admin_key = 'first' } }
15
+
16
+ let(:label) { :second_application }
17
+ let(:admin_key) { 'adminkey' }
18
+
19
+ it { configuration.application(label) { |a| a.admin_key = admin_key }; expect(configuration.default_application.admin_key).not_to eq(admin_key) }
20
+ it { configuration.application(label) { |a| a.admin_key = admin_key }; expect(configuration.applications.last.admin_key).to eq(admin_key) }
21
+ it { configuration.application(label) { |a| a.admin_key = admin_key }; expect(configuration.applications.last.label).to eq(label) }
22
+ end
23
+ end
24
+ end
@@ -1,11 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Hsquare::Device do
4
+ let(:client) { Hsquare.config.default_application.admin_client }
5
+
4
6
  describe '.find_by_user_id' do
5
7
  let(:user_id) { 123 }
6
8
  let(:mock_response) { double(parsed_response: []) }
7
9
 
8
- it { expect(Hsquare::Client::Admin).to receive(:get).with('/v1/push/tokens', query: { uuid: user_id }).and_return(mock_response); Hsquare::Device.find_by_user_id(user_id) }
10
+ it { expect(client).to receive(:get).with('/v1/push/tokens', query: { uuid: user_id }).and_return(mock_response); Hsquare::Device.find_by_user_id(user_id) }
9
11
  end
10
12
 
11
13
  describe '#initialize' do
@@ -35,13 +37,13 @@ RSpec.describe Hsquare::Device do
35
37
  let(:attributes) { { id: 1, user_id: 2, type: 'apns', token: 'token' } }
36
38
  let(:device) { Hsquare::Device.new(attributes) }
37
39
 
38
- it { expect(Hsquare::Client::Admin).to receive(:post).with('/v1/push/register', body: { uuid: device.user_id, device_id: device.id, push_type: device.type, push_token: device.token }); device.register }
40
+ it { expect(client).to receive(:post).with('/v1/push/register', body: { uuid: device.user_id, device_id: device.id, push_type: device.type, push_token: device.token }); device.register }
39
41
  end
40
42
 
41
43
  describe '#unregister' do
42
44
  let(:attributes) { { id: 1, user_id: 2, type: 'apns', token: 'token' } }
43
45
  let(:device) { Hsquare::Device.new(attributes) }
44
46
 
45
- it { expect(Hsquare::Client::Admin).to receive(:post).with('/v1/push/deregister', body: { uuid: device.user_id, device_id: device.id, push_type: device.type }); device.unregister }
47
+ it { expect(client).to receive(:post).with('/v1/push/deregister', body: { uuid: device.user_id, device_id: device.id, push_type: device.type }); device.unregister }
46
48
  end
47
49
  end
@@ -6,7 +6,13 @@ RSpec.describe Hsquare::Notification do
6
6
  let(:message) { 'message' }
7
7
  let(:notification) { Hsquare::Notification.new(recipient_id: recipient_id, message: message) }
8
8
 
9
- it { expect(Hsquare::Client::Admin).to receive(:post).with('/v1/push/send', body: { uuids: [recipient_id].to_json, push_message: { for_apns: { push_alert: true, message: message }, for_gcm: { delay_while_idle: false, custom_field: { message: message } } }.to_json }); notification.deliver }
9
+ it do
10
+ Hsquare.config.applications.each do |application|
11
+ expect(application.admin_client).to receive(:post).with('/v1/push/send', body: { uuids: [recipient_id].to_json, push_message: { for_apns: { push_alert: true, message: message }, for_gcm: { delay_while_idle: false, custom_field: { message: message } } }.to_json })
12
+ end
13
+
14
+ notification.deliver
15
+ end
10
16
  end
11
17
 
12
18
  describe '#recipient_id=' do
@@ -15,11 +15,29 @@ RSpec.describe Hsquare do
15
15
  end
16
16
 
17
17
  context 'when admin_key is set' do
18
+ before { Hsquare.apply(configuration) }
18
19
  before { configuration.admin_key = 'admin-key' }
19
20
 
20
21
  it { expect { Hsquare.apply(configuration) }.not_to change { Hsquare::Client.default_options } }
21
- it { expect { Hsquare.apply(configuration) }.to change { Hsquare::Client::Admin.admin_key } }
22
- it { expect { Hsquare.apply(configuration) }.to change { Hsquare::Client::Admin.default_options } }
22
+ it { expect { Hsquare.apply(configuration) }.not_to change { Hsquare::Client::Admin.default_options } }
23
+ it { expect { Hsquare.apply(configuration) }.to change { Hsquare.config.default_application.admin_client.default_options } }
23
24
  end
24
25
  end
26
+
27
+ describe '.application' do
28
+ let(:configuration) { Hsquare::Configuration.new }
29
+
30
+ before { configuration.application(:first) { |a| a.admin_key = 'firstkey' } }
31
+ before { configuration.application(:second) { |a| a.admin_key = 'secondkey' } }
32
+ before { Hsquare.apply(configuration) }
33
+
34
+ it { expect(Hsquare.application(:first)).not_to be_nil }
35
+ it { expect(Hsquare.application(:first).admin_key).to eq('firstkey') }
36
+ it { expect(Hsquare.application(:second)).not_to be_nil }
37
+ it { expect(Hsquare.application(:second).admin_key).to eq('secondkey') }
38
+ it { expect(Hsquare.application(:third)).to be_nil }
39
+ it { expect(Hsquare.application).not_to be_nil }
40
+ it { expect(Hsquare.application.label).to eq(:first) }
41
+ it { expect(Hsquare.application.admin_key).to eq('firstkey') }
42
+ end
25
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hsquare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inbeom Hwang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-12 00:00:00.000000000 Z
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -81,12 +81,15 @@ files:
81
81
  - Rakefile
82
82
  - hsquare.gemspec
83
83
  - lib/hsquare.rb
84
+ - lib/hsquare/application.rb
84
85
  - lib/hsquare/client.rb
85
86
  - lib/hsquare/configuration.rb
86
87
  - lib/hsquare/device.rb
87
88
  - lib/hsquare/notification.rb
88
89
  - lib/hsquare/version.rb
90
+ - spec/lib/hsquare/application_spec.rb
89
91
  - spec/lib/hsquare/client_spec.rb
92
+ - spec/lib/hsquare/configuration_spec.rb
90
93
  - spec/lib/hsquare/device_spec.rb
91
94
  - spec/lib/hsquare/notification_spec.rb
92
95
  - spec/lib/hsquare_spec.rb
@@ -117,7 +120,9 @@ signing_key:
117
120
  specification_version: 4
118
121
  summary: Client library for Kakao Developers Open Platform API
119
122
  test_files:
123
+ - spec/lib/hsquare/application_spec.rb
120
124
  - spec/lib/hsquare/client_spec.rb
125
+ - spec/lib/hsquare/configuration_spec.rb
121
126
  - spec/lib/hsquare/device_spec.rb
122
127
  - spec/lib/hsquare/notification_spec.rb
123
128
  - spec/lib/hsquare_spec.rb