Class: RubyMarks

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rubymarks.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RubyMarks

When instantiating RubyMarks, you need to supply your TextMarks username, password, keyword, and API key (all required).

r = RubyMarks.new(
  :user => 'myusername',
  :password => 'mypassword',
  :keyword => 'mykeyword', # The keyword you chose when you signed up for TextMarks
  :api_key => 'mysite_com_123456'
)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubymarks.rb', line 15

def initialize(options = {})    
  if [:user, :password, :keyword, :api_key].any? { |o| options[o].nil? }
    raise ArgumentError, "Missing required argument(s). Requires :user, :password, :keyword, :api_key"
  end

  valid_api_key = self.class.get('/Test/test_api_key/', :query => { :api_key => options[:api_key] })['head']['rescode'] == 0
   = self.class.get('/Test/test_auth_user/', :query => { :auth_user => options[:user], :auth_pass => options[:password] })['head']['rescode'] == 0
  keyword_status = self.class.get('/Anybody/keyword_status/', :query => { :keyword => options[:keyword] })

  if valid_api_key
    if 
      if keyword_status['body']['available']
        raise "Keyword '#{options[:keyword]}' hasn't been registered."
      else
        if keyword_status['body']['public_config']['manager']['nick'] == options[:user]
          self.class.default_params :auth_user => options[:user], :auth_pass => options[:password], :tm => options[:keyword], :api_key => options[:api_key]
        else
          raise "Keyword doesn't belong to user '#{options[:user]}'"
        end
      end
    else
      raise "Invalid username or password."
    end
  else
    raise "Invalid API key."
  end
end

Instance Method Details

#delete_member(user) ⇒ Object

Deletes (unsubscribes) a member from your group. Supply either a 10 digit phone number or a username.

r.delete_member '2065552323'

r.delete_member 'User123'


92
93
94
# File 'lib/rubymarks.rb', line 92

def delete_member(user)
  self.class.post('/GroupLeader/kick_member/', :query => { :user => user })
end

#delete_message(message_id) ⇒ Object

Deletes a message that is in your group.

r.delete_message '123456' # Delete message with the id of 123456

r.delete_message r.messages.first['id'] # Delete the first message in the group


117
118
119
# File 'lib/rubymarks.rb', line 117

def delete_message(message_id)
  self.class.post('/GroupLeader/delete_message/', :query => { :msg_id => message_id })
end

#is_member?(user) ⇒ Boolean

Check if someone is subscribed to your group. Supply either a 10 digit phone number or a username.

r.is_member? 'User123'
=> true

r.is_member? '2065552323'
=> true

r.is_member? 'random_dude'
=> false

Returns:

  • (Boolean)


84
85
86
# File 'lib/rubymarks.rb', line 84

def is_member?(user)
  self.class.get('/GroupLeader/has_member/', :query => { :user => user })['body']['member'] if user
end

#membersObject

Returns an array of everyone subscribed to your group. (Can only return up to 100 members)

r.members
=> [{"time"=>1241682280, "nick"=>"group_leader", "phone"=>"+12065551414"},
    {"time"=>1241685492, "nick"=>"User123", "phone"=>"+12065552323"},
    {"time"=>1241682332, "nick"=>"User456", "phone"=>"+12065557979"}]

r.members.size
=> 3

r.members.last['nick']
=> "User456"


71
72
73
# File 'lib/rubymarks.rb', line 71

def members
  self.class.get('/GroupLeader/members/', :query => { :count => 100 })['body']['members']
end

#messagesObject

Returns an array of the public messages in your group (these include alerts sent out and messages your subscribes may have sent back to your group). (JSON format)

r.messages
=> [{"msg"=>"Change of plans! We're all now going to meet at the hot dog stand.", "time"=>1241680837, "nick"=>"group_leader", "id"=>123456, "phone"=>"+12065551414"},
    {"msg"=>"But I don't like hot dogs.", "time"=>1241638161, "nick"=>"User123", "id"=>867750, "phone"=>"+12065552323"}]

r.messages.size
=> 2

for message in r.messages
  p "#{message['nick']} said '#{message['msg']}'"
end
=> "group_leader said 'Change of plans! We're all now going to meet at the hot dog stand.'"
   "User123 said 'But I don't like hot dogs.'"


109
110
111
# File 'lib/rubymarks.rb', line 109

def messages
  self.class.get('/GroupLeader/messages/')['body']['messages']
end

#send(action, options = {}) ⇒ Object

Allows you to send invitations, messages, and alerts. To invite someone to your group:

r.send :invite, :to => '2065551414'

To send a regular message:

r.send :message, :to =>'2065551414', :message => 'Hi there!'

To send an alert (message that is sent to all of your subscribers):

r.send :alert, :message => 'Important! A new episode of 30 Rock is on!'

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
# File 'lib/rubymarks.rb', line 50

def send(action, options = {})
  raise ArgumentError, "Invalid messaging action: #{action}" unless [:invite, :message, :alert].member? action

  case action
    when :invite then send_invite(options)
    when :message then send_message(options)
    when :alert then send_alert(options)
  end
end