Class: Flumtter::AccountSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/flumtter/app/core/account_selector.rb

Constant Summary collapse

@@account_list =
(Config[:accounts] ||= []).map{|a|Account.new(a)}

Class Method Summary collapse

Class Method Details

.listObject



23
24
25
# File 'lib/flumtter/app/core/account_selector.rb', line 23

def list
  @@account_list
end

.list_to_sObject



27
28
29
# File 'lib/flumtter/app/core/account_selector.rb', line 27

def list_to_s
  @@account_list.map.with_index{|a,i|"#{i}: #{a.screen_name}"}.join("\n")
end

.registObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/flumtter/app/core/account_selector.rb', line 59

def regist
  dialog = Window::Dialog.new("Register Twitter Account", <<~EOF, 6, 70)
    Please enter according to the screen.
  EOF
  keys = {}
  dialog.show do |win|
    Curses.echo
    win.setpos(win.cury+2, 1)
    win.addstr("consumer_key: ")
    keys[:consumer_key] = win.getstr
    win.setpos(win.cury, 1)
    win.addstr("consumer_secret: ")
    keys[:consumer_secret] = win.getstr
  end
  if keys[:consumer_key].empty? && !@@account_list.empty?
    keys[:consumer_key], keys[:consumer_secret] = 
      @@account_list.last.keys.values_at(*%i(consumer_key consumer_secret))
  end

  consumer = OAuth::Consumer.new(keys[:consumer_key], keys[:consumer_secret], {:site=>"https://api.twitter.com"})
  request_token = consumer.get_request_token

  dialog = Window::Dialog.new("Register Twitter Account", <<~EOF)
    Please access the following URL.
    And get the Pin code.

    #{request_token.authorize_url}

    Enter your Pin code: 
  EOF
  dialog.command(/(.+)/) do |m|
    access_token = request_token.get_access_token(:oauth_verifier => m[1])
    keys[:access_token] = access_token.token
    keys[:access_token_secret] = access_token.secret
    user = Twitter::REST::Client.new(keys).user
    keys[:id], keys[:screen_name] = user.id, user.screen_name
    Config[:accounts] << keys
    @@account_list = Config[:accounts].map{|a|Account.new(a)}
  end
  dialog.show(false, false)
  select
end

.select(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flumtter/app/core/account_selector.rb', line 31

def select(options={})
  if options[:id]
    @@account_list[options[:id]]
  elsif options[:name]
    @@account_list.select{|a|a.screen_name == options[:name]}.first
  elsif options[:names]
    @@account_list.select{|a|options[:names].include?(a.screen_name)}.first
  elsif @@account_list.empty?
    regist
    @@account_list.first
  else
    dialog = Window::Dialog.new("Account Selector", <<~EOF)
      Please input your account number.
      Input 'regist' if you want to regist new account.

      #{list_to_s}
    EOF
    dialog.command(/^regist$/, "account registration"){|m|regist}
    dialog.command(/^del ([#{@@account_list.size.times.to_a.join(",")}])$/, "delete account") do |m|
      Config[:accounts].delete_at(m[1].to_i)
      @@account_list.delete_at(m[1].to_i)
      select
    end
    dialog.command(/^([#{@@account_list.size.times.to_a.join(",")}])$/, "account index"){|m|@@account_list[m[1].to_i]}
    dialog.show(true)
  end
end