Class: Blend::Client::JuiceClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/blend/client/juice_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ JuiceClient

Returns a new instance of JuiceClient.



31
32
33
34
# File 'lib/blend/client/juice_client.rb', line 31

def initialize(opts={})
  @options = {}
  @options['auth_token'] = opts[:auth_token] if opts.include? :auth_token
end

Class Method Details

.get(url, *args) ⇒ Object

Wrap HHTParty’s get method so we can actually catch errors



11
12
13
14
15
16
17
18
# File 'lib/blend/client/juice_client.rb', line 11

def get( url, *args )
  response = super( url, *args )
  case response.code
    when 401
      raise Exceptions::AuthenticationFailure
  end
  return response
end

.post(url, *args) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/blend/client/juice_client.rb', line 20

def post( url, *args )
  response = super( url, *args )
  case response.code
    when 401
      raise Exceptions::AuthenticationFailure
  end
  return response
end

Instance Method Details

#activities(project_id, after, before = Time.now) ⇒ Object



215
216
217
218
219
# File 'lib/blend/client/juice_client.rb', line 215

def activities( project_id, after, before = Time.now )
  auth_token
  activities = self.class.get "/projects/#{project_id}/activities.json", {query: { after: after.to_i, before: before.to_i } }
  activities_stats activities, after, before # <-- so activities actually returns activities_stats? That's... weird.
end

#activities_stats(activities, after, before) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/blend/client/juice_client.rb', line 221

def activities_stats( activities, after, before )
  stats = { type: {}, 
            actors_activites: {}, 
            activities: activities, 
            after: after, 
            before: before, 
            active_tickets: {},
            closed_tickets: {}}

  activities.each do |x|
    x['happened_at'] = Time.parse x['happened_at']
  end.sort do |a,b|
    a['happened_at'] <=> b['happened_at']
  end.each do |a|
    actor = a['actor_identifier']
    type = a['activity_type']
    stats[:type][type] ||= []
    stats[:type][type] << a

    if( !(type =~ /^juice/) && !(type =~ /tweet/) )
      stats[:actors_activites][type] ||= {}
      stats[:actors_activites][type][actor] ||= []
      stats[:actors_activites][type][actor] << a
    end
  end

  stats
end

#add_feed(project_id, feed_type, key, environment = nil) ⇒ Object



186
187
188
189
# File 'lib/blend/client/juice_client.rb', line 186

def add_feed( project_id, feed_type, key, environment = nil )
  auth_token
  self.class.post "/projects/#{project_id}/feeds", {query: { feed_type: feed_type, key: key } }
end

#auth(provider) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/blend/client/juice_client.rb', line 207

def auth( provider )
  auths.select do |x|
    return x['token'] if x['provider'] == provider
  end

  nil
end

#auth_tokenObject



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/blend/client/juice_client.rb', line 377

def auth_token

  # Just return it if it's been set manually. This is accomplished when client is
  # instantiated with JuiceClient.new(auth_token: ---)
  if !@options['auth_token'].nil?
    self.class.default_params auth_token: @options['auth_token']
    return @options['auth_token']
  end

  if ENV['JUICE_AUTH_TOKEN'].nil?
    begin
      
    rescue Exceptions::AlreadyLoggedIn
    end
  else
    @options = { 'auth_token' => ENV['JUICE_AUTH_TOKEN'] }
  end
  self.class.default_params auth_token: @options['auth_token']
  @options['auth_token']
end

#authsObject



202
203
204
205
# File 'lib/blend/client/juice_client.rb', line 202

def auths
  auth_token
  @auths ||= self.class.get "/profile/authentications.json"
end

#check_credentials(username, password) ⇒ Object



330
331
332
333
334
# File 'lib/blend/client/juice_client.rb', line 330

def check_credentials( username, password )
  response = self.class.post '/auth', {body: {username: username, password: password}}
  raise Exceptions::LoginAuthenticationFailure if response['auth_token'].nil?
  @options = response.parsed_response if @options['auth_token'].nil?
end

#config_fileObject



313
314
315
# File 'lib/blend/client/juice_client.rb', line 313

def config_file
  "#{ENV['HOME']}/.juice.yml"
end

#create_project(name) ⇒ Object



47
48
49
50
51
# File 'lib/blend/client/juice_client.rb', line 47

def create_project( name )
  auth_token
  pp self.class.post "/organizations/1/projects", { query: { "project[name]" => name } }
  @projects = nil
end

#destroy_auth_tokenObject



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/blend/client/juice_client.rb', line 317

