Class: Bandwidth::Message

Inherits:
Object
  • Object
show all
Extended by:
ClientWrapper
Defined in:
lib/bandwidth/message.rb

Overview

The Messages resource lets you send SMS text messages and view messages that were previously sent or received.

Class Method Summary collapse

Methods included from ClientWrapper

wrap_client_arg

Class Method Details

.create(client, data) ⇒ Hash

Send text messages

Examples:

message = Message.create(client, {:from=>"from", :to=>"to", :text=>"text"})
statuses = Message.create(client, [{:from=>"from1", :to=>"to1", :text=>"text1"}, {:from=>"from2", :to=>"to2", :text=>"text2"}])

Parameters:

  • client (Client)

    optional client instance to make requests

  • data (Hash|Array)

    options of new message or list of messages

Returns:

  • (Hash)

    created message or statuses of list of messages



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bandwidth/message.rb', line 36

def self.create(client, data)
  res = client.make_request(:post, client.concat_user_path(MESSAGE_PATH), data)
  if data.is_a? Array
    res[0].map do |i|
      if i[:result] == "error"
        {:error => StandardError.new(i[:error][:message])}
      else
        items = (i[:location] || '').split('/')
        if items.size < 2 then  {:error =>  StandardError.new('Missing id in the location header')} else {:id => items.last} end
      end
    end
  else
    headers = res[1]
    id = Client.get_id_from_location_header(headers[:location])
    self.get(client, id)
  end
end

.get(client, id) ⇒ Hash

Get information about a message that was sent or received

Examples:

message = Message.get(client, "id")

Parameters:

  • client (Client)

    optional client instance to make requests

  • id (String)

    id of message

Returns:

  • (Hash)

    message information



13
14
15
# File 'lib/bandwidth/message.rb', line 13

def self.get(client, id)
  client.make_request(:get, client.concat_user_path("#{MESSAGE_PATH}/#{id}"))[0]
end

.list(client, query = nil) ⇒ Array

Get a list of previous messages that were sent or received

Examples:

messages = Message.list(client)

Parameters:

  • client (Client)

    optional client instance to make requests

  • query (Hash) (defaults to: nil)

    query options

Returns:

  • (Array)

    list of messages



24
25
26
# File 'lib/bandwidth/message.rb', line 24

def self.list(client, query = nil)
  client.make_request(:get, client.concat_user_path(MESSAGE_PATH), query)[0]
end