imsg 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebc7fd076a7c56ab0d450077012b0aa36f9cccc8
4
- data.tar.gz: bd23220b3346f311046c51e078b5b32c5420e621
3
+ metadata.gz: 764296b4df5fa36643856287659a63fde3af17d9
4
+ data.tar.gz: 00e11b5b9dffca41e12ce1e230aeea630d41653e
5
5
  SHA512:
6
- metadata.gz: 68d1ec0492b62487b8d105569d877c211a15306bb3a6147ffcd12e465c0b59f3a00bd717f71ef087d4ac39b0be7aceb3c1fedec58334869045215c7568cf6ee6
7
- data.tar.gz: 68409ec4ef8c10663ebf8c1bfdbbfaf6d952e72149fb6adab6f1080b9d07252d62febbe8048dd2c0991d5371699a9f0efb44b78cfb41601a1277b46ac22438b7
6
+ metadata.gz: 923bde834dc20f9f44bbee55fca2d2ebec66b1d94d3b14ace5fc3e3bbc09259df6df5f85b4daf997d3e8f8cabce45214d52f2b0de65e228aec57358e975a57d7
7
+ data.tar.gz: e31ac22f208906b84376133a72641dc860c1893682ec61cd7f844ed8d94163a9f9ddd9cfe9728b4d5c62e76ac4784225e0da062e61f9e718ec496610c36e776e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- imsg (1.0.0)
4
+ imsg (0.0.5)
5
5
  rb-appscript
6
6
 
7
7
  GEM
data/bin/imsg CHANGED
@@ -11,21 +11,16 @@ def interactWithUser
11
11
  ARGV.clear
12
12
  STDOUT.flush
13
13
 
14
- # Show the chat list with the buddies names
15
- ImsgHandler.showChatList
14
+ # handles user message
15
+ ImsgHandler.ask_for_buddy_with_msg str
16
+ end
16
17
 
17
- # Gets the buddy name or number
18
- response = gets.chomp
19
18
 
20
- # Send the message captured on the beggining to the selected buddy
21
- ImsgHandler.sendMessage str, response
19
+ # Control+C trick in order to get out of exit() signal gracefully
20
+ trap("SIGINT") { throw :ctrl_c }
21
+ catch :ctrl_c do
22
+ begin
23
+ interactWithUser
24
+ rescue Exception
25
+ end
22
26
  end
23
-
24
- # Control+C trick in order to get out of command gracefully
25
- trap("SIGINT") { throw :ctrl_c }
26
- catch :ctrl_c do
27
- begin
28
- interactWithUser
29
- rescue Exception
30
- end
31
- end
@@ -1,56 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- require "imsg/version"
3
- require 'appscript'
2
+ require 'imsg/version'
3
+ require 'model/chat'
4
4
 
5
5
  module ImsgHandler
6
- CHAT_DISPLAY_LIMIT = 12
7
-
8
- class Chat
9
- attr_accessor :chat_number, :updated, :participants
10
-
11
- def initialize(chat_number, updated, participants)
12
- self.chat_number = chat_number
13
- self.updated = updated
14
- self.participants = participants
15
- end
16
-
17
- def to_s
18
- "#{chat_number} - #{participants.map(&:name).join(", ")}"
19
- end
20
-
21
- def self.fetch_all
22
- ascript_chats.map.with_index(1){ |chat, i| from_ascript_chat(chat, i) }
23
- end
24
-
25
- def self.ascript_chats
26
- Appscript.app("Messages").chats.get()
27
- end
28
-
29
- def self.from_ascript_chat(chat, chat_number)
30
- new(chat_number, chat.updated.get(), participants_from_ascript_chat(chat))
31
- end
32
-
33
- def self.participants_from_ascript_chat(chat)
34
- chat.participants.get().map do |participant|
35
- Participant.from_ascript_participant(participant)
36
- end
37
- end
38
- end
39
-
40
- class Participant
41
- attr_accessor :name
42
-
43
- def initialize(name)
44
- self.name = name
45
- end
46
-
47
- def self.from_ascript_participant(ascript_participant)
48
- self.new(ascript_participant.name.get())
49
- end
50
- end
6
+ CHAT_DISPLAY_LIMIT = 15
51
7
 
