Class: Duolingo::User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#allObject (readonly)

Returns the value of attribute all.



3
4
5
# File 'lib/duolingo/user.rb', line 3

def all
  @all
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/duolingo/user.rb', line 3

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/duolingo/user.rb', line 3

def response
  @response
end

#usernameObject (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

Returns:

  • (Boolean)


46
47
48
# File 'lib/duolingo/user.rb', line 46

def admin?
  data['admin']
end

#created_atObject



50
51
52
# File 'lib/duolingo/user.rb', line 50

def created_at
  data['created'].gsub("\n", "")
end

#current_languagesObject



27
28
29
30
31
32
33
# File 'lib/duolingo/user.rb', line 27

def current_languages
  currently_studying = []
  data['languages'].map do |lan|
    currently_studying << lan['language_string'] if lan['current_learning']
  end
  currently_studying
end

#fieldsObject



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

#friendsObject



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_statsObject



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_nameObject



58
59
60
# File 'lib/duolingo/user.rb', line 58

def full_name
  data['fullname']
end

#get_all_infoObject



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_studiedObject



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_followingObject



54
55
56
# File 'lib/duolingo/user.rb', line 54

def num_following
  data['num_following']
end

#number_of_followersObject



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

#rankObject



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_studiedObject



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_pointsObject



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