Class: GoodData::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/gooddata/connection.rb

Constant Summary collapse

@@options =
{
  api_url: 'https://gooddata.com',
  etl_url: 'https://na1-di.gooddata.com',
  verbose: false,
  adapter: :net_http
}

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options) ⇒ Connection

Returns a new instance of Connection.


18
19
20
21
# File 'lib/gooddata/connection.rb', line 18

def initialize(username, password, options)
  @credentials = { login: username, password: password }
  @@options.merge!(options)
end

Instance Method Details

#build_faraday_connectionObject

Create new Faraday connection


102
103
104
105
106
107
# File 'lib/gooddata/connection.rb', line 102

def build_faraday_connection
  Faraday.new(url: @@options[:api_url], headers: {'Accept'=>'application/json;version=1','Content-Type'=>'application/json'}) do |faraday|
    faraday.response :logger if @@options[:verbose] # log response to STDOUT
    faraday.adapter  @@options[:adapter]            # make requests with specified adapter
  end
end

#cloud_upload(path, data) ⇒ Object

Upload file to GoodData Private Cloud

Parameters:

  • path (String)

    file path

  • data (String)

    file content as string


75
76
77
78
79
# File 'lib/gooddata/connection.rb', line 75

def cloud_upload(path, data)
  dav = Net::DAV.new(@@options[:etl_url])
  dav.credentials(*@credentials.values)
  dav.put_string(path, data)
end

#companySring

Get company name

Returns:

  • (Sring)

    your company name


94
95
96
97
# File 'lib/gooddata/connection.rb', line 94

def company
  establish_connection unless @company
  @company
end

#connectionObject

Establish and cache GoodData connection


26
27
28
# File 'lib/gooddata/connection.rb', line 26

def connection
  @connection ||= establish_connection
end

#delete(url) ⇒ Hash

Proxy DELETE request to connection

Parameters:

  • url (String)

    GoodData API url

Returns:

  • (Hash)

    Parsed JSON data


65
66
67
68
# File 'lib/gooddata/connection.rb', line 65

def delete(url)
  response = fault_safe {connection.delete(escape(url))}
  JSON.parse(response.body) rescue nil
end

#escape(url) ⇒ Object (private)


164
165
166
167
# File 'lib/gooddata/connection.rb', line 164

def escape(url)
  url.gsub!('{user-id}', @user_id.to_s)
  url.gsub('{company}', @company.to_s)
end

#establish_connectionObject (private)

Establish GoodData connection and set user_id instance variable


113
114
115
116
117
118
119
120
121
122
# File 'lib/gooddata/connection.rb', line 113

def establish_connection  # Build connection

  @connection = build_faraday_connection  # Get user profile

  profile = retrieve_profile  # Set instance variables

  @user_id = profile.split('/').last
  @company = JSON.parse(@connection.get(profile).body)['accountSetting']['companyName'].downcase
  @connection
end

#fault_safeFaraday::Response (private)

Fault safe request

Yield Returns:

  • (Faraday::Response)

    Faraday response

Returns:

  • (Faraday::Response)

    Faraday response


147
148
149
150
151
152
153
154
# File 'lib/gooddata/connection.rb', line 147

def fault_safe
  response = yield
  if response.status == 401
    establish_connection
    response = yield
  end
  return response
end

#get(url, params = {}) ⇒ Hash

Proxy GET request to connection

Parameters:

  • url (String)

    GoodData API url

Returns:

  • (Hash)

    Parsed JSON data


35
36
37
38
# File 'lib/gooddata/connection.rb', line 35

def get(url, params = {})
  response = fault_safe {connection.get(escape(url), params)}
  JSON.parse(response.body) rescue nil
end

#post(url, data) ⇒ Hash

Proxy POST request to connection

Parameters:

  • url (String)

    GoodData API url

  • data (Hash)

    JSON data to send in POST request

Returns:

  • (Hash)

    Parsed JSON data


46
47
48
# File 'lib/gooddata/connection.rb', line 46

def post(url, data)
  submit(url, data) {|url1,data1| connection.post(url1, data1) }
end

#put(url, data) ⇒ Hash

Proxy PUT request to connection

Parameters:

  • url (String)

    GoodData API url

  • data (Hash)

    JSON data to send in PUT request

Returns:

  • (Hash)

    Parsed JSON data


56
57
58
# File 'lib/gooddata/connection.rb', line 56

def put(url, data)
  submit(url, data){|url1,data1| connection.put(url1, data1) }
end

#retrieve_profileString (private)

Set cookies and get user profile

Returns:

  • (String)

    user id


128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gooddata/connection.rb', line 128

def retrieve_profile  # Get SST Token and user id

  response = @connection.post '/gdc/account/login', {postUserLogin: @credentials.merge(remember: 1)}.to_json
  profile = JSON.parse(response.body)['userLogin']['profile']  # Get TT Token

  response = @connection.get do |req|
    req.url '/gdc/account/token'
    req.headers['Cookie'] = response.headers['Set-Cookie']
  end  # Set TT Token for all connetion requests

  @connection.headers['Cookie'] = response.headers['Set-Cookie']
  return profile
end

#submit(url, data) ⇒ Object (private)


156
157
158
159
160
161
162
# File 'lib/gooddata/connection.rb', line 156

def submit(url, data)
  # Encode to JSON if it's not json already
  data = data.to_json unless data.is_a? String
  url = escape(url)
  response = fault_safe{ yield(url, data) }
  JSON.parse(response.body) rescue nil
end

#user_idString

Get user id

Returns:

  • (String)

    GoodData user id


85
86
87
88
# File 'lib/gooddata/connection.rb', line 85

def user_id
  establish_connection unless @user_id
  @user_id
end