Class: MessagePub::Client

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

Overview

Implements a client object to communicate with the MessagePub Rest API.

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client

To create a new MessagePub::Client, you need to pass in your MessagePub API Key.

client = MessagePub::Client.new('YOURAPIKEYGOESHERE')

You can also set it in your environment by putting the following line in your .bashrc file.

export MESSAGEPUB_API_KEY=YOURKEY

In that case, you can initialize a new client by simply saying:

client = MessagePub::Client.new



22
23
24
25
# File 'lib/messagepub/client.rb', line 22

def initialize(api_key=nil)
  @api_key = api_key || ENV['MESSAGEPUB_API_KEY']
  self.class.basic_auth @api_key, 'password'
end

Instance Method Details

#cancel(id) ⇒ Object

Cancel the notification for the unique id specified. Returns true if the notification was cancelled, false otherwise. cancelled = client.cancel_notification(4)



79
80
81
82
# File 'lib/messagepub/client.rb', line 79

def cancel(id)
  response = self.class.delete("/notifications/" + id.to_s + ".xml")
  response.is_a?(Hash) && response.empty?
end

#create!(note) ⇒ Object

Creates a new notification.

note = MessagePub::Notification.new

note.body = 'The servers are down.'

note.escalation = 15

note.save

note.add_recipient(MessagePub::Recipient.new(:position => 1, :channel => 'aim', :address => 'username'))

note.add_recipient(MessagePub::Recipient.new(:position => 1, :channel => 'email', :address => '[email protected]'))

client.create!(note)



99
100
101
# File 'lib/messagepub/client.rb', line 99

def create!(note)
  self.class.post('/notifications.xml', :body => note.to_xml)
end

#notification(id) ⇒ Object

Gets the notification for the unique id specified. Returns a MessagePub::Notification object. my_notification = client.notification(4)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/messagepub/client.rb', line 58

def notification(id)
  response = self.class.get("/notifications/" + id.to_s + ".xml")
  note = response["notification"]
  new_note = Notification.new( :body => note["body"],
                               :subject => note["subject"],
                               :id => note["id"],
                               :send_at => note["send_at"],
                               :escalation => note["escalation"])
  note["recipients"].each do |rcpt|
    new_note.add_recipient(Recipient.new(:id => rcpt["id"],
                                         :channel => rcpt["channel"],
                                         :address => rcpt["address"],
                                         :status  => rcpt["status"],
                                         :send_at => rcpt["send_at"]))
  end
  new_note       
end

#notificationsObject

Gets a list of the last notifications sent Returns an array of MessagePub::Notification objects.

client = MessagePub::Client.new('YOURAPIKEYGOESHERE')

n = client.notifications



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/messagepub/client.rb', line 34

def notifications
  response = self.class.get('/notifications.xml')
  notifications_array = []
  response['notifications'].each do |note|
    new_note = Notification.new(:body => note["body"],
                                :subject => note["subject"],
                                :send_at => note["send_at"],
                                :id => note["id"],
                                :escalation => note["escalation"])
    note["recipients"].each do |rcpt|
      new_note.add_recipient(Recipient.new(:id => rcpt["id"],
                                           :channel => rcpt["channel"],
                                           :address => rcpt["address"],
                                           :status  => rcpt["status"],
                                           :send_at => rcpt["send_at"]))
    end
    notifications_array << new_note        
  end
  notifications_array
end

#repliesObject

Gets a list of the latest replies to your notifications. Returns an array of MessagePub::Reply objects

my_replies = client.replies



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/messagepub/client.rb', line 107

def replies
  response = self.class.get("/replies.xml")
  replies_array = []
  response['replies'].each do |reply|
    new_reply = Reply.new(:id => reply['id'], :body => reply['body'],
                          :channel => reply['channel'], :address => reply['address'],
                          :notification_id => reply['notification_id'])
    replies_array << new_reply
  end
  replies_array
end