Class: HashBlue::Message

Inherits:
Client
  • Object
show all
Defined in:
lib/hash_blue/message.rb

Overview

This class models the Message entity, providing an easy way to CRUD operations using ActiveRecord as design model

Examples of use:

# Initialize client with a valid access token HashBlue::Client.user = <valid_access_token>

# Retrieve all messages messages = HashBlue::Message.find(:all)

# Retrieve a specific message message = HashBlue::Message.find(<valid_message_id>)

# Retrieve a contact messages messages = HashBlue::Message.find(=> <valid_contact_id>)

# Retrieve favourite messages messages = HashBlue::Message.favourites

# Mark a message as favourite message = HashBlue::Message.find(<valid_message_id>) message.favourite!

# Unmark a message as favourite message = HashBlue::Message.find(<valid_message_id>) message.unfavourite!

# Create a message HashBlue::Message.create!(<phone_number>, <content>)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

client, delete, get, post, put

Instance Attribute Details

#contactObject

HashBlue::Contact object



49
50
51
# File 'lib/hash_blue/message.rb', line 49

def contact
  @contact
end

#contentObject

Returns the value of attribute content.



48
49
50
# File 'lib/hash_blue/message.rb', line 48

def content
  @content
end

#idObject

Returns the value of attribute id.



47
48
49
# File 'lib/hash_blue/message.rb', line 47

def id
  @id
end

#sentObject

true|false



50
51
52
# File 'lib/hash_blue/message.rb', line 50

def sent
  @sent
end

#timestampObject

Returns the value of attribute timestamp.



51
52
53
# File 'lib/hash_blue/message.rb', line 51

def timestamp
  @timestamp
end

Class Method Details

.create!(phone_number, content) ⇒ Object

Send a message to a specific number



129
130
131
132
# File 'lib/hash_blue/message.rb', line 129

def create!(phone_number, content)
  message = {:phone_number => phone_number, :content => content}
  post "/messages", {:message => message}
end

.favouritesObject

Retrieve the messaged marked as favourites



124
125
126
# File 'lib/hash_blue/message.rb', line 124

def favourites
  parse_response(get "/messages/favourites")
end

.find(arg = nil) ⇒ Object

Retrieve a specific message or a set of messages

> nil => retrieve all messages

> :all => retrieve all messages

> :first => retrieve the first message

> => ISO 8601 format => retrieve messages since the

specified data (i.e. 2011-01-14T14:30Z)

> => index, :count => count => retrieve count messages from index first

> => <contact_id> => retrieve a specific contact messages

> id => retrieve a specific message using a valid unique identifier

Parameters:

  • arg:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hash_blue/message.rb', line 93

def find(arg = nil)
  if arg.nil?
    messages = get "/messages"
  elsif arg.is_a? Symbol
    if arg.eql?(:all)
      messages = get "/messages"
    elsif arg.eql?(:first)
      messages = get "/messages?per_page=1"
    else
      raise ArgumentError, "Invalid argument #{arg}"
    end
  elsif arg.is_a? String or arg.is_a? Fixnum
    messages = get "/messages/#{arg}"
  elsif arg.is_a? Hash
    if arg.has_key?(:contact)
      messages = get "/contacts/#{arg[:contact]}/messages"
    elsif arg.has_key?(:since)
      arg[:since].is_a?(String) or arg[:since].is_a?
      messages = get "/messages?since=#{arg[:since]}"
    elsif arg.has_key?(:first)
      messages = get "/messages?page=2&per_page=#{arg[:first]}"
    else
      raise ArgumentError, "Invalid argument #{arg}"
    end
  else
    raise ArgumentError, "Invalid argument #{arg}"
  end
  parse_response(messages)
end

Instance Method Details

#delete!Object Also known as: destroy!

Delete message



64
65
66
# File 'lib/hash_blue/message.rb', line 64

def delete!
  self.class.delete "/messages/#{id}"
end

#favourite!Object

Mark as favourite



54
55
56
# File 'lib/hash_blue/message.rb', line 54

def favourite!
  self.class.put "/messages/#{id}/favourite"
end

#save!Object

Create message



69
70
71
# File 'lib/hash_blue/message.rb', line 69

def save!
  self.class.create!(contact.is_a? HashBlue::Contact ? contact.phone_number : contact, content)
end

#to_sObject



73
74
75
# File 'lib/hash_blue/message.rb', line 73

def to_s
  "#{self.class.name} [#{id}] => {:content => #{content}, :contact => #{contact}, :sent => #{sent}, :timestamp => #{timestamp}}"
end

#unfavourite!Object

Unmark as favourite



59
60
61
# File 'lib/hash_blue/message.rb', line 59

def unfavourite!
  self.class.delete "/messages/#{id}/favourite"
end