Class: Emojidex::Service::User

Inherits:
Object
  • Object
show all
Defined in:
lib/emojidex/service/user.rb

Overview

User auth and user details

Constant Summary collapse

@@auth_status_codes =
{ none: false, failure: false,
unverified: false, verified: true,
loaded: false }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ User

Returns a new instance of User.



21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def auth_token
  @auth_token
end

#cache_pathObject

Returns the value of attribute cache_path.



12
13
14
# File 'lib/emojidex/service/user.rb', line 12

def cache_path
  @cache_path
end

#favoritesObject

Returns the value of attribute favorites.



12
13
14
# File 'lib/emojidex/service/user.rb', line 12

def favorites
  @favorites
end

#historyObject

Returns the value of attribute history.



12
13
14
# File 'lib/emojidex/service/user.rb', line 12

def history
  @history
end

#history_pageObject

Returns the value of attribute history_page.



12
13
14
# File 'lib/emojidex/service/user.rb', line 12

def history_page
  @history_page
end

#premiumObject (readonly)

Returns the value of attribute premium.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def premium
  @premium
end

#premium_expObject (readonly)

Returns the value of attribute premium_exp.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def premium_exp
  @premium_exp
end

#proObject (readonly)

Returns the value of attribute pro.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def pro
  @pro
end

#pro_expObject (readonly)

Returns the value of attribute pro_exp.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def pro_exp
  @pro_exp
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def status
  @status
end

#usernameObject (readonly)

Returns the value of attribute username.



11
12
13
# File 'lib/emojidex/service/user.rb', line 11

def username
  @username
end

Class Method Details

.auth_status_codesObject



17
18
19
# File 'lib/emojidex/service/user.rb', line 17

def self.auth_status_codes
  @@auth_status_codes
end

Instance Method Details

#add_favorite(code) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/emojidex/service/user.rb', line 68

def authorized?
  @@auth_status_codes[@status]
end

#clear_auth_dataObject



151
152
153
154
155
156
157
# 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) ⇒ Object



170
171
172
173
174
175
176
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/emojidex/service/user.rb', line 34

def (user, password,  = 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_favorites
    sync_history
  end
  true
end

#remove_favorite(code) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



163
164
165
166
167
168
# File 'lib/emojidex/service/user.rb', line 163

def save(path = nil)
  _set_cache_path(path)
  _save_user
  _save_favorites
  _save_history
end

#syncObject



159
160
161
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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