Class: Libgss::Network

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

API_VERSION =
"1.0.0".freeze
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”。

  • :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)

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

  • :device_type_cd (Integer)

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

  • :client_version (String)

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



62
63
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
# File 'lib/libgss/network.rb', line 62

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"
  @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]

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

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



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

def auth_token
  @auth_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#client_versionObject

Returns the value of attribute client_version.



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

def client_version
  @client_version
end

#consumer_keyObject

Returns the value of attribute consumer_key.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



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

def consumer_secret
  @consumer_secret
end

#device_type_cdObject

Returns the value of attribute device_type_cd.



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

def device_type_cd
  @device_type_cd
end

#ignore_oauth_nonceObject

Returns the value of attribute ignore_oauth_nonce.



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

def ignore_oauth_nonce
  @ignore_oauth_nonce
end

#oauth_nonceObject

Returns the value of attribute oauth_nonce.



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

def oauth_nonce
  @oauth_nonce
end

#oauth_timestampObject

Returns the value of attribute oauth_timestamp.



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

def oauth_timestamp
  @oauth_timestamp
end

#platformObject

Returns the value of attribute platform.



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

def platform
  @platform
end

#player_idObject

Returns the value of attribute player_id.



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

def player_id
  @player_id
end

#player_infoObject

Returns the value of attribute player_info.



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

def player_info
  @player_info
end

#public_asset_url_prefixObject

Returns the value of attribute public_asset_url_prefix.



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

def public_asset_url_prefix
  @public_asset_url_prefix
end

#public_asset_url_suffixObject

Returns the value of attribute public_asset_url_suffix.



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

def public_asset_url_suffix
  @public_asset_url_suffix
end

#signature_keyObject (readonly)

Returns the value of attribute signature_key.



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

def signature_key
  @signature_key
end

#ssl_base_urlObject (readonly)

Returns the value of attribute ssl_base_url.



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

def ssl_base_url
  @ssl_base_url
end

#ssl_disabledObject (readonly)

Returns the value of attribute ssl_disabled.



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

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の文字列



178
179
180
181
182
183
# File 'lib/libgss/network.rb', line 178

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

#ignore_signature_key?Boolean

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

Returns:

  • (Boolean)

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



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

def ignore_signature_key?
  @ignore_signature_key
end

#inspectObject



91
92
93
94
95
# File 'lib/libgss/network.rb', line 91

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 = "config/app_garden.yml") ⇒ Object

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

Parameters:

  • path (String) (defaults to: "config/app_garden.yml")

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

Returns:

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



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/libgss/network.rb', line 161

def load_app_garden(path = "config/app_garden.yml")
  hash = YAML.load_file_with_erb(path)
  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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/libgss/network.rb', line 103

def (extra = {})
  attrs = @player_info.merge({ "player[id]" => player_id })
  extra.each{|k, v| attrs[ "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:



122
123
124
125
# File 'lib/libgss/network.rb', line 122

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

#new_action_requestLibgss::ActionRequest

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

Returns:



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

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

#new_async_action_requestLibgss::AsyncActionRequest

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

Returns:



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

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

#new_protected_asset_request(asset_path) ⇒ Libgss::AssetRequest

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

Returns:

  • (Libgss::AssetRequest)

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



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

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)

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



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

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)

    デバイス種別



190
191
192
193
194
# File 'lib/libgss/network.rb', line 190

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



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

def setup
  load_player_id
  
end

#uuid_genObject



196
197
198
# File 'lib/libgss/network.rb', line 196

def uuid_gen
  @uuid_gen ||= UUID.new
end