Class: Emojidex::Service::User
- Inherits:
-
Object
- Object
- Emojidex::Service::User
- 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
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#cache_path ⇒ Object
Returns the value of attribute cache_path.
-
#favorites ⇒ Object
Returns the value of attribute favorites.
-
#history ⇒ Object
Returns the value of attribute history.
-
#history_page ⇒ Object
Returns the value of attribute history_page.
-
#premium ⇒ Object
readonly
Returns the value of attribute premium.
-
#premium_exp ⇒ Object
readonly
Returns the value of attribute premium_exp.
-
#pro ⇒ Object
readonly
Returns the value of attribute pro.
-
#pro_exp ⇒ Object
readonly
Returns the value of attribute pro_exp.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #add_favorite(code) ⇒ Object
- #add_history(code) ⇒ Object
- #authorize(username, auth_token, sync_on_auth = true) ⇒ Object
- #authorized? ⇒ Boolean
- #clear_auth_data ⇒ Object
-
#initialize(opts = {}) ⇒ User
constructor
A new instance of User.
- #load(path = nil, auto_sync = true) ⇒ Object
- #login(user, password, sync_on_login = true) ⇒ Object
- #remove_favorite(code) ⇒ Object
- #save(path = nil) ⇒ Object
- #sync ⇒ Object
- #sync_favorites(limit = Emojidex::Defaults.limit, detailed = true) ⇒ Object
- #sync_history(limit = Emojidex::Defaults.limit, page = 0) ⇒ Object
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_token ⇒ Object (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_path ⇒ Object
Returns the value of attribute cache_path.
12 13 14 |
# File 'lib/emojidex/service/user.rb', line 12 def cache_path @cache_path end |
#favorites ⇒ Object
Returns the value of attribute favorites.
12 13 14 |
# File 'lib/emojidex/service/user.rb', line 12 def favorites @favorites end |
#history ⇒ Object
Returns the value of attribute history.
12 13 14 |
# File 'lib/emojidex/service/user.rb', line 12 def history @history end |
#history_page ⇒ Object
Returns the value of attribute history_page.
12 13 14 |
# File 'lib/emojidex/service/user.rb', line 12 def history_page @history_page end |
#premium ⇒ Object (readonly)
Returns the value of attribute premium.
11 12 13 |
# File 'lib/emojidex/service/user.rb', line 11 def premium @premium end |
#premium_exp ⇒ Object (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 |
#pro ⇒ Object (readonly)
Returns the value of attribute pro.
11 12 13 |
# File 'lib/emojidex/service/user.rb', line 11 def pro @pro end |
#pro_exp ⇒ Object (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 |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/emojidex/service/user.rb', line 11 def status @status end |
#username ⇒ Object (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_codes ⇒ Object
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 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 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 (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
68 69 70 |
# File 'lib/emojidex/service/user.rb', line 68 def @@auth_status_codes[@status] end |
#clear_auth_data ⇒ Object
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 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) ⇒ 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 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 |
#sync ⇒ Object
159 160 161 |
# File 'lib/emojidex/service/user.rb', line 159 def sync (@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 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 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 |