Class: QstClient
Defined Under Namespace
Classes: Exception
Instance Method Summary collapse
-
#get_last_id ⇒ Object
Returns the response of requesting the last id received by the server.
-
#get_messages(from_id = nil) ⇒ Object
Get messages from the server, optionally saying from which id to start.
-
#initialize(url, username, password) ⇒ QstClient
constructor
A new instance of QstClient.
-
#put_messages(messages) ⇒ Object
Put some messages on the server.
Constructor Details
#initialize(url, username, password) ⇒ QstClient
Returns a new instance of QstClient.
30 31 32 33 |
# File 'lib/qst_client.rb', line 30 def initialize(url, username, password) @url = url @auth = {:username => username, :password => password} end |
Instance Method Details
#get_last_id ⇒ Object
Returns the response of requesting the last id received by the server.
36 37 38 39 40 |
# File 'lib/qst_client.rb', line 36 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['etag'] end |
#get_messages(from_id = nil) ⇒ Object
Get messages from the server, optionally saying from which id to start
43 44 45 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 |
# File 'lib/qst_client.rb', line 43 def (from_id = nil) response = self.class.get "#{@url}/outgoing", :basic_auth => @auth raise QstClient::Exception.new response unless (200 ... 400).include? response.code = [] 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 << msg end end |
#put_messages(messages) ⇒ Object
Put some messages on the server.
[{'id' => '2', 'from' => 'sms://1234', 'to' => 'sms://5678', 'text' => 'Hello!', 'properties' => {'foo' => 'bar'}}]
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/qst_client.rb', line 85 def () xml = Builder::XmlMarkup.new(:indent => 1) xml.instruct! xml. do .each do |msg| = {} [:id] = msg['id'] if msg['id'] [:from] = msg['from'] if msg['from'] [:to] = msg['to'] if msg['to'] [:when] = msg['when'].xmlschema if msg['when'] xml. 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 |