libgss 0.9.0 → 0.10.0

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: 7851a6116163ca2eb6bf2af61af08bebd7a59383
4
- data.tar.gz: ef4dcbd1330729b3d27d63a643b600abee0e5af9
3
+ metadata.gz: 04a7f3350eb992b75f94b53b265dcaccf33aa168
4
+ data.tar.gz: 3f915ba45f3bfbf49cc8313b7b61b465882d36f7
5
5
  SHA512:
6
- metadata.gz: 17085bf8a1bd853273cfcd6bce736eb5f64aba253d09161bfc99cd9f898acd463430143c6cc80cfde8517f1c5dd5c99ef901466371932e481fd850d2e4e8d7ec
7
- data.tar.gz: 6f09c4a77661bc89bb5981ddd1981c51df36f2df209060e8b3b1abf3eee18371af0842777470b85ee2f707f4c73512336d474703ce5e9d6961871993127b7d25
6
+ metadata.gz: 428061466d77c8e1764bd3c4bc21430c70d77ac31704a31bd4ab133188ddfbd631b072443c415ae3bff53f52910c3fe908875f179cc3a3de862260dfcc821ac3
7
+ data.tar.gz: e020cecda53511ceb7e739340b9d68d6aa864714408534a778f791134ea1aa43aa5b4afa172e5c7fe5cb322d6ed82701104806b585f9768f533757d12a334278
data/lib/libgss.rb CHANGED
@@ -20,6 +20,43 @@ module Libgss
20
20
 
21
21
  MAX_RETRY_COUNT = (ENV["LIBGSS_MAX_RETRY_COUNT"] || 10).to_i
22
22
 
23
+ class Error < StandardError; end
24
+ class ClientError < Error; end
25
+
26
+ class ErrorResponse < Error
27
+ class << self
28
+ def subclasses
29
+ @subclasses ||= []
30
+ end
31
+ def inherited(klass)
32
+ subclasses << klass
33
+ end
34
+ def build(res)
35
+ return nil if res.nil?
36
+ klass = subclasses.detect{|k| k.respond_to?(:match?) && k.match?(res)} || self
37
+ klass.new(res.status, res.content)
38
+ end
39
+ end
40
+
41
+ attr_reader :status
42
+ def initialize(status, message)
43
+ @status = status
44
+ super(message)
45
+ end
46
+ end
47
+
48
+ class InvalidResponse < ErrorResponse
49
+ end
50
+
51
+ class ServerBlockError < ErrorResponse
52
+ STATUS = 503
53
+ BODY = "api maintenance".freeze
54
+ def self.match?(res)
55
+ res.content.nil? ? false :
56
+ (res.status == STATUS) && (res.content.strip == BODY)
57
+ end
58
+ end
59
+
23
60
  class << self
24
61
 
25
62
  def with_retry(name)
@@ -70,10 +70,11 @@ module Libgss
70
70
 
71
71
  # レスポンスの処理を行います
72
72
  def process_response(res, req_type)
73
- case res.code.to_i
73
+ case res.status.to_i
74
74
  when 200..299 then # OK
75
75
  else
76
- raise Error, "failed to send #{req_type}: [#{res.code}] #{res.content}"
76
+ # raise Error, "failed to send #{req_type}: [#{res.status}] #{res.content}"
77
+ raise ErrorResponse.build(res)
77
78
  end
78
79
  verify_signature(res) do |content|
79
80
  begin
@@ -12,8 +12,7 @@ module Libgss
12
12
 
13
13
  class Network
14
14
 
15
- class Error < StandardError
16
- end
15
+ Error = Libgss::Error
17
16
 
18
17
  attr_reader :base_url
19
18
  attr_reader :ssl_base_url
@@ -52,6 +51,7 @@ module Libgss
52
51
  # @option options [String] :platform 接続先のGSSサーバの認証のプラットフォーム。デフォルトは"fontana"。
53
52
  # @option options [String] :api_version APIのバージョン。デフォルトは "1.0.0"
54
53
  # @option options [String] :player_id 接続に使用するプレイヤのID
54
+ # @option options [Hash] :player_info pf_player_info として格納されるはずの諸情報
55
55
  # @option options [String] :consumer_secret GSSサーバとクライアントの間で行う署名の検証に使用される文字列。
56
56
  # @option options [Boolean] :ignore_oauth_nonce OAuth認証時にoauth_nonceとoauth_timestampを使用しないかどうか。
