Module: ItchRewards::CLI::Helper
- Included in:
- Commands::Games::List, Commands::Rewards::Automate, Commands::Rewards::List, Commands::Rewards::Update, Commands::Setup, Commands::Version
- Defined in:
- lib/itch_rewards/cli.rb
Instance Method Summary collapse
- #authenticated_client(options) ⇒ Object
- #authenticated_client!(options) ⇒ Object
- #cli ⇒ Object
- #color ⇒ Object
- #objects_to_table(objects) ⇒ Object
- #render_table(table) ⇒ Object
- #show_rewards(game) ⇒ Object
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() @authenticated_client ||= begin interactive = [:interactive] username = [:username] || -> { interactive ? cli.ask("Itch.io username:", required: true) : (cli.error("Username required but not provided"); exit 1) } password = [: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) } = [:cookies] ? [:cookie_path] : nil client = Itch.new(username: username, password: password, cookie_path: ) client.totp = totp client.login && client rescue Itch::AuthError => e cli.error(e.) nil end end |
#authenticated_client!(options) ⇒ Object
53 54 55 |
# File 'lib/itch_rewards/cli.rb', line 53 def authenticated_client!() authenticated_client() || exit(1) end |
#cli ⇒ Object
23 24 25 26 27 |
# File 'lib/itch_rewards/cli.rb', line 23 def cli @cli ||= begin TTY::Prompt.new end end |
#color ⇒ Object
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 |