lib/slacky/user.rb in slacky-0.1.4 vs lib/slacky/user.rb in slacky-0.2

- old
+ new

@@ -1,12 +1,17 @@ require 'json' module Slacky class User attr_accessor :username, :slack_id, :slack_im_id, :first_name, :last_name, :email, :timezone, :presence, :data + attr_writer :valid attr_reader :tz + def valid? + @valid + end + @@decorator = @@config = @@db = nil def self.decorator=(decorator) @@decorator = decorator end @@ -31,46 +36,67 @@ first_name varchar(64), last_name varchar(64), email varchar(128) not null, timezone varchar(256), presence varchar(64), + valid boolean not null default false, data jsonb not null ); SQL end + def self.invalidate_all_users + self.db.exec 'update users set valid = FALSE' + end + def self.find(user) + return nil unless user return user.map { |u| User.find u }.compact if user.is_a? Array - result = self.db.exec_params "select * from users where slack_id = $1", [ user ] + match = user.match(/^<@(.*)>$/) + id = ( match ? match[1] : user ) + result = self.db.exec_params "select * from users where slack_id = $1", [ id ] if result.ntuples == 0 - result = self.db.exec_params "select * from users where username = $1", [ user ] + username = ( user =~ /^@/ ? user.sub(/^@/, '') : user ) + result = self.db.exec_params "select * from users where username = $1", [ username ] end return nil if result.ntuples == 0 + hydrate(result)[0] + end - row = result[0] - user = self.new username: row['username'], - slack_id: row['slack_id'], - slack_im_id: row['slack_im_id'], - first_name: row['first_name'], - last_name: row['last_name'], - email: row['email'], - timezone: row['timezone'], - presence: row['presence'], - data: JSON.parse(row['data']) - user.extend @@decorator if @@decorator - user + def self.find_by_data(query) + result = self.db.exec "select * from users where data #{query}" + hydrate result end + def self.hydrate(result) + return [] if result.ntuples == 0 + result.map do |row| + user = self.new username: row['username'], + slack_id: row['slack_id'], + slack_im_id: row['slack_im_id'], + first_name: row['first_name'], + last_name: row['last_name'], + email: row['email'], + timezone: row['timezone'], + presence: row['presence'], + valid: row['valid'], + data: JSON.parse(row['data']) + user.extend @@decorator if @@decorator + user + end + end + def initialize(attrs={}) @username = attrs[:username] @slack_id = attrs[:slack_id] @slack_im_id = attrs[:slack_im_id] @first_name = attrs[:first_name] @last_name = attrs[:last_name] @email = attrs[:email] @timezone = attrs[:timezone] || "America/Los_Angeles" @presence = attrs[:presence] + @valid = attrs[:valid] @data = attrs[:data] || {} end def populate(member) @username = member.name @@ -81,14 +107,19 @@ @presence = member['presence'] @data = {} unless @data self end + def validate + @valid = true + self + end + def save User.db.exec_params "delete from users where slack_id = $1", [ @slack_id ] - User.db.exec_params "insert into users (username, slack_id, slack_im_id, first_name, last_name, email, timezone, presence, data) - values ($1, $2, $3, $4, $5, $6, $7, $8, $9)", - [ @username, @slack_id, @slack_im_id, @first_name, @last_name, @email, @timezone, @presence, JSON.dump(@data) ] + User.db.exec_params "insert into users (username, slack_id, slack_im_id, first_name, last_name, email, timezone, presence, valid, data) + values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", + [ @username, @slack_id, @slack_im_id, @first_name, @last_name, @email, @timezone, @presence, @valid, JSON.dump(@data) ] self end def reset @data = {}