Module: ItchRewards::CLI::Helper

Instance Method Summary collapse

Instance Method Details

#authenticated_client(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/itch_rewards/cli.rb', line 33

def authenticated_client(options)
  @authenticated_client ||= begin
    interactive = options[:interactive]

    username = options[:username] || -> { interactive ? cli.ask("Itch.io username:", required: true) : (cli.error("Username required but not provided"); exit 1) }
    password = options[:password] || -> { interactive ? cli.mask("Itch.io password:", required: true) :  (cli.error("Password required but not provided"); exit 1) }
    totp = -> { interactive ? cli.mask("Enter your 2FA code", required: true) : (cli.error("Cannot enter totp code in non-interactive mode"); exit 1) }

    cookie_path = options[:cookies] ? options[:cookie_path] : nil
    
    client = Itch.new(username: username, password: password, cookie_path: cookie_path)
    client.totp = totp
    
    client. && client
  rescue Itch::AuthError => e
    cli.error(e.message)
    nil
  end
end

#authenticated_client!(options) ⇒ Object



53
54
55
# File 'lib/itch_rewards/cli.rb', line 53

def authenticated_client!(options)
  authenticated_client(options) || exit(1)
end

#cliObject



23
24
25
26
27
# File 'lib/itch_rewards/cli.rb', line 23

def cli
  @cli ||= begin
    TTY::Prompt.new
  end
end

#colorObject



29
30
31
# File 'lib/itch_rewards/cli.rb', line 29

def color
  @pastel ||= Pastel.new
end

#objects_to_table(objects) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/itch_rewards/cli.rb', line 57

def objects_to_table(objects)
  objects = Array(objects)
  return nil if objects.none?
  if objects.first.is_a? Hash
    headers = objects.first.keys.sort          
    data = objects.map {|v| v.values_at(*headers) }
    headers = headers.map(&:upcase)
  else
    fields = objects.first.instance_variables.sort
    data = objects.map do |object|
      fields.map {|k| object.instance_variable_get(k) }
    end
    headers = fields.map {|f| f.to_s[1..].upcase }
  end

  TTY::Table.new(headers, data)
end

#render_table(table) ⇒ Object



75
76
77
78
79
# File 'lib/itch_rewards/cli.rb', line 75

def render_table(table)
  return "No data" unless table

  table.render(:unicode, multiline: true, padding: [0,1], resize: false, border: { style: :green })
end

#show_rewards(game) ⇒ Object



81
82
83
84
85
# File 'lib/itch_rewards/cli.rb', line 81

def show_rewards(game)
  cli.say "Rewards for #{game.name} (id: #{game.id})"
  table = objects_to_table(game.rewards.list)
  cli.say render_table(table)
end