Module: ImsgHandler
- Defined in:
- lib/imsg.rb
Constant Summary collapse
- CHAT_DISPLAY_LIMIT =
15
Class Method Summary collapse
-
.ask_for_buddy_with_msg(msg) ⇒ Object
Shows the chat list along with their participants.
- .display_chats(chats) ⇒ Object
-
.is_i?(str) ⇒ Boolean
Check if a String is a integer number.
-
.send_message(message, buddy, chat) ⇒ Object
Calls Applescript in order to trigger an iMessage message to a buddy The buddy parameter accepts a String with either a chat number or a Buddy name.
- .sort_by_updated(chats) ⇒ Object
Class Method Details
.ask_for_buddy_with_msg(msg) ⇒ Object
Shows the chat list along with their participants
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/imsg.rb', line 34 def self.ask_for_buddy_with_msg msg chats = sort_by_updated(Chat.fetch_all) puts "\nTo whom would you like to send your message?" puts "(You can choose a number or type a buddy name/email)\n\n" puts display_chats(chats) response = gets.chomp chat = is_i?(response) ? chats[response.to_i - 1] : nil response = chat ? chat.chat_number.to_s : response (msg, response, chat) end |
.display_chats(chats) ⇒ Object
8 9 10 |
# File 'lib/imsg.rb', line 8 def self.display_chats(chats) chats.first(CHAT_DISPLAY_LIMIT).map.with_index{ |x, i| "#{i + 1} - #{x.to_s}"}.join("\n") end |
.is_i?(str) ⇒ Boolean
Check if a String is a integer number
17 18 19 |
# File 'lib/imsg.rb', line 17 def self.is_i?(str) str =~ /\A\d+\z/ end |
.send_message(message, buddy, chat) ⇒ Object
Calls Applescript in order to trigger an iMessage message to a buddy The buddy parameter accepts a String with either a chat number or a Buddy name
23 24 25 26 27 28 29 30 31 |
# File 'lib/imsg.rb', line 23 def self. , buddy, chat if (chat) puts "Sending \'#{}\' to chat with #{chat.to_s}" `osascript -e 'tell application "Messages" to send \"#{}\" to item #{buddy.to_i} of text chats'` else puts "Sending \'#{}\' to buddy \'#{buddy}\'" `osascript -e 'tell application "Messages" to send \"#{}\" to buddy \"#{buddy}\"'` end end |
.sort_by_updated(chats) ⇒ Object
12 13 14 |
# File 'lib/imsg.rb', line 12 def self.sort_by_updated(chats) chats.sort{ |a, b| b.updated <=> a.updated } end |