bolide_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1 @@
1
+ Please see http://bolideapp.com/api
@@ -0,0 +1,89 @@
1
+ module Bolide
2
+
3
+ class Account
4
+ attr_accessor :session
5
+
6
+ def initialize(account, api_key, url = 'http://live.bolideapp.com')
7
+ @account = account
8
+ @api_key = api_key
9
+ @url = url
10
+ @session = Patron::Session.new
11
+ @session.base_url = @url
12
+ end
13
+
14
+ def get_q(name, create = true)
15
+ q = Q.new(self , name)
16
+ q.new_token if create
17
+ q
18
+ end
19
+
20
+ def send_msg(body, qs = "/.*/")
21
+ return false if !qs.kind_of?(String) && !qs.kind_of?(Array)
22
+
23
+ msg = create_msg(body, qs)
24
+ resp = session.post('/msg.xml', msg, headers)
25
+ p resp.body
26
+ if resp.status > 400
27
+ xml = Nokogiri::XML(resp.body)
28
+
29
+ @error = xml.at_css('error').content
30
+ if(@error)
31
+ raise @error
32
+ end
33
+ end
34
+ end
35
+
36
+ def headers
37
+ now = DateTime.now.to_s
38
+ return {'X-Bol-Date'=>now, 'X-Bol-Authentication'=>auth_key(now), 'Content-Type'=>'application/xml'}
39
+ end
40
+
41
+ def marshal_dump
42
+ {
43
+ :account=>@account,
44
+ :api_key=>@api_key,
45
+ :url=>@url,
46
+ }
47
+ end
48
+
49
+ def marshal_load(data)
50
+ @account = data[:account]
51
+ @api_key = data[:api_key]
52
+ @url = data[:url]
53
+ @session = Patron::Session.new
54
+ @session.base_url = @url
55
+ end
56
+
57
+ private
58
+ def auth_key(now)
59
+ auth_key_prev = 'Account:' + @account + '\n' + 'Api-Key:' + @api_key + '\n' + 'X-Bol-Date:' + now
60
+ auth_key = Digest::MD5.hexdigest(auth_key_prev)
61
+ @account + ':' + auth_key
62
+ end
63
+
64
+ def create_msg(body, qs)
65
+ xml = Nokogiri::XML::Builder.new do |xml|
66
+ xml.msg do
67
+ if qs.kind_of?(String)
68
+ xml.qs :select=>qs
69
+ elsif qs.kind_of?(Array)
70
+ xml.qs do
71
+ qs.each do |q|
72
+ if q.kind_of?(Bolide::Q)
73
+ xml.q q.name
74
+ else
75
+ xml.q q
76
+ end
77
+ end
78
+ end
79
+ end
80
+ xml.body do
81
+ xml.cdata body
82
+ end
83
+ end
84
+ end
85
+ xml.to_xml.to_s
86
+ end
87
+
88
+ end
89
+ end
data/lib/bolide/q.rb ADDED
@@ -0,0 +1,76 @@
1
+ module Bolide
2
+
3
+ class Q
4
+ attr_accessor :token, :name, :bolide, :expire_on, :msg_count
5
+
6
+ def initialize(bolide, name)
7
+ @bolide = bolide
8
+ @name = name
9
+ @token = nil
10
+ @expire_on = nil
11
+ @msg_count = nil
12
+ end
13
+
14
+ def expired?
15
+ resp = bolide.session.get('/q/' + name + '.xml', bolide.headers)
16
+ if resp.status < 400
17
+ read_response resp
18
+ else
19
+ raise read_error(resp)
20
+ end
21
+ @expire_on && @expire_on > DateTime.now && @token
22
+ end
23
+
24
+ def new_token
25
+ resp = bolide.session.put('/q/' + name + '.xml','', bolide.headers)
26
+ if resp.status < 400
27
+ read_response resp
28
+ else
29
+ raise read_error(resp)
30
+ end
31
+ return @token
32
+ end
33
+
34
+ def send_msg(body)
35
+ bolide.send_msg(body, [@name])
36
+ end
37
+
38
+ def marshal_dump
39
+ {
40
+ :name=>@name,
41
+ :token=>@token,
42
+ :bolide=>@bolide,
43
+ :msg_count=>@msg_count,
44
+ :expire_on=>@expire_on
45
+ }
46
+ end
47
+
48
+ def marshal_load(data)
49
+ @name = data[:name]
50
+ @bolide = data[:bolide]
51
+ @msg_count = data[:msg_count]
52
+ @token = data[:token]
53
+ @expire_on = data[:expire_on]
54
+ end
55
+
56
+ private
57
+ def read_error(resp)
58
+ xml = Nokogiri::XML(resp.body)
59
+
60
+ @error = xml.at_css('error').content
61
+ if(@error)
62
+ p @error
63
+ @error
64
+ end
65
+ end
66
+
67
+ def read_response(resp)
68
+ #parse xml
69
+ xml = Nokogiri::XML(resp.body)
70
+
71
+ @token = xml.at_css('q token').content
72
+ @expire_on = xml.at_css('q expire_on').content
73
+ @msg_count = xml.at_css('q msg_count').content
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'patron'
3
+ require 'nokogiri'
4
+ require 'digest/md5'
5
+
6
+
7
+ module Bolide
8
+
9
+
10
+ autoload :Account, File.expand_path("../bolide/account.rb", __FILE__)
11
+ autoload :Q, File.expand_path("../bolide/q.rb", __FILE__)
12
+
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bolide_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julien Guimont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-16 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: patron
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.1
34
+ version:
35
+ description:
36
+ email: julien.guimont@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - lib/bolide/account.rb
45
+ - lib/bolide/q.rb
46
+ - lib/bolide_client.rb
47
+ - README
48
+ has_rdoc: true
49
+ homepage: http://github.com/juggy/bolide-client
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Ruby client for Bolide API
76
+ test_files: []
77
+