57
57
  # @option options [String] :oauth_nonce OAuth認証のoauth_nonceパラメータ
@@ -105,13 +105,14 @@ module Libgss
105
105
  # @param [Hash] extra オプション
106
106
  # @option extra [Integer] :device_type デバイス種別
107
107
  # @option extra [Integer] :device_id デバイス識別子
108
- # @return [Boolean] ログインに成功した場合はtrue、失敗した場合はfalse
109
- def login(extra = {})
108
+ # @return [Integer, false, Error] 基本的にサーバが返したレスポンスのステータスを返しますが、200番台でも検証に失敗した場合などは、falseあるいは例外オブジェクトを返します。
109
+ def login_and_status(extra = {})
110
110
  @player_info[:id] = player_id
111
111
  @player_info.update(extra)
112
- attrs = @player_info.each_with_object({}){|(k,v), d| d[ "player[#{k}]" ] = v }
112
+ # attrs = @player_info.each_with_object({}){|(k,v), d| d[ "player[#{k}]" ] = v }
113
+ json = {"player" => @player_info}.to_json
113
114
  res = Libgss.with_retry("login") do
114
- @httpclient.post(login_url, attrs, req_headers)
115
+ @httpclient.post(login_url, json, req_headers)
115
116
  end
116
117
  process_json_response(res) do |obj|
117
118
  @player_id ||= obj["player_id"]
@@ -121,14 +122,31 @@ module Libgss
121
122
  end
122
123
  end
123
124
 
125
+ # GSSサーバに接続してログインの検証と処理を行います。
126
+ #
127
+ # @param [Hash] extra オプション
128
+ # @option extra [Integer] :device_type デバイス種別
129
+ # @option extra [Integer] :device_id デバイス識別子
130
+ # @return [Boolean] ログインに成功した場合はtrue、失敗した場合はfalse
131
+ def login(extra = {})
132
+ case login_and_status(extra)
133
+ when 200...300 then true
134
+ else false
135
+ end
136
+ end
137
+
124
138
  # GSSサーバに接続してログインの検証と処理を行います。
125
139
  #
126
140
  # @param [Hash] extra オプション
127
141
  # @see #login
128
142
  # @return ログインに成功した場合は自身のオブジェクト返します。失敗した場合はLibgss::Network::Errorがraiseされます。
129
143
  def login!(extra = {})
130
- raise Error, "Login Failure" unless login(extra)
131
- self
144
+ result = login_and_status(extra)
145
+ case result
146
+ when 200...300 then return self
147
+ when ErrorResponse then raise result
148
+ else raise Error, "Login Failure"
149
+ end
132
150
  end
133
151
 
134
152
  # @return [Boolean] コンストラクタに指定されたignore_signature_keyを返します
@@ -227,6 +245,7 @@ module Libgss
227
245
 
228
246
  def req_headers
229
247
  {
248
+ "Content-Type" => "application/json",
230
249
  "X-Device-Type" => device_type_cd,
231
250
  "X-Client-Version" => client_version,
232
251
  }
@@ -245,19 +264,21 @@ module Libgss
245
264
  end
246
265
 
247
266
  def process_json_response(res)
248
- case res.status
267
+ result = res.status
268
+ case result
249
269
  when 200...300 then # OK
250
- when 300...400 then return false # リダイレクト対応はしません
251
- when 400...500 then return false
252
- when 500...600 then return false
253
- else raise "invalid http status: #{res.status}"
270
+ when 300...600 then
271
+ return ErrorResponse.build(res)
272
+ else
273
+ raise InvalidResponse.new(result, "invalid http status")
254
274
  end
255
275
  begin
256
276
  obj = JSON.parse(res.content)
257
- return yield(obj)
277
+ yield(obj)
278
+ return result
258
279
  rescue JSON::ParserError => e
259
280
  $stderr.puts("\e[31m[#{e.class}] #{e.message}\n#{res.content}")
260
- return false
281
+ return e
261
282
  end
262
283
  end
263
284
 
@@ -1,3 +1,3 @@
1
1
  module Libgss
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -130,8 +130,7 @@ describe Libgss::Network do
130
130
  [300, 400, 500].map{|n| (1..10).map{|i| n + i} }.flatten.each do |status_code|
131
131
  context "status_code is #{status_code}" do
132
132
  before do
133
- res = double(:reponse)
134
- res.should_receive(:status).and_return(status_code)
133
+ res = double(:reponse, status: status_code, content: nil)
135
134
  HTTPClient.any_instance.should_receive(:post).and_return(res)
