Class: Libgss::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/libgss/network.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

PRODUCTION_HTTP_PORT =
80
PRODUCTION_HTTPS_PORT =
443
DEFAULT_HTTP_PORT =
(ENV['DEFAULT_HTTP_PORT' ] ||  80).to_i
DEFAULT_HTTPS_PORT =
(ENV['DEFAULT_HTTPS_PORT'] || 443).to_i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url_or_host, options = {}) ⇒ Network

Libgss::Networkのコンストラクタです。

Parameters:

  • base_url_or_host (String)

    接続先の基準となるURLあるいはホスト名

  • options (Hash) (defaults to: {})

    オプション

Options Hash (options):

  • :platform (String)

    接続先のGSSサーバの認証のプラットフォーム。デフォルトは“fontana”。

  • :api_version (String)

    APIのバージョン。デフォルトは “1.0.0”

  • :player_id (String)

    接続に使用するプレイヤのID

  • :consumer_secret (String)

    GSSサーバとクライアントの間で行う署名の検証に使用される文字列。

  • :ignore_oauth_nonce (Boolean)

    OAuth認証時にoauth_nonceとoauth_timestampを使用しないかどうか。

  • :oauth_nonce (String)

    OAuth認証のoauth_nonceパラメータ

  • :oauth_timestamp (Integer)

    OAuth認証のoauth_timestampパラメータ

  • :ssl_disabled (Boolean)

    SSLを無効にするかどうか。

  • :ignore_signature_key (Boolean)

    シグネチャキーによる署名を無視するかどうか

  • :skip_verifying_signature (Boolean)

    レスポンスのシグネチャキーによる署名の検証をスキップするかどうか

  • :device_type_cd (Integer)

    GSS/fontanaに登録されたデバイス種別

  • :client_version (String)

    GSS/fontanaに登録されたクライアントリリースのバージョン



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/libgss/network.rb', line 64

def initialize(base_url_or_host, options = {})
  @ssl_disabled = options.delete(:ssl_disabled)
  if base_url_or_host =~ URI.regexp
    @base_url = base_url_or_host.sub(/\/\Z/, '')
    uri = URI.parse(@base_url)
    @ssl_base_url = build_https_url(uri)
  else
    uri = URI::Generic.build({scheme: "http", host: base_url_or_host, port: DEFAULT_HTTP_PORT}.update(options))
    @base_url = uri.to_s
    @ssl_base_url = build_https_url(uri)
  end
  @ssl_base_url = @base_url if @ssl_disabled
  @platform  = options[:platform] || "fontana"
  @api_version = options[:api_version] || "1.0.0"
  @player_id = options[:player_id]
  @player_info = options[:player_info] || {}

  @consumer_secret = options[:consumer_secret] || ENV["CONSUMER_SECRET"]
  @ignore_signature_key = !!options[:ignore_signature_key]
  @ignore_oauth_nonce = !!options[:ignore_oauth_nonce]
  @oauth_nonce = options[:oauth_nonce] || nil
  @oauth_timestamp = options[:oauth_timestamp] || nil

  @device_type_cd = options[:device_type_cd]
  @client_version = options[:client_version]

  @skip_verifying_signature = options[:skip_verifying_signature]

  @httpclient = HTTPClient.new
  @httpclient.ssl_config.verify_mode = nil # 自己署名の証明書をOKにする
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



30
31
32
# File 'lib/libgss/network.rb', line 30

def api_version
  @api_version
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



22
23
24
# File 'lib/libgss/network.rb', line 22

def auth_token
  @auth_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



18
19
20
# File 'lib/libgss/network.rb', line 18

def base_url
  @base_url
end

#client_versionObject

Returns the value of attribute client_version.



37
38
39
# File 'lib/libgss/network.rb', line 37

def client_version
  @client_version
end

#consumer_keyObject

Returns the value of attribute consumer_key.



25
26
27
# File 'lib/libgss/network.rb', line 25

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



24
25
26
# File 'lib/libgss/network.rb', line 24

def consumer_secret
  @consumer_secret
end

#device_type_cdObject

Returns the value of attribute device_type_cd.



38
39
40
# File 'lib/libgss/network.rb', line 38

def device_type_cd
  @device_type_cd
end

#ignore_oauth_nonceObject

Returns the value of attribute ignore_oauth_nonce.



26
27
28
# File 'lib/libgss/network.rb', line 26

def ignore_oauth_nonce
  @ignore_oauth_nonce
end

