em-ucengine 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ require_relative "errors"
6
+
7
+ module UCEngine
8
+ module NetHttpResponse
9
+
10
+ def answer(req)
11
+ if req.kind_of?(Net::HTTPServerError)
12
+ raise UCEngine::Client::HttpError.new(req.code, req.body)
13
+ end
14
+
15
+ data = JSON.parse(req.body)
16
+ if data['error']
17
+ raise UCEngine::Client::UCError.new(req.code, data['error'])
18
+ end
19
+
20
+ data['result']
21
+ end
22
+
23
+ def answer_bool(req, &block)
24
+ result = answer(req)
25
+ result == "true"
26
+ end
27
+
28
+ def answer_connect(req)
29
+ credentials = answer(req)
30
+ UCEngine::Client::Session.new(self, credentials['uid'], credentials['sid'])
31
+ end
32
+
33
+ end
34
+
35
+ module NetHttpRequest
36
+
37
+ def get(path, params=nil)
38
+ uri = URI.parse(path)
39
+ params ||= {}
40
+ params.merge!({:uid => self.uid, :sid => self.sid})
41
+
42
+ query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') if params
43
+ Net::HTTP.start(uri.host, uri.port) do |http|
44
+ http.get("#{uri.path}?#{query}")
45
+ end
46
+ end
47
+
48
+ def post(path, params=nil, body=nil)
49
+ uri = URI.parse(path)
50
+ body ||={}
51
+ body.merge!(params) if params
52
+ body.merge!({:uid => self.uid, :sid => self.sid}) if self.class == UCEngine::Client::Session
53
+
54
+ Net::HTTP.post_form(uri, body)
55
+ end
56
+
57
+ def delete(path, params=nil)
58
+ uri = URI.parse(path)
59
+ params ||= {}
60
+ params.merge!({:uid => self.uid, :sid => self.sid})
61
+
62
+ query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&') if params
63
+ Net::HTTP.start(uri.host, uri.port) do |http|
64
+ http.delete("#{uri.path}?#{query}")
65
+ end
66
+ end
67
+
68
+ # Perform a post request on the API with a content type application/json
69
+ #
70
+ # @param [String] path
71
+ # @param [Hash] body
72
+ def json_post(path, body)
73
+ #http_request(:post, path, {}, body, nil, {'Content-Type' => 'application/json'})
74
+ uri = URI.parse(path)
75
+
76
+ req = Net::HTTP::Post.new(uri.path)
77
+ req.body = body.to_json
78
+ req.add_field("Content-Type", "application/json")
79
+
80
+ Net::HTTP.new(uri.host, uri.port).start do |http|
81
+ http.request(req)
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ class Client
88
+ include UCEngine::NetHttpResponse
89
+ include UCEngine::NetHttpRequest
90
+
91
+ class Session
92
+ include UCEngine::NetHttpResponse
93
+ include UCEngine::NetHttpRequest
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,21 @@
1
+ module UCEngine
2
+ class Client
3
+ class HttpError < StandardError
4
+ attr_reader :code, :description, :uri
5
+
6
+ def initialize code, description, uri=nil
7
+ @code = code
8
+ @description = description
9
+ @uri = uri
10
+ end
11
+
12
+ def to_s
13
+ "<#{self.class.name} ##{@code} \"#{@description}\" #{@uri}>"
14
+ end
15
+ end
16
+
17
+ class UCError < HttpError
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,46 @@
1
+ class Fixnum
2
+ def days
3
+ return hours * 24
4
+ end
5
+
6
+ def hours
7
+ return mins * 60
8
+ end
9
+
10
+ def mins
11
+ return self * 60
12
+ end
13
+
14
+ def secs
15
+ return self
16
+ end
17
+
18
+ alias :day :days
19
+ alias :hour :hours
20
+ alias :min :mins
21
+ alias :sec :secs
22
+ end
23
+
24
+
25
+ class Float
26
+ def days
27
+ return hours * 24
28
+ end
29
+
30
+ def hours
31
+ return mins * 60
32
+ end
33
+
34
+ def mins
35
+ return self * 60
36
+ end
37
+
38
+ def secs
39
+ return self
40
+ end
41
+
42
+ alias :day :days
43
+ alias :hour :hours
44
+ alias :min :mins
45
+ alias :sec :secs
46
+ end
metadata CHANGED
@@ -1,135 +1,177 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: em-ucengine
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Bruno Michel
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-04-14 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: eventmachine
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &15237120 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - "="
28
- - !ruby/object:Gem::Version
29
- hash: 62196357
30
- segments:
31
- - 1
32
- - 0
33
- - 0
34
- - beta
35
- - 3
36
- version: 1.0.0.beta3
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0.beta.4
37
22
  type: :runtime
38
- version_requirements: *id001
39
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: *15237120
25
+ - !ruby/object:Gem::Dependency
40
26
  name: em-http-request
