Module: Swagger

Defined in:
lib/swagger.rb,
lib/swagger/request.rb,
lib/swagger/version.rb,
lib/swagger/response.rb,
lib/swagger/configuration.rb

Defined Under Namespace

Classes: Configuration, Request, Response

Constant Summary collapse

VERSION =
"4.06.08"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

A Swagger configuration object. Must act like a hash and return sensible values for all Swagger configuration options. See Swagger::Configuration.



15
16
17
# File 'lib/swagger.rb', line 15

def configuration
  @configuration
end

.loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/swagger.rb', line 11

def logger
  @logger
end

.resourcesObject

Returns the value of attribute resources.



17
18
19
# File 'lib/swagger.rb', line 17

def resources
  @resources
end

Class Method Details

.authenticateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/swagger.rb', line 56

def authenticate
  return if Swagger.authenticated?
  
  if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
    raise ClientError, "Username and password are required to authenticate."
  end
  
  request = Swagger::Request.new(
    :get, 
    "account/authenticate/{username}", 
    :params => {
      :username => Swagger.configuration.username, 
      :password => Swagger.configuration.password
    }
  )
  
  response_body = request.response.body
  Swagger.configuration.auth_token = response_body['token']
end

.authenticated?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/swagger.rb', line 48

def authenticated?
  Swagger.configuration.auth_token.present?
end

.configure {|configuration| ... } ⇒ Object

Call this method to modify defaults in your initializers.

Examples:

Swagger.configure do |config|
  config.api_key = '1234567890abcdef'     # required
  config.username = 'wordlover'           # optional, but needed for user-related functions
  config.password = 'i<3words'            # optional, but needed for user-related functions
  config.format = 'json'                  # optional, defaults to 'json'
end

Yields:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/swagger.rb', line 29

def configure
  self.configuration ||= Configuration.new
  yield(configuration) if block_given?

  # Configure logger.  Default to use Rails
  self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))

  # remove :// from scheme
  configuration.scheme.sub!(/:\/\//, '')

  # remove http(s):// and anything after a slash
  configuration.host.sub!(/https?:\/\//, '')
  configuration.host = configuration.host.split('/').first

  # Add leading and trailing slashes to base_path
  configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
  configuration.base_path = "" if configuration.base_path == "/"
end

.de_authenticateObject



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

def de_authenticate
  Swagger.configuration.auth_token = nil
end