Class: Duolingo::User
- Inherits:
-
Object
- Object
- Duolingo::User
- Defined in:
- lib/duolingo/user.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #admin? ⇒ Boolean
- #created_at ⇒ Object
- #current_languages ⇒ Object
- #fields ⇒ Object
- #friends ⇒ Object
- #friends_stats ⇒ Object
- #full_name ⇒ Object
- #get_all_info ⇒ Object
-
#initialize(username) ⇒ User
constructor
A new instance of User.
- #languages_studied ⇒ Object
- #num_following ⇒ Object
- #number_of_followers ⇒ Object
- #points_for(language) ⇒ Object
- #rank ⇒ Object
- #stats_for_languages_studied ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(username) ⇒ User
Returns a new instance of User.
5 6 7 8 9 10 11 |
# File 'lib/duolingo/user.rb', line 5 def initialize(username) @username = username @response = Faraday.get("http://www.duolingo.com/users/#{username}") @data = JSON.parse(response.body) @all = data['language_data']['pt']['points_ranking_data'] end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
3 4 5 |
# File 'lib/duolingo/user.rb', line 3 def all @all end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/duolingo/user.rb', line 3 def data @data end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/duolingo/user.rb', line 3 def response @response end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
3 4 5 |
# File 'lib/duolingo/user.rb', line 3 def username @username end |
Instance Method Details
#admin? ⇒ Boolean
46 47 48 |
# File 'lib/duolingo/user.rb', line 46 def admin? data['admin'] end |
#created_at ⇒ Object
50 51 52 |
# File 'lib/duolingo/user.rb', line 50 def created_at data['created'].gsub("\n", "") end |
#current_languages ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/duolingo/user.rb', line 27 def current_languages = [] data['languages'].map do |lan| << lan['language_string'] if lan['current_learning'] end end |
#fields ⇒ Object
74 75 76 |
# File 'lib/duolingo/user.rb', line 74 def fields ['username','bio','id','num_following','cohort','num_followers', 'learning_language_string','created','contribution_points','gplus_id','twitter_id','admin','invites_left','location','fullname','avatar','ui_language'] end |
#friends ⇒ Object
78 79 80 81 82 |
# File 'lib/duolingo/user.rb', line 78 def friends all.select do |friend| friend unless friend['username'] == username.downcase end end |
#friends_stats ⇒ Object
84 85 86 87 88 |
# File 'lib/duolingo/user.rb', line 84 def friends_stats friends.map do |friend| {username: friend['username'], points: friend['points_data']['total']} end end |
#full_name ⇒ Object
58 59 60 |
# File 'lib/duolingo/user.rb', line 58 def full_name data['fullname'] end |
#get_all_info ⇒ Object
62 63 64 65 66 |
# File 'lib/duolingo/user.rb', line 62 def get_all_info fields.map do |field| data[field] end end |
#languages_studied ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/duolingo/user.rb', line 19 def languages_studied languages = [] data['languages'].map do |lan| languages << lan["language_string"] if lan["points"] > 0 end languages end |
#num_following ⇒ Object
54 55 56 |
# File 'lib/duolingo/user.rb', line 54 def num_following data['num_following'] end |
#number_of_followers ⇒ Object
42 43 44 |
# File 'lib/duolingo/user.rb', line 42 def number_of_followers data['num_followers'] end |
#points_for(language) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/duolingo/user.rb', line 35 def points_for(language) points = data['languages'].map do |lan| lan['points'] if lan['language_string'] == language.capitalize end.compact "#{username} has #{points.first} points for #{language}" end |
#rank ⇒ Object
90 91 92 93 94 |
# File 'lib/duolingo/user.rb', line 90 def rank data['language_data']['pt']['points_ranking_data'].detect do |user| user['username'] == username.downcase end['rank'] end |
#stats_for_languages_studied ⇒ Object
68 69 70 71 72 |
# File 'lib/duolingo/user.rb', line 68 def stats_for_languages_studied data['languages'].select do |lan| lan if lan['points'] > 0 end end |
#total_points ⇒ Object
13 14 15 16 17 |
# File 'lib/duolingo/user.rb', line 13 def total_points data['languages'].inject(0) do |sum, lan| sum + lan["points"] end end |