52
8
  def self.display_chats(chats)
53
- sort_by_updated(chats).first(CHAT_DISPLAY_LIMIT).map(&:to_s).join("\n")
9
+ chats.first(CHAT_DISPLAY_LIMIT).map.with_index{ |x, i| "#{i + 1} - #{x.to_s}"}.join("\n")
54
10
  end
55
11
 
56
12
  def self.sort_by_updated(chats)
@@ -58,15 +14,15 @@ module ImsgHandler
58
14
  end
59
15
 
60
16
  # Check if a String is a integer number
61
- def self.is_i str
62
- !!(str =~ /^[-+]?[0-9]+$/)
17
+ def self.is_i?(str)
18
+ str =~ /\A\d+\z/
63
19
  end
64
20
 
65
21
  # Calls Applescript in order to trigger an iMessage message to a buddy
66
22
  # The buddy parameter accepts a String with either a chat number or a Buddy name
67
- def self.sendMessage message, buddy
68
- if is_i buddy
69
- puts "Sending \'#{message}\' to chat number #{buddy}"
23
+ def self.send_message message, buddy, chat
24
+ if (chat)
25
+ puts "Sending \'#{message}\' to chat with #{chat.to_s}"
70
26
  `osascript -e 'tell application "Messages" to send \"#{message}\" to item #{buddy.to_i} of text chats'`
71
27
  else
72
28
  puts "Sending \'#{message}\' to buddy \'#{buddy}\'"
@@ -75,11 +31,15 @@ module ImsgHandler
75
31
  end
76
32
 
77
33
  # Shows the chat list along with their participants
78
- def self.showChatList
79
- chats = Chat.fetch_all
80
- puts "\nWho would you like to send your message to?"
34
+ def self.ask_for_buddy_with_msg msg
35
+ chats = sort_by_updated(Chat.fetch_all)
36
+ puts "\nTo whom would you like to send your message?"
81
37
  puts "(You can choose a number or type a buddy name/email)\n\n"
82
38
  puts display_chats(chats)
39
+ response = gets.chomp
40
+ chat = is_i?(response) ? chats[response.to_i - 1] : nil
41
+ response = chat ? chat.chat_number.to_s : response
42
+
43
+ send_message(msg, response, chat)
83
44
  end
84
-
85
45
  end
@@ -1,3 +1,3 @@
1
1
  module Imsg
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'appscript'
2
+ require 'model/participant'
3
+
4
+ class Chat
5
+ attr_accessor :chat_number, :updated, :participants
6
+
7
+ def initialize(chat_number, updated, participants)
8
+ self.chat_number = chat_number
9
+ self.updated = updated
10
+ self.participants = participants
11
+ end
12
+
13
+ def to_s
14
+ participants.map(&:name).join(", ")
15
+ end
16
+
17
+ def self.fetch_all
18
+ ascript_chats.map.with_index(1){ |chat, i| from_ascript_chat(chat, i) }
19
+ end
20
+
21
+ def self.ascript_chats
22
+ Appscript.app("Messages").chats.get()
23
+ end
24
+
25
+ def self.from_ascript_chat(chat, chat_number)
26
+ new(chat_number, chat.updated.get(), participants_from_ascript_chat(chat))
27
+ end
28
+
29
+ def self.participants_from_ascript_chat(chat)
30
+ chat.participants.get().map do |participant|
31
+ Participant.from_ascript_participant(participant)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ class Participant
2
+ attr_accessor :name
3
+
4
+ def initialize(name)
5
+ self.name = name
6
+ end
7
+
8
+ def self.from_ascript_participant(ascript_participant)
9
+ self.new(ascript_participant.name.get())
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imsg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sampaio
@@ -71,6 +71,8 @@ files:
71
71
  - imsg.gemspec
72
72
  - lib/imsg.rb
73
73
  - lib/imsg/version.rb
74
+ - lib/model/chat.rb
75
+ - lib/model/participant.rb
74
76
  homepage: https://github.com/chrisfsampaio/imsg
75
77
  licenses:
76
78
  - MIT