class Adash::Manager

Public Class Methods

new() click to toggle source
# File lib/adash.rb, line 11
def initialize
end

Public Instance Methods

sub_deregistrate(name) click to toggle source
# File lib/adash.rb, line 62
def sub_deregistrate(name)
  device = get_device_by_name(name)
  unless device
    puts "Device #{name} not found"
    return 5
  end
  client = create_client_from_device(device)
  resp = client.deregistrate_device
  if resp.instance_of?(AmazonDrs::Error)
    STDERR.puts resp.inspect
  else
    save_credentials_without_device_model(device['device_model'])
  end
  0
end
sub_init(name, device_model, is_test) click to toggle source
# File lib/adash.rb, line 14
def sub_init(name, device_model, is_test)
  serial = generate_serial(device_model)
  credentials = get_credentials
  authorized_devices = credentials['authorized_devices']
  hit = authorized_devices&.find_index { |d| d['device_model'] == device_model }
  if hit
    puts "Adash knows device what is #{device_model}."
    return 3
  end
  hit = authorized_devices&.find_index { |d| d['name'] == name }
  if hit
    puts "Adash knows device what is named #{name}."
    return 4
  end
  wi = Adash::WaitIndefinitely.new(device_model, serial)
  Signal.trap(:INT){ wi.shutdown }
  code = wi.get_code
  FileUtils.mkdir_p(File.expand_path('..', Adash::Config.credentials_path))
  new_device = {
    'name' => name,
    'device_model' => device_model,
    'serial' => serial,
    'authorization_code' => code,
    'redirect_uri' => wi.redirect_uri
  }
  new_device['is_test'] = true if is_test
  if credentials['authorized_devices']
    credentials['authorized_devices'] << new_device
  else
    credentials['authorized_devices'] = [new_device]
  end
  save_credentials(credentials)
  client = create_client_from_device(new_device)
  client.get_token
  0
end
sub_list() click to toggle source
# File lib/adash.rb, line 51
def sub_list
  credentials = get_credentials
  credentials['authorized_devices'].each do |device|
    puts "---- name: #{device['name']}"
    puts "* device_model: #{device['device_model']}"
    puts "  serial: #{device['serial']}"
    puts '  THIS DEVICE IS TEST PURCHASE MODE' if device['is_test']
  end
  0
end
sub_list_slot(name) click to toggle source
# File lib/adash.rb, line 78
def sub_list_slot(name)
  device = get_device_by_name(name)
  unless device
    puts "Device #{name} not found"
    return 5
  end
  client = create_client_from_device(device)
  resp = client.subscription_info
  index = 0
  resp.slots.each do |slot_id, available|
    puts "---- #{index}"
    puts "* slot_id: #{slot_id}"
    puts "  available: #{available}"
    index =+ 1
  end
  0
end
sub_replenish(name, slot_id) click to toggle source
# File lib/adash.rb, line 96
def sub_replenish(name, slot_id)
  device = get_device_by_name(name)
  unless device
    puts "Device #{name} not found"
    return 5
  end
  client = create_client_from_device(device)
  slot_id = select_slot_prompt(client) unless slot_id
  resp = client.replenish(slot_id)
  if resp.instance_of?(AmazonDrs::Replenish)
    puts "ERROR: #{resp.message}"
  else
    case resp.detail_code
    when 'STANDARD_ORDER_PLACED'
      puts 'Succeeded to order.'
    when 'ORDER_INPROGRESS'
      puts 'The order is in progress.'
    end
  end
  0
end

Private Instance Methods

create_client_from_device(device) click to toggle source
# File lib/adash.rb, line 169
def create_client_from_device(device)
  AmazonDrs::Client.new(device['device_model']) do |c|
    c.authorization_code = device['authorization_code']
    c.serial = device['serial']
    c.redirect_uri = device['redirect_uri']
    c.access_token = device['access_token']
    c.refresh_token = device['refresh_token']
    c.client_id = Adash::Config.client_id
    c.client_secret = Adash::Config.client_secret
    c.redirect_uri = "http://localhost:#{Adash::Config.redirect_port}/"
    c.on_new_token = proc { |access_token, refresh_token|
      credentials = get_credentials
      updated_device = get_device_from_credentials(credentials, device['device_model'])
      updated_device['access_token'] = access_token
      updated_device['refresh_token'] = refresh_token
      save_credentials_with_device(credentials, updated_device)
      device['device_model']
    }
  end
end
generate_serial(device_model) click to toggle source
# File lib/adash.rb, line 118
def generate_serial(device_model)
  orig = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
  random_suffix = (0...16).map { orig[rand(orig.size)] }.join
  "#{device_model}_#{Time.now.to_i}_#{random_suffix}"
end
get_credentials() click to toggle source
# File lib/adash.rb, line 125
def get_credentials
  if File.exist?(Adash::Config.credentials_path)
    credentials = YAML.load_file(Adash::Config.credentials_path)
  else
    { 'authorized_devices' => [] }
  end
end
get_device_by_name(name) click to toggle source
# File lib/adash.rb, line 191
def get_device_by_name(name)
  credentials = get_credentials
  hit = credentials['authorized_devices'].find_index { |d| d['name'] == name }
  if hit
    device = credentials['authorized_devices'][hit]
  else
    nil
  end
end
get_device_from_credentials(credentials, device_model) click to toggle source
# File lib/adash.rb, line 141
def get_device_from_credentials(credentials, device_model)
  i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device_model }
  if i
    credentials['authorized_devices'][i]
  else
    nil
  end
end
save_credentials(credentials) click to toggle source
# File lib/adash.rb, line 134
def save_credentials(credentials)
  open(Adash::Config.credentials_path, 'w') do |f|
    f.write(credentials.to_yaml)
  end
end
save_credentials_with_device(credentials, device) click to toggle source
# File lib/adash.rb, line 151
def save_credentials_with_device(credentials, device)
  i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device['device_model'] }
  if i
    credentials['authorized_devices'][i] = device
  else
    credentials['authorized_devices'] << device
  end
  save_credentials(credentials)
end
save_credentials_without_device_model(device_model) click to toggle source
# File lib/adash.rb, line 162
def save_credentials_without_device_model(device_model)
  credentials = get_credentials
  credentials['authorized_devices'] = credentials['authorized_devices'].delete_if { |d| d['device_model'] == device_model }
  save_credentials(credentials)
end
select_slot_prompt(client) click to toggle source
# File lib/adash.rb, line 215
def select_slot_prompt(client)
  resp = client.subscription_info
  slots = resp.slots.select{ |k, v| v }
  if slots.size == 1
    return slots.keys.first
  end
  loop do
    show_slots(slots)
    slot_num = Readline.readline('Select slot number> ')
    if (0..(slots.size - 1)).member?(slot_num.to_i)
      break slots.keys[slot_num.to_i]
    else
      puts "ERROR: #{slot_num} is out of Range"
    end
  end
end
show_slots(slots) click to toggle source
# File lib/adash.rb, line 202
def show_slots(slots)
  index = 0
  slots.each do |slot_id, available|
    puts
    puts "---- number: #{index}"
    puts "* slot_id: #{slot_id}"
    puts "  available: #{available}"
    puts
    index =+ 1
  end
end