lib/gzr/modules/user.rb in gazer-0.3.10 vs lib/gzr/modules/user.rb in gazer-0.3.11
- old
+ new
@@ -1,8 +1,8 @@
# The MIT License (MIT)
-# Copyright (c) 2018 Mike DeAngelo Looker Data Sciences, Inc.
+# Copyright (c) 2023 Mike DeAngelo Google, Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
@@ -22,106 +22,115 @@
# frozen_string_literal: true
module Gzr
module User
- def query_me(fields=nil)
- data = nil
+ def query_me(fields='id')
begin
- data = @sdk.me(fields ? {:fields=>fields} : nil )
+ @sdk.me(fields ? {:fields=>fields} : nil ).to_attrs
rescue LookerSDK::Error => e
say_error "Error querying me({:fields=>\"#{fields}\"})"
say_error e
raise
end
- data
end
- def query_user(id,fields=nil)
- data = nil
+ def query_user(id,fields='id')
begin
- data = @sdk.user(id, fields ? {:fields=>fields} : nil )
+ @sdk.user(id, fields ? {:fields=>fields} : nil ).to_attrs
+ rescue LookerSDK::NotFound => e
+ say_error "user(#{id}) not found"
+ say_error e
+ raise
rescue LookerSDK::Error => e
say_error "Error querying user(#{id},{:fields=>\"#{fields}\"})"
say_error e
raise
end
- data
end
def search_users(filter, fields=nil, sorts=nil)
req = {
- :per_page=>128
+ :limit=>64
}
req.merge!(filter)
req[:fields] = fields if fields
req[:sorts] = sorts if sorts
data = Array.new
- page = 1
- loop do
- begin
- req[:page] = page
- scratch_data = @sdk.search_users(req)
- rescue LookerSDK::ClientError => e
- say_error "Unable to get search_users(#{JSON.pretty_generate(req)})"
- say_error e
- raise
+ begin
+ loop do
+ page = @sdk.search_users(req).collect { |r| r.to_attrs }
+ data+=page
+ break unless page.length == req[:limit]
+ req[:offset] = (req[:offset] || 0) + req[:limit]
end
- break if scratch_data.length == 0
- page += 1
- data += scratch_data
+ rescue LookerSDK::NotFound => e
+ # do nothing
+ rescue LookerSDK::ClientError => e
+ say_error "Unable to get search_users(#{JSON.pretty_generate(req)})"
+ say_error e
+ raise
end
data
end
def query_all_users(fields=nil, sorts=nil)
req = {
- :per_page=>128
+ :limit=>64
}
req[:fields] = fields if fields
req[:sorts] = sorts if sorts
data = Array.new
- page = 1
- loop do
- begin
- req[:page] = page
- scratch_data = @sdk.all_users(req)
- rescue LookerSDK::ClientError => e
- say_error "Unable to get all_users(#{JSON.pretty_generate(req)})"
- say_error e
- raise
+ begin
+ loop do
+ page = @sdk.all_users(req).collect { |r| r.to_attrs }
+ data+=page
+ break unless page.length == req[:limit]
+ req[:offset] = (req[:offset] || 0) + req[:limit]
end
- break if scratch_data.length == 0
- page += 1
- data += scratch_data
+ rescue LookerSDK::NotFound => e
+ # do nothing
+ rescue LookerSDK::ClientError => e
+ say_error "Unable to get all_users(#{JSON.pretty_generate(req)})"
+ say_error e
+ raise
end
data
end
def update_user(id,req)
- data = nil
begin
- data = @sdk.update_user(id,req)
+ @sdk.update_user(id,req)&.to_attrs
+ rescue LookerSDK::NotFound => e
+ say_error "updating user(#{id},#{JSON.pretty_generate(req)}) not found"
+ say_error e
+ raise
rescue LookerSDK::Error => e
say_error "Error updating user(#{id},#{JSON.pretty_generate(req)})"
say_error e
raise
end
- data
end
def delete_user(id)
- data = nil
- req = id
begin
- data = @sdk.delete_user(req)
+ @sdk.delete_user(id)
+ rescue LookerSDK::NotFound => e
+ say_error "delete user(#{id},#{JSON.pretty_generate(req)}) not found"
+ say_error e
+ raise
rescue LookerSDK::Error => e
- say_error "Error deleting user(#{id},#{JSON.pretty_generate(req)})"
+ say_error "Error deleting user(#{id})"
say_error e
raise
end
- data
+ end
+
+ def trim_user(data)
+ data.select do |k,v|
+ (keys_to_keep('create_user') + [:id]).include? k
+ end
end
end
end