Class: GameRocket::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/gamerocket/configuration.rb

Constant Summary collapse

API_VERSION =
"1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



46
47
48
49
50
# File 'lib/gamerocket/configuration.rb', line 46

def initialize(options = {})
  [:environment, :apiKey, :secretKey, :custom_user_agent, :logger].each do |attr|
    instance_variable_set "@#{attr}", options[attr]
  end
end

Class Attribute Details

.apiKey=(value) ⇒ Object (writeonly)

Sets the attribute apiKey

Parameters:

  • value

    the value to set the attribute apiKey to.



6
7
8
# File 'lib/gamerocket/configuration.rb', line 6

def apiKey=(value)
  @apiKey = value
end

.custom_user_agent=(value) ⇒ Object (writeonly)

Sets the attribute custom_user_agent

Parameters:

  • value

    the value to set the attribute custom_user_agent to.



6
7
8
# File 'lib/gamerocket/configuration.rb', line 6

def custom_user_agent=(value)
  @custom_user_agent = value
end

.loggerObject



42
43
44
# File 'lib/gamerocket/configuration.rb', line 42

def self.logger
  @logger ||=_default_logger
end

.secretKey=(value) ⇒ Object (writeonly)

Sets the attribute secretKey

Parameters:

  • value

    the value to set the attribute secretKey to.



6
7
8
# File 'lib/gamerocket/configuration.rb', line 6

def secretKey=(value)
  @secretKey = value
end

Instance Attribute Details

#apiKeyObject (readonly)

Returns the value of attribute apiKey.



8
9
10
# File 'lib/gamerocket/configuration.rb', line 8

def apiKey
  @apiKey
end

#secretKeyObject (readonly)

Returns the value of attribute secretKey.



8
9
10
# File 'lib/gamerocket/configuration.rb', line 8

def secretKey
  @secretKey
end

Class Method Details

._default_loggerObject



111
112
113
114
115
# File 'lib/gamerocket/configuration.rb', line 111

def self._default_logger
  logger = Logger.new(STDOUT)
  logger.level = Logger::INFO
  logger
end

.environment=(env) ⇒ Object



21
22
23
24
25
26
# File 'lib/gamerocket/configuration.rb', line 21

def self.environment=(env)
  unless [:development, :production].include?(env)
    raise ArgumentError, "#{env.inspect} is not a valid environment"
  end
  @environment = env
end

.expectant_reader(*attributes) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/gamerocket/configuration.rb', line 10

def self.expectant_reader(*attributes)
  attributes.each do |attribute|
    (class << self; self; end).send(:define_method, attribute) do
      attribute_value = instance_variable_get("@#{attribute}")
      raise ConfigurationError.new(attribute.to_s, "needs to be set") unless attribute_value
      attribute_value
    end
  end
end

.gatewayObject



28
29
30
# File 'lib/gamerocket/configuration.rb', line 28

def self.gateway
  GameRocket::Gateway.new(instantiate)
end

.instantiateObject



32
33
34
35
36
37
38
39
40
# File 'lib/gamerocket/configuration.rb', line 32

def self.instantiate
  config = new(
    :custom_user_agent => @custom_user_agent,
    :environment => environment,
    :logger => logger,
    :apiKey => apiKey,
    :secretKey => secretKey
  )
end

Instance Method Details

#api_versionObject



52
53
54
# File 'lib/gamerocket/configuration.rb', line 52

def api_version
  API_VERSION
end

#baseUrlObject



56
57
58
# File 'lib/gamerocket/configuration.rb', line 56

def baseUrl
  "#{protocol}://#{server}:#{port}/api/#{API_VERSION}"
end

#ca_fileObject



60
61
62
63
64
65
# File 'lib/gamerocket/configuration.rb', line 60

def ca_file
  case @environment
  when :production
    File.expand_path(File.join(File.dirname(_FILE_), "..", "ssl", "www_gamerocket_io.ca.crt" ))
  end
end

#httpObject



67
68
69
# File 'lib/gamerocket/configuration.rb', line 67

def http
  Http.new(self)
end

#inspectObject



117
118
119
# File 'lib/gamerocket/configuration.rb', line 117

def inspect
  super.gsub(/@secretKey=\".*\"/, '@secrectKey="[FILTERED]"')
end

#loggerObject



71
72
73
# File 'lib/gamerocket/configuration.rb', line 71

def logger
  @logger ||=self.class._default_logger
end

#portObject



75
76
77
78
79
80
81
82
# File 'lib/gamerocket/configuration.rb', line 75

def port
  case @environment
  when :development
    ENV['GATEWAY_PORT'] || 8180
  when :production
    443      
  end
end

#serverObject



88
89
90
91
92
93
94
95
# File 'lib/gamerocket/configuration.rb', line 88

def server
  case @environment
  when :development
    "localhost"
  when :production
    "www.gamerocket.io"
  end
end

#ssl?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'lib/gamerocket/configuration.rb', line 97

def ssl?
  case @environment
  when :development
    false
  when :production
    true
  end
end

#user_agentObject



106
107
108
109
# File 'lib/gamerocket/configuration.rb', line 106

def user_agent
  base_user_agent = "GameRocket Ruby Gem #{GameRocket::Version::String}"
  @custom_user_agent ? "#{base_user_agent} (#{@custom_user_agent}" : base_user_agent
end