#oauth_nonceObject

Returns the value of attribute oauth_nonce.



27
28
29
# File 'lib/libgss/network.rb', line 27

def oauth_nonce
  @oauth_nonce
end

#oauth_timestampObject

Returns the value of attribute oauth_timestamp.



28
29
30
# File 'lib/libgss/network.rb', line 28

def oauth_timestamp
  @oauth_timestamp
end

#platformObject

Returns the value of attribute platform.



31
32
33
# File 'lib/libgss/network.rb', line 31

def platform
  @platform
end

#player_idObject

Returns the value of attribute player_id.



32
33
34
# File 'lib/libgss/network.rb', line 32

def player_id
  @player_id
end

#player_infoObject

Returns the value of attribute player_info.



33
34
35
# File 'lib/libgss/network.rb', line 33

def player_info
  @player_info
end

#public_asset_url_prefixObject

Returns the value of attribute public_asset_url_prefix.



34
35
36
# File 'lib/libgss/network.rb', line 34

def public_asset_url_prefix
  @public_asset_url_prefix
end

#public_asset_url_suffixObject

Returns the value of attribute public_asset_url_suffix.



35
36
37
# File 'lib/libgss/network.rb', line 35

def public_asset_url_suffix
  @public_asset_url_suffix
end

#signature_keyObject (readonly)

Returns the value of attribute signature_key.



22
23
24
# File 'lib/libgss/network.rb', line 22

def signature_key
  @signature_key
end

#skip_verifying_signatureObject

Returns the value of attribute skip_verifying_signature.



40
41
42
# File 'lib/libgss/network.rb', line 40

def skip_verifying_signature
  @skip_verifying_signature
end

#ssl_base_urlObject (readonly)

Returns the value of attribute ssl_base_url.



19
20
21
# File 'lib/libgss/network.rb', line 19

def ssl_base_url
  @ssl_base_url
end

#ssl_disabledObject (readonly)

Returns the value of attribute ssl_disabled.



20
21
22
# File 'lib/libgss/network.rb', line 20

def ssl_disabled
  @ssl_disabled
end

Instance Method Details

#generate_device_id(options = {device_type: 1}) ⇒ String

device_idを生成します

Parameters:

  • options (Hash) (defaults to: {device_type: 1})

    オプション

Options Hash (options):

  • :device_type (Integer)

    デバイス種別

Returns:

  • (String)

    生成したUUIDの文字列



197
198
199
200
201
202
# File 'lib/libgss/network.rb', line 197

def generate_device_id(options = {device_type: 1})
  result = uuid_gen.generate
  player_info.update(options)
  player_info[:device_id] = result
  result
end

#httpclient_for_actionObject



219
220
221
222
223
# File 'lib/libgss/network.rb', line 219

def httpclient_for_action
  @httpclient_for_action ||=
    @ignore_signature_key ? @httpclient :
    HttpClientWithSignatureKey.new(@httpclient, self)
end

#ignore_signature_key?Boolean

Returns コンストラクタに指定されたignore_signature_keyを返します.

Returns:

  • (Boolean)

    コンストラクタに指定されたignore_signature_keyを返します



134
135
136
# File 'lib/libgss/network.rb', line 134

def ignore_signature_key?
  @ignore_signature_key
end

#inspectObject



96
97
98
99
100
# File 'lib/libgss/network.rb', line 96

def inspect
  r = "#<#{self.class.name}:#{self.object_id} "
  fields = (instance_variables - [:@httpclient]).map{|f| "#{f}=#{instance_variable_get(f).inspect}"}
  r << fields.join(", ") << ">"
end

#load_app_garden(path = nil) ⇒ Object

Returns 成功した場合自身のオブジェクトを返します。.

Parameters:

  • path (String) (defaults to: nil)

    対象となるapp_garden.ymlへのパス。デフォルトは “config/app_garden.yml” あるいは “config/app_garden.yml.erb”

Returns:

  • 成功した場合自身のオブジェクトを返します。

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/libgss/network.rb', line 172

def load_app_garden(path = nil)
  path ||= Dir.glob("config/app_garden.yml*").first
  raise ArgumentError, "file not found config/app_garden.yml* at #{Dir.pwd}" unless path

  # hash = YAML.load_file_with_erb(path, binding: binding) # tengine_supportが対応したらこんな感じで書きたい
  erb = ERB.new(IO.read(path))
  erb.filename = path
  text = erb.result(binding) # Libgss::FontanaをFontanaとしてアクセスできるようにしたいので、このbindingの指定が必要です
  hash = YAML.load(text)

  self.consumer_secret = hash["consumer_secret"]
  if platform = hash["platform"]
    name = (platform["name"] || "").strip
    unless name.empty?
      self.platform = name
    end
  end
  self
