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(options = {:max => 10}) ⇒ Object
Get messages from the server.
-
#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.
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_id ⇒ Object
Returns the response of requesting the last id received by the server.
35 36 37 38 39 40 41 |
# 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 etag = response.headers['etag'] etag = etag.first if etag.kind_of? Array 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
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 88 89 |
# File 'lib/qst_client.rb', line 48 def ( = {:max => 10}) params = {:basic_auth => @auth} params[:headers] = {'if-none-match' => [:from_id]} if [:from_id] response = self.class.get "#{@url}/outgoing?max=#{[:max]}", params 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'}}]
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 |
# File 'lib/qst_client.rb', line 93 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 |