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.



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_idObject

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. Returns an array of hashes similar to the one you would use to put_messages.



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

def get_messages(from_id = nil)
  response = self.class.get "#{@url}/outgoing", :basic_auth => @auth
  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'}}]


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
113
114
# File 'lib/qst_client.rb', line 86

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
  response['etag']
end