end

#login(extra = {}) ⇒ Boolean

GSSサーバに接続してログインの検証と処理を行います。

Parameters:

  • extra (Hash) (defaults to: {})

    オプション

Options Hash (extra):

  • :device_type (Integer)

    デバイス種別

  • :device_id (Integer)

    デバイス識別子

Returns:

  • (Boolean)

    ログインに成功した場合はtrue、失敗した場合はfalse



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/libgss/network.rb', line 108

def (extra = {})
  @player_info[:id] = player_id
  @player_info.update(extra)
  attrs = @player_info.each_with_object({}){|(k,v), d| d[ "player[#{k}]" ] = v }
  res = Libgss.with_retry("login") do
    @httpclient.post(, attrs, req_headers)
  end
  process_json_response(res) do |obj|
    @player_id ||= obj["player_id"]
    @auth_token = obj["auth_token"]
    @signature_key = obj["signature_key"]
    !!@auth_token && !!@signature_key
  end
end

#login!(extra = {}) ⇒ Object

GSSサーバに接続してログインの検証と処理を行います。

Parameters:

  • extra (Hash) (defaults to: {})

    オプション

Returns:

  • ログインに成功した場合は自身のオブジェクト返します。失敗した場合はLibgss::Network::Errorがraiseされます。

Raises:

See Also:



128
129
130
131
# File 'lib/libgss/network.rb', line 128

def login!(extra = {})
  raise Error, "Login Failure" unless (extra)
  self
end

#new_action_requestLibgss::ActionRequest

Returns アクション用リクエストを生成して返します.

Returns:



151
152
153
# File 'lib/libgss/network.rb', line 151

def new_action_request
  ActionRequest.new(self, action_url, req_headers)
end

#new_async_action_requestLibgss::AsyncActionRequest

Returns 非同期アクション用リクエストを生成して返します.

Returns:



156
157
158
# File 'lib/libgss/network.rb', line 156

def new_async_action_request
  AsyncActionRequest.new(self, async_action_url, async_result_url, req_headers)
end

#new_protected_asset_request(asset_path) ⇒ Libgss::AssetRequest

Returns 保護付きアセットを取得するリクエストを生成して返します.

Returns:

  • (Libgss::AssetRequest)

    保護付きアセットを取得するリクエストを生成して返します



166
167
168
# File 'lib/libgss/network.rb', line 166

def new_protected_asset_request(asset_path)
  AssetRequest.new(@httpclient, protected_asset_url(asset_path), req_headers)
end

#new_public_asset_request(asset_path) ⇒ Libgss::AssetRequest

Returns 公開アセットを取得するリクエストを生成して返します.

Returns:

  • (Libgss::AssetRequest)

    公開アセットを取得するリクエストを生成して返します



161
162
163
# File 'lib/libgss/network.rb', line 161

def new_public_asset_request(asset_path)
  AssetRequest.new(@httpclient, public_asset_url(asset_path), req_headers)
end

#set_device_id(device_id, options = {device_type: 1}) ⇒ Object

device_idを設定します

Parameters:

  • device_id (String)

    デバイスID

  • options (Hash) (defaults to: {device_type: 1})

    オプション

Options Hash (options):

  • :device_type (Integer)

    デバイス種別



209
210
211
212
213
# File 'lib/libgss/network.rb', line 209

def set_device_id(device_id, options = {device_type: 1})
  if player_info[:device_id] = device_id
    player_info.update(options)
  end
end

#setupObject

load_player_id メソッドをオーバーライドした場合に使用することを想定しています。それ以外の場合は使用しないでください。



145
146
147
148
# File 'lib/libgss/network.rb', line 145

def setup
  load_player_id
  
end

#skip_verifying_signature?Boolean

Returns コンストラクタに指定されたskip_verifying_signatureを返します.

Returns:

  • (Boolean)

    コンストラクタに指定されたskip_verifying_signatureを返します



139
140
141
# File 'lib/libgss/network.rb', line 139

def skip_verifying_signature?
  @skip_verifying_signature
end

#uuid_genObject



215
216
217
# File 'lib/libgss/network.rb', line 215

def uuid_gen
  @uuid_gen ||= UUID.new
end