qst_client 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/qst_client.rb +114 -0
  2. metadata +79 -0
data/lib/qst_client.rb ADDED
@@ -0,0 +1,114 @@
1
+ # A QST client implemented in ruby.
2
+ #
3
+ # === Install
4
+ #
5
+ # gem install qst_client
6
+ #
7
+ # === Example
8
+ #
9
+ # Every method raises a QstClient::Exception with the response if the response code
10
+ # is not between 200 (inclusive) and 400 (exclusive).
11
+ #
12
+ # client = QstClient.new 'service_url', 'username', 'psasword'
13
+ # last_id = client.put_messages [{'id' => '2', 'from' => 'sms://1234', 'to' => 'sms://5678', 'text' => 'Hello!', 'properties' => {'foo' => 'bar'}}]
14
+ # messages = client.get_messages
15
+ # messages = client.get_messages 'from_id'
16
+ require 'rubygems'
17
+ require 'httparty'
18
+ require 'builder'
19
+
20
+ class QstClient
21
+ include HTTParty
22
+
23
+ class Exception < ::Exception
24
+ attr_reader :response
25
+ def initialize(response)
26
+ @response = response
27
+ end
28
+ end
29
+
30
+ def initialize(url, username, password)
31
+ @url = url
32
+ @auth = {:username => username, :password => password}
33
+ end
34
+
35
+ # Returns the response of requesting the last id received by the server.
36
+ def get_last_id
37
+ response = self.class.head("#{@url}/incoming", :basic_auth => @auth)
38
+ raise QstClient::Exception.new response unless (200 ... 400).include? response.code
39
+ response['etag']
40
+ end
41
+
42
+ # Get messages from the server, optionally saying from which id to start
43
+ def get_messages(from_id = nil)
44
+ response = self.class.get "#{@url}/outgoing", :basic_auth => @auth
45
+ raise QstClient::Exception.new response unless (200 ... 400).include? response.code
46
+
47
+ messages = []
48
+
49
+ elems = ((response || {})['messages'] || {})['message']
50
+ elems = [elems] if elems.class <= Hash
51
+
52
+ (elems || []).each do |elem|
53
+ msg = {}
54
+ msg['from'] = elem['from']
55
+ msg['to'] = elem['to']
56
+ msg['text'] = elem['text']
57
+ msg['id'] = elem['id']
58
+ msg['when'] = Time.parse(elem['when']) if elem['when'] rescue nil
59
+
60
+ properties = elem['property']
61
+ if properties
62
+ msg['properties'] = {}
63
+ properties = [properties] if properties.class <= Hash
64
+ properties.each do |prop|
65
+ if msg['properties'][prop['name']]
66
+ if msg['properties'][prop['name']].kind_of? Array
67
+ msg['properties'][prop['name']] << prop['value']
68
+ else
69
+ msg['properties'][prop['name']] = [msg['properties'][prop['name']], prop['value']]
70
+ end
71
+ else
72
+ msg['properties'][prop['name']] = prop['value']
73
+ end
74
+ end
75
+ end
76
+
77
+ messages << msg
78
+ end
79
+
80
+ messages
81
+ end
82
+
83
+ # Put some messages on the server.
84
+ # put_messages [{'id' => '2', 'from' => 'sms://1234', 'to' => 'sms://5678', 'text' => 'Hello!', 'properties' => {'foo' => 'bar'}}]
85
+ def put_messages(messages)
86
+ xml = Builder::XmlMarkup.new(:indent => 1)
87
+ xml.instruct!
88
+ xml.messages do
89
+ messages.each do |msg|
90
+ options = {}
91
+ options[:id] = msg['id'] if msg['id']
92
+ options[:from] = msg['from'] if msg['from']
93
+ options[:to] = msg['to'] if msg['to']
94
+ options[:when] = msg['when'].xmlschema if msg['when']
95
+
96
+ xml.message options do
97
+ xml.text msg['text'] if msg['text']
98
+ if msg[:properties]
99
+ msg[:properties].each do |name, values|
100
+ values = [values] unless values.kind_of? Array
101
+ values.each do |value|
102
+ xml.property :name => name, :value => value
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ response = self.class.post "#{@url}/incoming", :basic_auth => @auth, :body => xml.target!, :headers => {'Content-Type' => 'application/xml'}
111
+ raise QstClient::Exception.new response unless (200 ... 400).include? response.code
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qst_client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - InsTEDD
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-08 00:00:00 +07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httparty
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: A QST client.
35
+ email: aborenszweig@manas.com.ar
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/qst_client.rb
44
+ has_rdoc: true
45
+ homepage: http://code.google.com/p/qst-client-ruby
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A QST client
78
+ test_files: []
79
+