Class: AUXHelper::Api

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

Overview

AUXHelper::Api makes it easier to use the Alpha UX backend REST API

Usage

api = AUXHelper::Api.new 'http://localhost:9393'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_server, api_key = nil) ⇒ Api

Returns a new instance of Api.



32
33
34
35
36
# File 'lib/aux_helper.rb', line 32

def initialize(api_server, api_key=nil)
  @api_server = api_server
  @api_key = api_key
  p " Running AUXHelper version #{AUXHelper::VERSION} with server #{api_server}"
end

Instance Attribute Details

#api_serverObject (readonly)

Returns the value of attribute api_server.



38
39
40
# File 'lib/aux_helper.rb', line 38

def api_server
  @api_server
end

Class Method Details

.validate_env_variables(env_vars) ⇒ Object

return true when all environment variables are set



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aux_helper.rb', line 41

def self.validate_env_variables(env_vars)
  p "Environment variables at runtime"
  env_vars.all? {|env_var|
    if ENV.has_key? env_var
      p "  #{env_var}=#{ENV[env_var]}"
      true
    else
      p "  #{env_var} is not set! The application will not start."
      false
    end
  }
end

Instance Method Details

#add_comment_tap(tap_id, comment_string) ⇒ Object



195
196
197
198
# File 'lib/aux_helper.rb', line 195

def add_comment_tap(tap_id, comment_string)
  return if are_invalid tap_id, comment_string
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :comments => [comment_string] } }
end

#add_drop(tap_id, options) ⇒ Object



124
125
126
127
# File 'lib/aux_helper.rb', line 124

def add_drop(tap_id, options)
  return if are_invalid tap_id, options
  post "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops", options
end

#add_tap(options) ⇒ Object



119
120
121
122
# File 'lib/aux_helper.rb', line 119

def add_tap(options)
  return if are_invalid options
  post "#{@api_server.to_s}/v0/taps", options
end

#auth_user(auth) ⇒ Object

authenticates the user with the API and returns their profile



135
136
137
138
139
140
141
142
143
# File 'lib/aux_helper.rb', line 135

def auth_user(auth)
  return if are_invalid auth
  # note - differences from 'post': (1) no .to_json (2) return response hash
  response_json = RestClient.post "#{@api_server.to_s}/v0/users/authenticate", auth, compute_headers(false)
  response = JSON.parse(response_json)
  response['response']
rescue => e
  log_error auth, e
end

#get_config(id) ⇒ Object

Returns hash with attributes passed in and readable_id.

Returns:

  • hash with attributes passed in and readable_id



221
222
223
224
# File 'lib/aux_helper.rb', line 221

def get_config(id)
  return if are_invalid id
  get "#{@api_server.to_s}/v0/configs/#{id}"
end

#get_drop(tap_id, drop_id) ⇒ Object

Description

Fetch the drop with the specified IDs

Example:

tap = api.get_tap('0123456789abcdef01234567', 5) #=> {"id" => 5, "tap_id" => "7654321fedcba9876543210", ...}

Returns nil on error.

Parameters:

  • tap_id

    string containing the mongo ID of an existing tap

  • drop_id

    string containing the mongo ID of an existing drop



113
114
115
116
117
# File 'lib/aux_helper.rb', line 113

def get_drop(tap_id, drop_id)
  #return if are_invalid_mongo_key tap_id
  return if are_invalid drop_id
  get "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id.to_i}"
end

#get_orgs_by_auth_domain(auth_domain) ⇒ Object

Find orgs associated with a domain



157
158
159
160
# File 'lib/aux_helper.rb', line 157

def get_orgs_by_auth_domain(auth_domain)
  return if are_invalid auth_domain
  get "#{@api_server.to_s}/v0/taps?auth_domain=#{auth_domain}"
end

#get_orgs_by_saml_issuer(issuer) ⇒ Object

Find orgs associated with an IdP



163
164
165
166
# File 'lib/aux_helper.rb', line 163

def get_orgs_by_saml_issuer(issuer)
  return if are_invalid issuer
  get "#{@api_server.to_s}/v0/taps?saml_issuer=#{issuer}"
end

#get_tap(tap_id) ⇒ Object

Description

Fetch the tap with the specified ID

will be returned as a hash

Example:

tap = api.get_tap('0123456789abcdef01234567') #=> {"id" => "0123456789abcdef01234567", ...}

Returns nil on error.

Parameters:

  • tap_id

    If valid mongo ID string for an existing tap, the tap