27
+ requirement: &15236620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *15236620
36
+ - !ruby/object:Gem::Dependency
37
+ name: em-synchrony
38
+ requirement: &15236100 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :runtime
41
45
  prerelease: false
42
- requirement: &id002 !ruby/object:Gem::Requirement
46
+ version_requirements: *15236100
47
+ - !ruby/object:Gem::Dependency
48
+ name: em-eventsource
49
+ requirement: &15235540 !ruby/object:Gem::Requirement
43
50
  none: false
44
- requirements:
45
- - - "="
46
- - !ruby/object:Gem::Version
47
- hash: 62196357
48
- segments:
49
- - 1
50
- - 0
51
- - 0
52
- - beta
53
- - 3
54
- version: 1.0.0.beta3
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
55
  type: :runtime
56
- version_requirements: *id002
57
- - !ruby/object:Gem::Dependency
56
+ prerelease: false
57
+ version_requirements: *15235540
58
+ - !ruby/object:Gem::Dependency
58
59
  name: yajl-ruby
60
+ requirement: &15223480 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.8'
66
+ type: :runtime
59
67
  prerelease: false
60
- requirement: &id003 !ruby/object:Gem::Requirement
68
+ version_requirements: *15223480
69
+ - !ruby/object:Gem::Dependency
70
+ name: multipart_body
71
+ requirement: &15222880 !ruby/object:Gem::Requirement
61
72
  none: false
62
- requirements:
73
+ requirements:
63
74
  - - ~>
64
- - !ruby/object:Gem::Version
65
- hash: 27
66
- segments:
67
- - 0
68
- - 8
69
- version: "0.8"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.2'
70
77
  type: :runtime
71
- version_requirements: *id003
72
- - !ruby/object:Gem::Dependency
73
- name: minitest
74
78
  prerelease: false
75
- requirement: &id004 !ruby/object:Gem::Requirement
79
+ version_requirements: *15222880
80
+ - !ruby/object:Gem::Dependency
81
+ name: minitest
82
+ requirement: &15222180 !ruby/object:Gem::Requirement
76
83
  none: false
77
- requirements:
84
+ requirements:
78
85
  - - ~>
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 2
83
- - 0
84
- version: "2.0"
86
+ - !ruby/object:Gem::Version
87
+ version: '2.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *15222180
91
+ - !ruby/object:Gem::Dependency
92
+ name: bundler
93
+ requirement: &15221200 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *15221200
102
+ - !ruby/object:Gem::Dependency
103
+ name: rake
104
+ requirement: &15220560 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
85
110
  type: :development
86
- version_requirements: *id004
111
+ prerelease: false
112
+ version_requirements: *15220560
113
+ - !ruby/object:Gem::Dependency
114
+ name: yard
115
+ requirement: &15219480 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *15219480
124
+ - !ruby/object:Gem::Dependency
125
+ name: em-spec
126
+ requirement: &15217700 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *15217700
87
135
  description: A library for U.C.Engine, powered by EventMachine
88
136
  email: bruno.michel@af83.com
89
137
  executables: []
90
-
91
138
  extensions: []
92
-
93
- extra_rdoc_files:
139
+ extra_rdoc_files:
94
140
  - README.md
95
- files:
141
+ files:
96
142
  - LICENSE
97
143
  - README.md
98
144
  - Gemfile
145
+ - lib/em-ucengine/brick_test.rb
146
+ - lib/em-ucengine/client_em.rb
147
+ - lib/em-ucengine/utils.rb
148
+ - lib/em-ucengine/client_nethttp.rb
149
+ - lib/em-ucengine/errors.rb
150
+ - lib/em-ucengine/client.rb
151
+ - lib/em-ucengine/brick.rb
99
152
  - lib/em-ucengine.rb
100
- has_rdoc: true
101
153
  homepage: http://github.com/af83/ucengine.em
102
154
  licenses: []
103
-
104
155
  post_install_message:
105
156
  rdoc_options: []
106
-
107
- require_paths:
157
+ require_paths:
108
158
  - lib
109
- required_ruby_version: !ruby/object:Gem::Requirement
159
+ required_ruby_version: !ruby/object:Gem::Requirement
110
160
  none: false
111
- requirements:
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- hash: 3
115
- segments:
116
- - 0
117
- version: "0"
118
- required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
166
  none: false
120
- requirements:
121
- - - ">="
122
- - !ruby/object:Gem::Version
123
- hash: 3
124
- segments:
125
- - 0
126
- version: "0"
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
127
171
  requirements: []
128
-
129
172
  rubyforge_project:
130
- rubygems_version: 1.5.2
173
+ rubygems_version: 1.8.10
131
174
  signing_key:
132
175
  specification_version: 3
133
176
  summary: A library for U.C.Engine, powered by EventMachine
134
177
  test_files: []
135
-