Module: Flumtter::Util

Included in:
Client, Command::User, Command::UserList, Keyboard, TimeLine::Base, Window::Buf::Element, Window::Buf::Screen
Defined in:
lib/flumtter/app/core/util.rb

Instance Method Summary collapse

Instance Method Details

#command_value_regexp(command) ⇒ Object



64
65
66
# File 'lib/flumtter/app/core/util.rb', line 64

def command_value_regexp(command)
  /^#{command}[ | ]*(.*)/
end

#dialog_for_index(title, body, with_screen_name = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flumtter/app/core/util.rb', line 35

def dialog_for_index(title, body, with_screen_name=false)
  dialog = Window::Dialog.new(title, body)
  dialog.command(index_regexp) do |m|
    [id2obj(m[1]), m[2]]
  end
  if with_screen_name
    dialog.command(screen_name_regexp) do |m|
      [m[1], m[2]]
    end
  end
  dialog.show(true, false)
end

#error(e) ⇒ Object



7
8
9
10
11
12
# File 'lib/flumtter/app/core/util.rb', line 7

def error(e)
  print <<~EOF.color(Setting[:color][:error])
    #{e.backtrace.shift}: #{e.message} (#{e.class})
    #{e.backtrace.join("\n")}
  EOF
end

#error_handlerObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flumtter/app/core/util.rb', line 68

def error_handler
  begin
    yield
  rescue IndexError
    Window::Popup::Error.new("Please select correct index.").show
  rescue UnSupportError
    puts "This object is unsupported.".color
  rescue ExecutedError => e
    text = e.message.empty? ? "The operation is already executed." : e.message
    print text.dnl.color(:cyan)
  rescue NoContentError
    puts "Please input content.".color
  rescue Twitter::Error::NotFound => e
    puts e.message.color
  rescue Twitter::Error::Unauthorized => e
    puts e.message.color
  rescue Twitter::Error::Forbidden => e
    puts e.message.color
  end
end

#id2obj(id) ⇒ Object

Raises:

  • (IndexError)


29
30
31
32
33
# File 'lib/flumtter/app/core/util.rb', line 29

def id2obj(id)
  obj = TimeLine::Base[id.to_i]
  raise IndexError if obj.nil?
  obj
end

#if_tweet(obj, twitter) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/flumtter/app/core/util.rb', line 89

def if_tweet(obj, twitter)
  case obj
  when Twitter::Tweet
    yield(obj)
  when Twitter::Streaming::Event
    type = obj.type(twitter)
    if type.include?(:favorite) || type.include?(:unfavorite)
      yield(obj.target_object)
    else
      raise UnSupportError
    end
  else
    raise UnSupportError
  end
end

#index_regexpObject



56
57
58
# File 'lib/flumtter/app/core/util.rb', line 56

def index_regexp
  /^(\d+)[ | ]*(.*)/
end

#index_with_dialog(m, title, body, with_screen_name = false) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/flumtter/app/core/util.rb', line 48

def index_with_dialog(m, title, body, with_screen_name=false)
  if m.empty?
    dialog_for_index(title, body, with_screen_name)
  else
    parse_index(m, with_screen_name)
  end
end

#on_event(*args, &blk) ⇒ Object



113
114
115
# File 'lib/flumtter/app/core/util.rb', line 113

def on_event(*args,&blk)
  Flumtter.on_event(*args,&blk)
end

#parse_index(text, with_screen_name = false) ⇒ Object

Raises:

  • (IndexError)


18
19
20
21
22
23
24
25
26
27
# File 'lib/flumtter/app/core/util.rb', line 18

def parse_index(text, with_screen_name=false)
  if m = text.match(index_regexp)
    obj = id2obj(m[1])
    return obj, m[2]
  end
  if with_screen_name && m = text.match(screen_name_regexp)
    return m[1], m[2]
  end
  raise IndexError
end

#parse_time(time) ⇒ Object



14
15
16
# File 'lib/flumtter/app/core/util.rb', line 14

def parse_time(time)
  time.getlocal.strftime("%Y/%m/%d %H:%M:%S")
end

#sarastire(*args) ⇒ Object



105
106
107
# File 'lib/flumtter/app/core/util.rb', line 105

def sarastire(*args)
  Flumtter.sarastire(*args)
end

#sarastire_user(*args) ⇒ Object



109
110
111
# File 'lib/flumtter/app/core/util.rb', line 109

def sarastire_user(*args)
  Flumtter.sarastire_user(*args)
end

#screen_name_regexpObject



60
61
62
# File 'lib/flumtter/app/core/util.rb', line 60

def screen_name_regexp
  /^([@|@]*[A-Za-z0-9_]{1,15})[ | ]*(.*)/
end