class Emojidex::Service::User

User auth and user details

Attributes

auth_token[R]
cache_path[RW]
favorites[RW]
history[RW]
history_page[RW]
premium[R]
premium_exp[R]
pro[R]
pro_exp[R]
status[R]
username[R]

Public Class Methods

auth_status_codes() click to toggle source
# File lib/emojidex/service/user.rb, line 17
def self.auth_status_codes
  @@auth_status_codes
end
new(opts = {}) click to toggle source
# File lib/emojidex/service/user.rb, line 21
def initialize(opts = {})
  clear_auth_data
  @status = :none
  @history = []
  @history_page = 0
  @favorites = Emojidex::Data::Collection.new
  if opts.key?(:cache_path)
    load(opts[:cache_path])
  elsif opts[:load_cache] == true
    load
  end
end

Public Instance Methods

add_favorite(code) click to toggle source
# File lib/emojidex/service/user.rb, line 87
def add_favorite(code)
  return false unless authorized?

  begin
    res = Transactor.post('users/favorites',
                          username: @username, auth_token: @auth_token,
                          emoji_code: Emojidex.escape_code(code))
  rescue Error::Unauthorized
    return false
  end
  return false if res.include?(:status) && res[:status] == 'emoji already in user favorites'
  @favorites.add_emoji([res])
  true
end
add_history(code) click to toggle source
# File lib/emojidex/service/user.rb, line 136
def add_history(code)
  return false unless authorized?

  begin
    result = Transactor.post('users/history',
                             username: @username, auth_token: @auth_token,
                             emoji_code: Emojidex.escape_code(code))
  rescue
    return false
  end

  _push_and_dedupe_history(result)
  true
end
authorize(username, auth_token, sync_on_auth = true) click to toggle source
# File lib/emojidex/service/user.rb, line 51
def authorize(username, auth_token, sync_on_auth = true)
  begin
    auth_response = Transactor.get('users/authenticate',
                                   username: username, token: auth_token)
  rescue Error::Unauthorized
    @status = :unverified
    return false
  end

  return false unless _process_auth_response(auth_response)
  if sync_on_auth
    sync_favorites
    sync_history
  end
  true
end
authorized?() click to toggle source
# File lib/emojidex/service/user.rb, line 68
def authorized?
  @@auth_status_codes[@status]
end
clear_auth_data() click to toggle source
# File lib/emojidex/service/user.rb, line 151
def clear_auth_data()
  @username = @auth_token = ''
  @pro = false
  @premium = false
  @pro_exp = nil
  @premium_exp = nil
end
load(path = nil, auto_sync = true) click to toggle source
# File lib/emojidex/service/user.rb, line 170
def load(path = nil, auto_sync = true)
  _set_cache_path(path)
  _load_user
  _load_favorites
  _load_history
  sync if auto_sync
end
login(user, password, sync_on_login = true) click to toggle source
# File lib/emojidex/service/user.rb, line 34
def login(user, password, sync_on_login = true)
  begin
    auth_response = Transactor.get('users/authenticate',
                                   user: user, password: password)
  rescue Error::Unauthorized
    @status = :unverified
    return false
  end

  return false unless _process_auth_response(auth_response)
  if sync_on_login
    sync_favorites
    sync_history
  end
  true
end
remove_favorite(code) click to toggle source
# File lib/emojidex/service/user.rb, line 102
def remove_favorite(code)
  return false unless authorized?

  begin
    res = Transactor.delete('users/favorites',
                            username: @username, auth_token: @auth_token,
                            emoji_code: Emojidex.escape_code(code))
  rescue Error::Unauthorized
    return false
  end
  return false if res.include?(:status) && res[:status] == 'emoji not in user favorites'
  @favorites.remove_emoji(code.to_sym)
  true
end
save(path = nil) click to toggle source
# File lib/emojidex/service/user.rb, line 163
def save(path = nil)
  _set_cache_path(path)
  _save_user
  _save_favorites
  _save_history
end
sync() click to toggle source
# File lib/emojidex/service/user.rb, line 159
def sync
  authorize(@username, @auth_token) && sync_favorites && sync_history
end
sync_favorites(limit = Emojidex::Defaults.limit, detailed = true) click to toggle source
# File lib/emojidex/service/user.rb, line 72
def sync_favorites(limit = Emojidex::Defaults.limit, detailed = true)
  return false unless authorized?

  begin
    res = Emojidex::Service::Collection.new(
      endpoint: 'users/favorites', limit: limit, detailed: detailed,
      username: @username, auth_token: @auth_token)
  rescue Error::Unauthorized
    return false
  end

  @favorites = res
  true