def destroy_auth_token
  _prev_options = @options
  begin
    return File.delete config_file
  rescue Errno::ENOENT
    raise Exceptions::AlreadyLoggedOut
  ensure
    @options = {}
  end
  _prev_options
end

#environments(id) ⇒ Object



191
192
193
194
# File 'lib/blend/client/juice_client.rb', line 191

def environments( id )
  auth_token
  self.class.get "/projects/#{id}/environments.json"
end

#feeds(id) ⇒ Object



181
182
183
184
# File 'lib/blend/client/juice_client.rb', line 181

def feeds( id )
  auth_token
  self.class.get "/projects/#{id}/feeds.json"
end

#github_team_checkObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/blend/client/juice_client.rb', line 295

def github_team_check
  github_client = Blend::Client.github_client

  teams = {}
  github_client.list_teams.each do |team|
    teams[team.name] = []
  end

  projects.each do |project|
    project['blend_config']['teams'].each do |x|
      teams[x] << project
    end
  end

  teams
end

#heroku_api(token = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/blend/client/juice_client.rb', line 250

def heroku_api( token = nil )
  auth_token
  ret = {}
  if( token )
    ret = self.class.post "/organizations/1/heroku/api_token.json", {query: {heroku_api_token: token}}
  else
    ret = self.class.get "/organizations/1/heroku/api_token.json"
  end
  ret['heroku_api_token']
end

#heroku_apps(id) ⇒ Object



196
197
198
199
200
# File 'lib/blend/client/juice_client.rb', line 196

def heroku_apps( id )
  auth_token
  f = feeds( id ).group_by{|x| x['feed_name']}
  (f['heroku'] || []).group_by { |x| x['environment']['name'].downcase}
end

#hipchat_api(token = nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
# File 'lib/blend/client/juice_client.rb', line 262

def hipchat_api( token = nil )
  auth_token
  ret = {}
  if( token )
    ret = self.class.post "/organizations/1/hipchat/auth_token.json", {query: {hipchat_auth_token: token}}
  else
    ret = self.class.get "/organizations/1/hipchat/auth_token.json"
  end
  ret['hipchat_auth_token']
end

#hipchat_checkObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/blend/client/juice_client.rb', line 273

def hipchat_check
  hipchat_client = Blend::Client.hipchat_client

  rooms = {}
  hipchat_client.rooms.each { |x| rooms[x['name']] = [] }

  projects.each do |project|
    room = project['blend_config']['hipchat_room']
    if room
      rooms[room] ||= []
      rooms[room] << project['name']
    end
  end

  rooms.keys.sort { |a,b| a <=> b }.collect do |room|
    if rooms[room].size == 0
      rooms[room] << "_unassigned_"
    end
    { room: room, projects: rooms[room] }
  end
end

#loginObject

debug_output $stderr



39
40
41
# File 'lib/blend/client/juice_client.rb', line 39

def 
  
end

#logoutObject



43
44
45
# File 'lib/blend/client/juice_client.rb', line 43

def logout
  destroy_auth_token
end

#lookup_user(query) ⇒ Object



125
126
127
128
# File 'lib/blend/client/juice_client.rb', line 125

def lookup_user( query )
  auth_token
  self.class.get "/users/lookup.json", { query: { query: query } }
end

#organization_users(id) ⇒ Object



140
141
142
143
# File 'lib/blend/client/juice_client.rb', line 140

def organization_users( id )
  auth_token
  self.class.get "/organizations/#{id}/users.json"
end

#organizationsObject



74
75
76
77
78
79
# File 'lib/blend/client/juice_client.rb', line 74

def organizations
  auth_token
  @organizations ||= self.class.get( "/organizations.json" ).each do |o|
    o['blend_config'] ||= {}
  end
end

#profileObject



53
54
55
56
# File 'lib/blend/client/juice_client.rb', line 53

def profile
  auth_token
  @projects ||= self.class.get( "/user.json" )
end

#project(id) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/blend/client/juice_client.rb', line 113

def project( id )
  return nil if id.nil?

  auth_token
  data = self.class.get "/projects/#{id}.json"

  data['blend_config'] ||= {}
  data['blend_config']['teams'] ||= []

  data
end

#project_add_hipchat(id, hipchat) ⇒ Object



175
176
177
178
179
# File 'lib/blend/client/juice_client.rb', line 175

def project_add_hipchat( id, hipchat )
  config = project_config( id )
  config['hipchat_room'] = hipchat
  project_config( id, config )
end

