Class: Wip::User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(avatar_url: nil, best_streak: nil, completed_todos_count: nil, first_name: nil, id: nil, last_name: nil, streak: nil, streaking: nil, time_zone: nil, url: nil, username: nil, todos: []) ⇒ User

Returns a new instance of User.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wip/user.rb', line 49

def initialize(avatar_url: nil, best_streak: nil, completed_todos_count: nil, first_name: nil, id: nil, last_name: nil, streak: nil, streaking: nil, time_zone: nil, url: nil, username: nil, todos: [])
  @avatar_url = avatar_url
  @best_streak = best_streak
  @completed_todos_count = completed_todos_count
  @first_name = first_name
  @id = id
  @last_name = last_name
  @streak = streak
  @streaking = streaking
  @time_zone = time_zone
  @url = url
  @username = username
  @todos = todos
end

Instance Attribute Details

#avatar_urlObject

Returns the value of attribute avatar_url.



6
7
8
# File 'lib/wip/user.rb', line 6

def avatar_url
  @avatar_url
end

#best_streakObject

Returns the value of attribute best_streak.



6
7
8
# File 'lib/wip/user.rb', line 6

def best_streak
  @best_streak
end

#completed_todos_countObject

Returns the value of attribute completed_todos_count.



6
7
8
# File 'lib/wip/user.rb', line 6

def completed_todos_count
  @completed_todos_count
end

#first_nameObject

Returns the value of attribute first_name.



6
7
8
# File 'lib/wip/user.rb', line 6

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/wip/user.rb', line 6

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



6
7
8
# File 'lib/wip/user.rb', line 6

def last_name
  @last_name
end

#streakObject

Returns the value of attribute streak.



6
7
8
# File 'lib/wip/user.rb', line 6

def streak
  @streak
end

#streakingObject

Returns the value of attribute streaking.



6
7
8
# File 'lib/wip/user.rb', line 6

def streaking
  @streaking
end

#time_zoneObject

Returns the value of attribute time_zone.



6
7
8
# File 'lib/wip/user.rb', line 6

def time_zone
  @time_zone
end

#todosObject

Returns the value of attribute todos.



6
7
8
# File 'lib/wip/user.rb', line 6

def todos
  @todos
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/wip/user.rb', line 6

def url
  @url
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/wip/user.rb', line 6

def username
  @username
end

Class Method Details

.default_selection(todos: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wip/user.rb', line 8

def self.default_selection(todos: {})
  todos_selection = Wip::Todo.collection_query(**todos)
  %{
    avatar_url
    best_streak
    completed_todos_count
    first_name
    id
    last_name
    streak
    streaking
    time_zone
    #{todos_selection}
    url
    username
  }
end

.find(id = nil, username: nil, todos: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wip/user.rb', line 37

def self.find(id = nil, username: nil, todos: {})
  client = Wip::Client.new
  find_by = id.nil? ? "username: \"#{username}\"" : "id: #{id}"
  find_query = %{
    {
      user(#{find_by}) {#{default_selection(todos: todos)}}
    }
  }
  client.request find_query
  parse client.data("user")
end

.parse(data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wip/user.rb', line 64

def self.parse(data)
  new.tap do |user|
    data.each do |key, raw_value|
      value = case key
      when "id"
        raw_value.to_i
      when "todos"
        raw_value.collect { |v| Wip::Todo.parse v }
      else
        raw_value
      end
      user.send("#{key}=", value)
    end
  end
end

.viewer(todos: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/wip/user.rb', line 26

def self.viewer(todos: {})
  client = Wip::Client.new
  find_query = %{
    {
      viewer {#{default_selection(todos: todos)}}
    }
  }
  client.request find_query
  parse client.data("viewer")
end

Instance Method Details

#done_todosObject



99
100
101
# File 'lib/wip/user.rb', line 99

def done_todos
  todos.filter(&:done?)
end

#nameObject



80
81
82
# File 'lib/wip/user.rb', line 80

def name
  [first_name, last_name].join(" ")
end

#streak_iconObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/wip/user.rb', line 88

def streak_icon
  case streak
  when 0
    ""
  when 1..99
    "🔥"
  else
    "🌶"
  end
end

#tzObject



84
85
86
# File 'lib/wip/user.rb', line 84

def tz
  TZInfo::Timezone.get(time_zone)
end