89
90
91
92
# File 'lib/aux_helper.rb', line 89

def get_tap(tap_id)
  #return if are_invalid_mongo_key tap_id
  get "#{@api_server.to_s}/v0/taps/#{tap_id}"
end

#get_tapsObject

Description

Fetch all recent taps from the Alpha UX API server

Example:

drops = api.get_taps #=> [{"id" => "0123456789abcdef01234567", ...}, ...]

Returns nil on error.

TODO - add limit parameter



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

def get_taps
  get "#{@api_server.to_s}/v0/taps"
end

#get_user(id) ⇒ Object



145
146
147
148
# File 'lib/aux_helper.rb', line 145

def get_user(id)
  #return if are_invalid_mongo_key id
  get "#{@api_server.to_s}/v0/users/#{id}"
end

#get_whitelisted_users(account_id) ⇒ Object



150
151
152
153
154
# File 'lib/aux_helper.rb', line 150

def get_whitelisted_users()
  return if are_invalid 
  # damn... it's broken: return if are_invalid_mongo_key account_id
  get "#{@api_server.to_s}/v0/users/whitelist/#{}"
end

#lookup_alias(element_alias) ⇒ Object



180
181
182
183
# File 'lib/aux_helper.rb', line 180

def lookup_alias(element_alias)
  return if are_invalid element_alias
  get "#{@api_server.to_s}/v0/aliases/#{element_alias}"
end

#put_config(id, config) ⇒ Object

Parameters:

  • config

    is a hash



227
228
229
230
# File 'lib/aux_helper.rb', line 227

def put_config(id, config)
  return if are_invalid id, config
  put "#{@api_server.to_s}/v0/configs/#{id}", config
end

#set_api_key(key) ⇒ Object



54
55
56
# File 'lib/aux_helper.rb', line 54

def set_api_key(key) 
  @api_key = key
end

#set_tags_tap(tap_id, tags) ⇒ Object

TAGS



210
211
212
213
# File 'lib/aux_helper.rb', line 210

def set_tags_tap(tap_id, tags)
  return if are_invalid tap_id, tags
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", {:feedback => { :set_tags => tags}}      
end

#signup_email(email_address) ⇒ Object

add email address to signup list returns true if successful



170
171
172
173
# File 'lib/aux_helper.rb', line 170

def (email_address)
  return if are_invalid email
  post "#{@api_server.to_s}/v0/register", { :address => email_address.to_s }
end

#unset_tags_tap(tap_id, tags) ⇒ Object



215
216
217
218
# File 'lib/aux_helper.rb', line 215

def unset_tags_tap(tap_id, tags)
  return if are_invalid tap_id, tags
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", {:feedback => { :unset_tags => tags}}      
end

#update_counters_tap(tap_id, counter_id, amount) ⇒ Object

COUNTERS



202
203
204
205
206
207
# File 'lib/aux_helper.rb', line 202

def update_counters_tap(tap_id, counter_id, amount)
  return if are_invalid tap_id, counter_id, amount
  counters_hash = Hash.new
  counters_hash[counter_id] = amount
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :counters => counters_hash } }
end

#update_drop(tap_id, drop_id, options) ⇒ Object



129
130
131
132
# File 'lib/aux_helper.rb', line 129

def update_drop(tap_id, drop_id, options)
  return if are_invalid tap_id, options
  put "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops/#{drop_id.to_s}", options
end

#update_drop_feedback(tap_id, drop_id, update) ⇒ Object



175
176
177
178
# File 'lib/aux_helper.rb', line 175

def update_drop_feedback(tap_id, drop_id, update)
  return if are_invalid tap_id, drop_id, update
  post "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id}/update", update
end

#update_location(drop_uri, longitude, latitude) ⇒ Object



185
186
187
188
# File 'lib/aux_helper.rb', line 185

def update_location(drop_uri, longitude, latitude)
  return if are_invalid drop_uri, longitude, latitude
  put drop_uri, { :location => [longitude, latitude] }
end

#update_tap(tap_id, options) ⇒ Object



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

def update_tap(tap_id, options)
  return if are_invalid options
  put "#{@api_server.to_s}/v0/taps/#{tap_id}", options
end

#update_user(user_id, body) ⇒ Object



190
191
192
193
# File 'lib/aux_helper.rb', line 190

def update_user(user_id, body)
  return if are_invalid user_id, body
  put "#{@api_server.to_s}/v0/users/#{user_id}", body
end