136
135
  end
137
136
  it_should_behave_like "Libgss::Network#login failure"
@@ -141,9 +140,7 @@ describe Libgss::Network do
141
140
  context "JSON parse Error" do
142
141
  before do
143
142
  $stderr.stub(:puts).with(an_instance_of(String)) # $stderrにメッセージが出力されます
144
- res = double(:reponse)
145
- res.stub(:status).and_return(200)
146
- res.should_receive(:content).twice.and_return("invalid JSON format string")
143
+ res = double(:reponse, status: 200, content: "invalid JSON format string")
147
144
  HTTPClient.any_instance.should_receive(:post).and_return(res)
148
145
  end
149
146
 
@@ -242,8 +239,7 @@ describe Libgss::Network do
242
239
  [300, 400, 500].map{|n| (1..10).map{|i| n + i} }.flatten.each do |status_code|
243
240
  context "status_code is #{status_code}" do
244
241
  before do
245
- res = double(:reponse)
246
- res.should_receive(:status).and_return(status_code)
242
+ res = double(:reponse, {status: status_code, content: nil})
247
243
  HTTPClient.any_instance.should_receive(:post).and_return(res)
248
244
  end
249
245
  it_should_behave_like "Libgss::Network#login failure"
@@ -253,9 +249,7 @@ describe Libgss::Network do
253
249
  context "JSON parse Error" do
254
250
  before do
255
251
  $stderr.stub(:puts).with(an_instance_of(String)) # $stderrにメッセージが出力されます
256
- res = double(:reponse)
257
- res.stub(:status).and_return(200)
258
- res.should_receive(:content).twice.and_return("invalid JSON format string")
252
+ res = double(:reponse, {status: 200, content: "invalid JSON format string"})
259
253
  HTTPClient.any_instance.should_receive(:post).and_return(res)
260
254
  end
261
255
 
@@ -351,13 +345,13 @@ describe Libgss::Network do
351
345
  it "with empty oauth_nonce" do
352
346
  proc{
353
347
  spec_with_network_options({oauth_nonce: ''})
354
- }.should raise_error(::Libgss::ActionRequest::Error)
348
+ }.should raise_error(::Libgss::ErrorResponse)
355
349
  end
356
350
  it "with old oauth_timestamp" do
357
351
  time = Time.local(2013,7,1,12,0,0)
358
352
  proc{
359
353
  spec_with_network_options({oauth_timestamp: time.to_i})
360
- }.should raise_error(::Libgss::ActionRequest::Error)
354
+ }.should raise_error(::Libgss::ErrorResponse)
361
355
  end
362
356
  end
363
357
  end
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe "サーバ閉塞中" do
5
+ before do
6
+ mock_res = mock(:res, {status: 503, content: "api maintenance"})
7
+ HTTPClient.any_instance.stub(:post).and_return(mock_res)
8
+ end
9
+
10
+ context "login" do
11
+ let(:network){ new_network }
12
+ it :login do
13
+ network.login_and_status.should be_a Libgss::ErrorResponse
14
+ end
15
+ it :login! do
16
+ expect{ network.login!}.to raise_error(Libgss::ServerBlockError)
17
+ end
18
+ end
19
+
20
+ context "action request" do
21
+ let(:network){ new_network }
22
+ it do
23
+ r = network.new_action_request
24
+ r.server_time
25
+ expect{ r.send_request }.to raise_error(Libgss::ServerBlockError)
26
+ end
27
+ end
28
+
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libgss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-25 00:00:00.000000000 Z
11
+ date: 2013-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -215,6 +215,7 @@ files:
215
215
  - spec/libgss/async_action_request_spec.rb
216
216
  - spec/libgss/network_spec.rb
217
217
  - spec/libgss/response_signatrue_spec.rb
218
+ - spec/libgss/server_block_spec.rb
218
219
  - spec/protected_assets/Icon.png
219
220
  - spec/public_assets/Default.png
220
221
  - spec/spec_helper.rb
@@ -269,6 +270,7 @@ test_files:
269
270
  - spec/libgss/async_action_request_spec.rb
270
271
  - spec/libgss/network_spec.rb
271
272
  - spec/libgss/response_signatrue_spec.rb
273
+ - spec/libgss/server_block_spec.rb
272
274
  - spec/protected_assets/Icon.png
273
275
  - spec/public_assets/Default.png
274
276
  - spec/spec_helper.rb