end
sync_history(limit = Emojidex::Defaults.limit, page = 0) click to toggle source
# File lib/emojidex/service/user.rb, line 117
def sync_history(limit = Emojidex::Defaults.limit, page = 0)
  return false unless authorized?

  page = @history_page + 1 if page == 0

  begin
    result = Transactor.get('users/history',
                            limit: limit, page: page,
                            username: @username, auth_token: @auth_token)
  rescue
    return false
  end

  return false unless (result.key?(:history) && result.key?(:meta))
  @history_page = result[:meta][:page]
  _merge_history(result[:history])
  true
end

Private Instance Methods

_load_favorites() click to toggle source
# File lib/emojidex/service/user.rb, line 240
def _load_favorites
  _save_favorites unless File.exist? "#{@cache_path}/favorites.json"
  json = IO.read("#{@cache_path}/favorites.json")
  @favorites = Emojidex::Service::Collection.new(
    emoji: JSON.parse(json, symbolize_names: true),
    auto_init: false)
end
_load_history() click to toggle source
# File lib/emojidex/service/user.rb, line 248
def _load_history
  _save_history unless File.exist? "#{@cache_path}/history.json"
  json = IO.read("#{@cache_path}/history.json")
  items = JSON.parse(json, symbolize_names: true)
  @history = []
  items.each { |item| @history << Emojidex::Service::HistoryItem.new(item[:emoji_code],
                                                                     item[:times_used],
                                                                     item[:last_used]) }
end
_load_user() click to toggle source
# File lib/emojidex/service/user.rb, line 227
def _load_user
  _save_user unless File.exist? "#{@cache_path}/user.json"
  json = IO.read("#{@cache_path}/user.json")
  user_info = JSON.parse(json, symbolize_names: true)
  @username = user_info[:username]
  @auth_token = user_info[:auth_token]
  @premium = user_info[:premium]
  @pro = user_info[:pro]
  @premium_exp = user_info[:premium_exp]
  @pro_exp = user_info[:pro_exp]
  @status = :loaded
end
_merge_history(history_delta = []) click to toggle source
# File lib/emojidex/service/user.rb, line 258
def _merge_history(history_delta = [])
  history_delta.each do |item|
    _push_and_dedupe_history(item)
  end
  _sort_history
end
_process_auth_response(auth_response) click to toggle source
# File lib/emojidex/service/user.rb, line 180
def _process_auth_response(auth_response)
  if auth_response[:auth_status] == 'verified'
    _set_verified_data(auth_response)
    return true
  elsif auth_response[:auth_status] == 'unverified'
    @status = :unverified
  else
    @status = :failure
  end
  clear_auth_data
  false
end
_push_and_dedupe_history(item) click to toggle source
# File lib/emojidex/service/user.rb, line 265
def _push_and_dedupe_history(item)
  @history.delete_if { |hi| hi.emoji_code == item[:emoji_code] }
  @history.unshift Emojidex::Service::HistoryItem.new(item[:emoji_code],
                                                      item[:times_used], item[:last_used])
end
_save_favorites() click to toggle source
# File lib/emojidex/service/user.rb, line 217
def _save_favorites
  File.open("#{@cache_path}/favorites.json", 'w') do |f|
    f.write @favorites.emoji.values.to_json
  end
end
_save_history() click to toggle source
# File lib/emojidex/service/user.rb, line 223
def _save_history
  File.open("#{@cache_path}/history.json", 'w') { |f| f.write @history.to_json }
end
_save_user() click to toggle source
# File lib/emojidex/service/user.rb, line 209
def _save_user
  user_info = { username: username, auth_token: auth_token,
                premium: premium, pro: pro,
                premium_exp: premium_exp, pro_exp: pro_exp
              }
  File.open("#{@cache_path}/user.json", 'w') { |f| f.write user_info.to_json }
end
_set_cache_path(path) click to toggle source
# File lib/emojidex/service/user.rb, line 203
def _set_cache_path(path)
  @cache_path ||= File.expand_path(path || Emojidex::Defaults.system_cache_path)
  FileUtils.mkdir_p(@cache_path)
  @cache_path
end
_set_verified_data(auth_response) click to toggle source
# File lib/emojidex/service/user.rb, line 193
def _set_verified_data(auth_response)
  @status = :verified
  @username = auth_response[:auth_user]
  @auth_token = auth_response[:auth_token]
  @pro = auth_response[:pro]
  @premium = auth_response[:premium]
  @pro_exp = auth_response[:pro_exp]
  @premium_exp = auth_response[:premium_exp]
end
_sort_history() click to toggle source
# File lib/emojidex/service/user.rb, line 271
def _sort_history
  @history.sort_by! {|h| -h.last_used.to_i}
end