Class: GoodData::Bricks::GoodDataMiddleware

Inherits:
Middleware show all
Defined in:
lib/gooddata/bricks/middleware/gooddata_middleware.rb

Overview

Connects to platform and enriches parameters with GoodData::Client

Constant Summary collapse

DEFAULT_PROTOCOL =
'https'
DEFAULT_HOSTNAME =
'secure.gooddata.com'

Instance Attribute Summary

Attributes inherited from Middleware

#app

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

#initialize, #load_defaults

Methods included from Utils

#returning

Constructor Details

This class inherits a constructor from GoodData::Bricks::Middleware

Class Method Details

.connect(server, verify_ssl, username, password, sst_token) ⇒ Object

rubocop:disable Metrics/ParameterLists



90
91
92
93
94
95
96
97
98
99
# File 'lib/gooddata/bricks/middleware/gooddata_middleware.rb', line 90

def connect(server, verify_ssl, username, password, sst_token) # rubocop:disable Metrics/ParameterLists
  if username.nil? || password.nil?
    puts "Connecting with SST to server #{server}"
    raise 'SST (SuperSecureToken) not present in params' if sst_token.nil?
    GoodData.connect(sst_token: sst_token, server: server, verify_ssl: verify_ssl)
  else
    puts "Connecting as #{username} to server #{server}"
    GoodData.connect(username, password, server: server, verify_ssl: verify_ssl)
  end
end

Instance Method Details

#call(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/gooddata/bricks/middleware/gooddata_middleware.rb', line 16

def call(params)
  # Convert possible jruby hash to plain hash
  params = params.to_hash

  # Transform keys
  params = GoodData::Helpers.stringify_keys(params)
  # params = GoodData::Helpers.symbolize_keys(params)

  # Set logger
  logger = params['GDC_LOGGER']
  GoodData.logger = logger

  # Connect Client
  protocol = params['CLIENT_GDC_PROTOCOL'] || DEFAULT_PROTOCOL
  hostname = params['CLIENT_GDC_HOSTNAME'] || DEFAULT_HOSTNAME
  server = "#{protocol}://#{hostname}"
  client = GoodDataMiddleware.connect(
    server,
    params['GDC_VERIFY_SSL'].to_b,
    params['GDC_USERNAME'],
    params['GDC_PASSWORD'],
    params['GDC_SST']
  )

  opts = params['development_client']
  if opts
    if opts['server']
      server = opts['server']
    else
      protocol = opts['protocol'] || DEFAULT_PROTOCOL
      hostname = opts['hostname'] || DEFAULT_HOSTNAME
      server = "#{protocol}://#{hostname}"
    end

    development_client = GoodDataMiddleware.connect(
      server,
      opts['verify_ssl'].to_b,
      opts['username'] || opts['login'] || opts['email'],
      opts['password'],
      opts['sst']
    )
  else
    development_client = client
  end

  new_params = {
    'GDC_GD_CLIENT' => client,
    'development_client' => development_client
  }

  if params['GDC_PROJECT_ID']
    new_params['gdc_project'] = GoodData.project = client.projects(params['GDC_PROJECT_ID'])
  end

  returning_value = @app.call(params.merge(new_params))

  # Try to disconnect client
  begin
    client.disconnect
  rescue
    puts 'Tried to disconnect client. Was unsuccessful. Proceeding anyway.'
  end

  # Try to disconnect development_client
  begin
    development_client.disconnect if development_client != client
  rescue
    puts 'Tried to disconnect development_client. Was unsuccessful. Proceeding anyway.'
  end

  returning_value
end