Class: Rubyists::Linear::User

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/linear/models/user.rb

Overview

The User class represents a Linear user.

Constant Summary collapse

Base =
fragment('BaseUser', 'User') do
  id
  name
  email
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ User

Returns a new instance of User.



41
42
43
44
# File 'lib/linear/models/user.rb', line 41

def initialize(data)
  super(data)
  self.teams = data[:teams] if data.key? :teams
end

Class Method Details

.base_fragmentObject



21
22
23
24
25
26
27
28
# File 'lib/linear/models/user.rb', line 21

def self.base_fragment
  @with_teams = fragment('UserWithTeams', 'User') do
    ___ Base
    teams do
      nodes { ___ Team.base_fragment }
    end
  end
end

.meObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/linear/models/user.rb', line 30

def self.me(**)
  fragment = base_fragment
  q = query do
    viewer do
      ___ fragment
    end
  end
  data = Api.query(q)
  new data[:viewer]
end

Instance Method Details

#display(_options) ⇒ Object



101
102
103
104
105
# File 'lib/linear/models/user.rb', line 101

def display(_options)
  return printf("%s\n", to_s) if @teams.nil?

  printf "%<to_s>s (%<teams>s)\n", to_s:, teams: @teams.map(&:name).join(', ')
end

#issue_query(first) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/linear/models/user.rb', line 46

def issue_query(first)
  id = data[:id]
  query do
    user(id:) do
      assignedIssues(first:, filter: { completedAt: { null: true } }) do
        nodes do
          ___ Issue.base_fragment
        end
      end
    end
  end
end

#issues(limit: 50) ⇒ Object



59
60
61
62
63
64
# File 'lib/linear/models/user.rb', line 59

def issues(limit: 50)
  issue_data = Api.query(issue_query(limit))
  issue_data[:user][:assignedIssues][:nodes].map do |issue|
    Issue.new issue
  end
end

#team_query(first) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/linear/models/user.rb', line 66

def team_query(first)
  id = data[:id]
  query do
    user(id:) do
      teams(first:) do
        nodes { ___ Team::Base }
      end
    end
  end
end

#teams(limit: 50) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/linear/models/user.rb', line 88

def teams(limit: 50)
  return @teams if @teams

  team_data = Api.query(team_query(limit))
  @teams = team_data[:user][:teams][:nodes].map do |team|
    Team.new team
  end
end

#teams=(team_data) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
# File 'lib/linear/models/user.rb', line 77

def teams=(team_data)
  team_data.is_a?(Array) && @teams = team_data && return

  if team_data.is_a?(Hash)
    @teams = team_data[:nodes].map { |team| Team.new team }
    return
  end

  raise ArgumentError, "Don't know how to handle #{team_data.class}"
end

#to_sObject



97
98
99
# File 'lib/linear/models/user.rb', line 97

def to_s
  format('%<id>-20s: %<name>s <%<email>s>', id:, name:, email:)
end