Class: QstClient

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

Defined Under Namespace

Classes: Exception

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password) ⇒ QstClient

Returns a new instance of QstClient.



29
30
31
32
# File 'lib/qst_client.rb', line 29

def initialize(url, username, password)
  @url = url
  @auth = {:username => username, :password => password}
end

Instance Method Details

#get_last_idObject

Returns the response of requesting the last id received by the server.



35
36
37
38
39
# File 'lib/qst_client.rb', line 35

def get_last_id
  response = self.class.head("#{@url}/incoming", :basic_auth => @auth)
  raise QstClient::Exception.new response unless (200 ... 400).include? response.code
  response.headers['etag']
end

#get_messages(options = {:max => 10}) ⇒ Object

Get messages from the server. Returns an array of hashes similar to the one you would use to put_messages. Options:

- :from_id: the id from which to return messages, exclusive
- :max: the maximum amount of messages to get


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/qst_client.rb', line 46

def get_messages(options = {:max => 10})
  params = {:basic_auth => @auth}
  params[:headers] = {'if-none-match' => options[:from_id]} if options[:from_id] 

  response = self.class.get "#{@url}/outgoing?max=#{options[:max]}", params 
  raise QstClient::Exception.new response unless (200 ... 400).include? response.code
  
  messages = []
  
  elems = ((response || {})['messages'] || {})['message']
  elems = [elems] if elems.class <= Hash
  
  (elems || []).each do |elem|
    msg = {}
    msg['from'] = elem['from']
    msg['to'] = elem['to']
    msg['text'] = elem['text']
    msg['id'] = elem['id']
    msg['when'] = Time.parse(elem['when']) if elem['when'] rescue nil
    
    properties = elem['property']
    if properties
      msg['properties'] = {}
      properties = [properties] if properties.class <= Hash      
      properties.each do |prop|
        if msg['properties'][prop['name']]
          if msg['properties'][prop['name']].kind_of? Array
            msg['properties'][prop['name']] << prop['value']
          else
            msg['properties'][prop['name']] = [msg['properties'][prop['name']], prop['value']]
          end 
        else
          msg['properties'][prop['name']] = prop['value']
        end 
      end
    end
    
    messages << msg
  end
  
  messages
end

#put_messages(messages) ⇒ Object

Put some messages on the server.

put_messages [{'id' => '2', 'from' => 'sms://1234', 'to' => 'sms://5678', 'text' => 'Hello!', 'properties' => {'foo' => 'bar'}}]


91
92
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
# File 'lib/qst_client.rb', line 91

def put_messages(messages)
  xml = Builder::XmlMarkup.new(:indent => 1)
  xml.instruct!
  xml.messages do
    messages.each do |msg|
      options = {}
      options[:id] = msg['id'] if msg['id'] 
      options[:from] = msg['from'] if msg['from']
      options[:to] = msg['to'] if msg['to']
      options[:when] = msg['when'].xmlschema if msg['when']
    
      xml.message options do
        xml.text msg['text'] if msg['text']
        if msg[:properties]
           msg[:properties].each do |name, values|
            values = [values] unless values.kind_of? Array
            values.each do |value|
              xml.property :name => name, :value => value
            end
          end
        end
      end
    end
  end

  response = self.class.post "#{@url}/incoming", :basic_auth => @auth, :body => xml.target!, :headers => {'Content-Type' => 'application/xml'}
  raise QstClient::Exception.new response unless (200 ... 400).include? response.code
end