Class: Groupme::Cli::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/groupme/cli/session.rb

Overview

Represents a GroupMe session. This wraps the GroupMe API gem and has methods for accessing the data for a specific session and sending messages.

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Session

Returns a new instance of Session.



13
14
15
16
17
18
19
# File 'lib/groupme/cli/session.rb', line 13

def initialize token
  @token = token
  @client = GroupMe::Client.new(token: token)
  @current_chat_id = nil
  @new_msgs = Array.new
  @last_msg_id = 0
end

Instance Method Details

#chat_is_open?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/groupme/cli/session.rb', line 97

def chat_is_open?
  !@current_chat_id.nil?
end

#get_groupsObject

Lists the names of all the groups that the user is part of.



22
23
24
# File 'lib/groupme/cli/session.rb', line 22

def get_groups
  @client.groups
end

#new_messagesObject

Fetch messages from the currently open group that haven’t been fetched yet.



56
57
58
59
60
61
62
63
# File 'lib/groupme/cli/session.rb', line 56

def new_messages
  # save the new messages
  messages = @new_msgs.clone
  # flush @new_msgs since we've seen these already now
  @new_msgs.clear

  return messages
end

#open_chat(name) ⇒ Object

“Open” a chat with the specified name. This means that the CLI will display live messages from the chat, and any messages you write will go to that chat as well. Returns true if it succeeds and false otherwise.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/groupme/cli/session.rb', line 29

def open_chat name
  group = @client.groups.select {|g| g[:name] == name}[0]

  if group.nil?
    return false
  else
    @current_chat_id = group[:id]
    @last_msg_id = 0
    return true
  end
end

#send_msg(msg, image = nil) ⇒ Object

Send a message to the current chat.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/groupme/cli/session.rb', line 42

def send_msg(msg, image=nil)
  with_valid_group do
    attachments = []
    # process image attachments
    unless image.nil?
      url = make_img_url image
      attachments << {type: "image", url: url}
    end

    @client.create_message(@current_chat_id, msg, attachments)
  end
end

#update_messagesObject

Query the API for “new” messages. In this case, “new” messages are ones that the programmer has not been given (via a call to Session#new_messages) yet.



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
# File 'lib/groupme/cli/session.rb', line 67

def update_messages
  with_valid_group do
    messages = @client.messages(@current_chat_id, {since_id: @last_msg_id, limit: 20})

    # look through the messages and save the new ones
    messages.each do |msg|
      sender = abbrev_name msg[:name]
      time = Time.at msg[:created_at]
      text = msg[:text] || ""

      # create a new Message object and push it onto the front of the new_msgs array
      message_obj = Message.new(sender: sender, time: time, text: text)
      @new_msgs.unshift message_obj

      # replace attachments with their types/urls since images and stuff aren't terminal-friendly
      replacements = msg[:attachments].map do |att|
        att[:type] == "image"? "[image @ #{att[:url]}]" : att[:type]
      end

      # add in the replacements, separated by newlines
      text << replacements.join("\n")

      # save the ID of the newest message
      unless messages.empty?
        @last_msg_id = messages[0][:id]
      end
    end
  end
end