#project_add_team(id, github_team) ⇒ Object



168
169
170
171
172
173
# File 'lib/blend/client/juice_client.rb', line 168

def project_add_team( id, github_team )
  config = project_config( id )
  config['teams'] ||= []
  config['teams'] << github_team
  project_config( id, config )
end

#project_add_user(project_id, user_id) ⇒ Object



150
151
152
153
# File 'lib/blend/client/juice_client.rb', line 150

def project_add_user( project_id, user_id )
  auth_token
  self.class.post "/projects/#{project_id}/users/#{user_id}"
end

#project_config(id, config = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/blend/client/juice_client.rb', line 155

def project_config( id, config = nil )
  auth_token
  if( config.nil? )
    puts "Loading config"
    project = self.class.get "/projects/#{id}.json"
    project['blend_config'] || {}
  else
    puts "Setting config #{config}"
    self.class.put "/projects/#{id}.json", { query: { project: { blend_config: config } } }
    @projects = nil
  end
end

#project_id_from_name(name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/blend/client/juice_client.rb', line 102

def project_id_from_name( name )
  return name if name.to_s =~ /^[0-9]+$/

  projects.each do |project|
    return project['id'] if project['name'].projectize == name.projectize
  end

  puts "Couldn't find project #{name.projectize}"
  nil
end

#project_id_from_room(room) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/blend/client/juice_client.rb', line 92

def project_id_from_room( room )
  @project_ids_from_rooms ||= {}
  p = project_info_from_room( room )
  if p.nil?
    nil
  else
    @project_ids_from_rooms[room] ||= p['id']
  end
end

#project_info_from_room(room) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/blend/client/juice_client.rb', line 81

def project_info_from_room( room )
  auth_token
  if room =~ /[0-9]+_[^@]+@.*/
    room_name = Blend::Client.hipchat_client.room_name_from_xmpp_jid( room )
  else
    room_name = room
  end
  return nil if room_name.nil? or room_name.length==0
  projects.select{|x| (x['blend_config']['hipchat_room'].downcase rescue nil) == room_name.downcase}.first
end

#project_users(id) ⇒ Object



145
146
147
148
# File 'lib/blend/client/juice_client.rb', line 145

def project_users( id )
  auth_token
  self.class.get "/projects/#{id}/users.json"
end

#projectsObject



58
59
60
61
62
63
64
# File 'lib/blend/client/juice_client.rb', line 58

def projects
  auth_token
  @projects ||= self.class.get( "/projects.json" ).each do |x|
    x['blend_config'] ||= {}
    x['blend_config']['teams'] ||= []
  end
end

#query_loginObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/blend/client/juice_client.rb', line 337

def 

  begin
    response = read_auth_token
    @options = response

    # If it doesn't contain an auth_token, it's as good as not existing:

    raise Exceptions::AlreadyLoggedIn

  rescue Errno::ENOENT
    username = ask( "Username : " ) { |q| q.echo = true }
    password = ask( "Password : " ) { |q| q.echo = '.' }

    @options = check_credentials( username, password )

    write_auth_token( @options )
  end
end

#read_auth_tokenObject



358
359
360
361
362
363
364
365
366
# File 'lib/blend/client/juice_client.rb', line 358

def read_auth_token
  begin
    response = YAML.load( File.read( config_file ) )
    raise StandardError.new unless response['auth_token']
    response
  rescue StandardError
    raise Errno::ENOENT
  end
end

#search_users(query) ⇒ Object



130
131
132
133
# File 'lib/blend/client/juice_client.rb', line 130

def search_users( query )
  auth_token
  self.class.get "/users/search.json", { query: { query: query } }
end

#summaryObject



66
67
68
69
70
71
72
# File 'lib/blend/client/juice_client.rb', line 66

def summary
  auth_token
  @projects ||= self.class.get( "/projects/summary.json" ).each do |x|
    x['blend_config'] ||= {}
    x['blend_config']['teams'] ||= []
  end
end

#user_from_github_user(github) ⇒ Object



135
136
137
138
# File 'lib/blend/client/juice_client.rb', line 135

def user_from_github_user( github )
  @github_users ||= {}
  @github_users[github] ||= search_users(github).select { |x| x['github_handle'] == github }.first
end

#write_auth_token(options) ⇒ Object



369
370
371
372
373
374
# File 'lib/blend/client/juice_client.rb', line 369

def write_auth_token( options )
  File.open( config_file, "w" ) do |o|
    o.puts YAML.dump( options || {} )
  end
  return options
end