Class: SteamMist::PseudoInterface::PseudoMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/steam_mist/pseudo_interface/pseudo_method.rb

Overview

A representation of a Steam Web API method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface, method, version = 1) ⇒ PseudoMethod

Initialize the method.

Parameters:

  • interface (PseudoInterface)

    the interface this method is a part of. Used to help build the RequestUri.

  • method (Symbol)

    the name of the method. See #api_name on how it’s used.

  • version (Numeric) (defaults to: 1)

    the version of the method. Can be found on the steam web api wiki.



40
41
42
43
44
45
46
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 40

def initialize(interface, method, version=1)
  @interface   = interface
  @name        = method
  @version     = version
  @arguments   = {}
  @cached      = false
end

Instance Attribute Details

#argumentsHash (readonly)

The arguments passed along with the method.

Returns:

  • (Hash)

    the arguments.



25
26
27
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 25

def arguments
  @arguments
end

#cachedBoolean (readonly)

Whether or not the connector is going to implement caching.

Returns:

  • (Boolean)


30
31
32
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 30

def cached
  @cached
end

#interfacePseudoInterface (readonly)

The interface that this method is a part of.

Returns:



20
21
22
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 20

def interface
  @interface
end

#nameSymbol (readonly)

The name of the method.

Returns:

  • (Symbol)

    the name.



15
16
17
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 15

def name
  @name
end

#versionNumeric (readonly)

The version of the method. Used for RequestUri.

Returns:

  • (Numeric)

    the version.



10
11
12
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 10

def version
  @version
end

Instance Method Details

#api_nameString

Turns the method name into the name the Steam Web API uses. It turns the method name from snake case to camel case if the first character isn’t uppercase. Otherwise, it uses the string form of it.

Returns:

  • (String)

    the Steam Web API compatible method name.



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 141

def api_name
  @_api_name ||= begin
    str = name.to_s

    if str[0] =~ /[A-Z]/
      str
    else
      str.gsub!(/_[a-z]/) { |m| m[1].upcase }
      str[0] = str[0].upcase
      str
    end
  end
end

#getConnector

Open up a connector for use. This is cached with this method.

Returns:

  • (Connector)

    the connector that will grab data.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 124

def get
  @_connector ||= begin
    connector = interface.session.connector.new(request_uri)

    if @cached
      connector.enable_caching @cached
    end

    connector
  end
end

#inspectString

Pretty inspection.

Returns:

  • (String)


169
170
171
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 169

def inspect
  "#<SteamMist::PseudoInterface::PseudoMethod #{interface.name}/#{name}>"
end

#request_uriRequestUri

This turns the method into its corresponding RequestUri. It uses #api_name and SteamMist::PseudoInterface#api_name to help create the uri.

Returns:



159
160
161
162
163
164
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 159

def request_uri
  @_request_uri ||= RequestUri.new :interface => interface.api_name,
    :method    => api_name,
    :arguments => interface.session.default_arguments.dup.merge(arguments),
    :version   => version
end

#with_arguments(new_arguments) ⇒ PseudoMethod

This merges the passed hash with the arguments.

Parameters:

  • new_arguments (Hash)

    the arguments to be added.

Returns:



52
53
54
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 52

def with_arguments(new_arguments)
  dup.with_arguments! new_arguments
end

#with_arguments!(new_arguments) ⇒ self

This merges the current arguments with the passed arguments. This modifies the instance it is called on. Invalidates the current connector.

Parameters:

  • new_arguments (Hash)

    the arguments to merge with.

Returns:

  • (self)


62
63
64
65
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 62

def with_arguments!(new_arguments)
  @arguments.merge!(new_arguments)
  reset_connector
end

#with_caching(path) ⇒ PseudoMethod

This makes sure the connector is set up to cache its results. Does not modify the connector until #get is called.

Parameters:

  • path (String)

    the path of the cache file.

Returns:



91
92
93
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 91

def with_caching(path)
  dup.with_caching!(path)
end

#with_caching!(path) ⇒ self

This modifies the current method to make sure the connector is set up to cache its results. Invalidates the current connector.

Parameters:

  • path (String)

    the path of the cache file.

Returns:

  • (self)


100
101
102
103
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 100

def with_caching!(path)
  @cached = path
  reset_connector
end

#with_version(new_version) ⇒ PseudoMethod

This sets the version.

Parameters:

  • new_version (Numeric)

    the version number to use.

Returns:



72
73
74
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 72

def with_version(new_version)
  dup.with_version!(new_version)
end

#with_version!(new_version) ⇒ self

Sets the version of the current method. This modifies the instance it is called on. Invalidates the current connector.

Parameters:

  • new_version (Numeric)

    the version to set to.

Returns:

  • (self)


81
82
83
84
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 81

def with_version!(new_version)
  @version = new_version
  reset_connector
end

#without_cachingPseudoMethod

This makes sure the connector is set up to not cache its results.

Returns:



108
109
110
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 108

def without_caching
  dup.without_caching!
end

#without_caching!self

This modifies the current method to make sure the connector is set up to not cache its results. Invalidates the current connector.

Returns:

  • (self)


116
117
118
119
# File 'lib/steam_mist/pseudo_interface/pseudo_method.rb', line 116

def without_caching!
  @cached = false
  reset_connector
end