Class: HashBlue::Message
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
-
#contact ⇒ Object
HashBlue::Contact object.
-
#content ⇒ Object
Returns the value of attribute content.
-
#id ⇒ Object
Returns the value of attribute id.
-
#sent ⇒ Object
true|false.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
-
.create!(phone_number, content) ⇒ Object
Send a message to a specific number.
-
.favourites ⇒ Object
Retrieve the messaged marked as favourites.
-
.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.
Instance Method Summary collapse
-
#delete! ⇒ Object
(also: #destroy!)
Delete message.
-
#favourite! ⇒ Object
Mark as favourite.
-
#save! ⇒ Object
Create message.
- #to_s ⇒ Object
-
#unfavourite! ⇒ Object
Unmark as favourite.
Methods inherited from Client
client, delete, get, post, put
Instance Attribute Details
#contact ⇒ Object
HashBlue::Contact object
49 50 51 |
# File 'lib/hash_blue/message.rb', line 49 def contact @contact end |
#content ⇒ Object
Returns the value of attribute content.
48 49 50 |
# File 'lib/hash_blue/message.rb', line 48 def content @content end |
#id ⇒ Object
Returns the value of attribute id.
47 48 49 |
# File 'lib/hash_blue/message.rb', line 47 def id @id end |
#sent ⇒ Object
true|false
50 51 52 |
# File 'lib/hash_blue/message.rb', line 50 def sent @sent end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
51 52 53 |
# File 'lib/hash_blue/message.rb', line 51 def @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) = {:phone_number => phone_number, :content => content} post "/messages", {:message => } end |
.favourites ⇒ Object
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
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? = get "/messages" elsif arg.is_a? Symbol if arg.eql?(:all) = get "/messages" elsif arg.eql?(:first) = get "/messages?per_page=1" else raise ArgumentError, "Invalid argument #{arg}" end elsif arg.is_a? String or arg.is_a? Fixnum = get "/messages/#{arg}" elsif arg.is_a? Hash if arg.has_key?(:contact) = get "/contacts/#{arg[:contact]}/messages" elsif arg.has_key?(:since) arg[:since].is_a?(String) or arg[:since].is_a? = get "/messages?since=#{arg[:since]}" elsif arg.has_key?(:first) = get "/messages?page=2&per_page=#{arg[:first]}" else raise ArgumentError, "Invalid argument #{arg}" end else raise ArgumentError, "Invalid argument #{arg}" end parse_response() 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_s ⇒ Object
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 => #{}}" 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 |