cocainum 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1e3eed6ffd3ca8cece471c859d71dc417821bf5
4
+ data.tar.gz: 4104780a5e80f70291a38519b5e3a11ebc71fada
5
+ SHA512:
6
+ metadata.gz: fb426094a1d27f49dc55580adbb505ec4db9737dec920507a7bdef9a6a4a9a20d7165ad7969b416ab944d869d52b459a7248a59d0e5038bfd19c5e339b2d2a59
7
+ data.tar.gz: 3294c81b8ce1c39b11a6e481fb7296e0eca8089b388f2da7d256877f33f64cd6ceca41590ca7896fe6d9d890e0079a65e5404eb94fd73e70876e07b23f1bb797
@@ -0,0 +1,3 @@
1
+ # [IDE specific]
2
+ .idea
3
+ *.iml
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
File without changes
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path('../lib', __FILE__)
3
+
4
+ require 'version'
5
+
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'cocainum'
9
+ s.version = Cocainum::VERSION
10
+ #s.homepage = 'https://github.com/cocaine/cocaine-framework-ruby'
11
+ s.licenses = ['Ruby', 'LGPLv3']
12
+
13
+ s.authors = ['Andrey Elmanov']
14
+ s.email = ['andrey@clodo.ru']
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.extensions = []
18
+
19
+ s.summary = 'Ruby/Cocainum library'
20
+ s.description = "Extend for Cocaine Framework"
21
+
22
+ #s.add_development_dependency 'rspec'
23
+
24
+ s.add_runtime_dependency 'cocaine-framework-ruby'
25
+ s.add_runtime_dependency 'nokogiri'
26
+ s.add_runtime_dependency 'msgpack'
27
+ end
@@ -0,0 +1,107 @@
1
+ require 'version'
2
+
3
+ require 'cocaine'
4
+ require 'em-synchrony'
5
+ require 'socket'
6
+ require 'nokogiri'
7
+
8
+ require 'cocaine/synchrony/service'
9
+
10
+ class Cocaine::Locator < Cocaine::AbstractService
11
+ private
12
+ def do_resolve(name, df)
13
+ node = ''
14
+ if name.include? '/'
15
+ node, service_name = name.split('/', 2)
16
+ if node == (self.class.default_host == 'localhost' ? Socket.gethostname : self.class.default_host)
17
+ name = service_name
18
+ end
19
+ end
20
+
21
+ $log.debug "resolving service '#{name}'"
22
+ channel = invoke 0, name
23
+ channel.callback do |future|
24
+ begin
25
+ res = future.get
26
+ if node != '' && res[0][0] != node
27
+ raise Cocaine::ServiceError.new 'the specified service is not available in the group'
28
+ else
29
+ df.succeed future.get
30
+ end
31
+ rescue Exception => err
32
+ df.fail err
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ class Nokogiri::XML::Node
39
+ TYPENAMES = {1=>'element',2=>'attribute',3=>'text',4=>'cdata',8=>'comment'}
40
+ def to_hash
41
+ [name.to_sym, case TYPENAMES[node_type]
42
+ when 'element'
43
+ node = {}
44
+ attribute_nodes.each {|attr| node[attr.name.to_sym] = attr.value}
45
+ children.each {|chld|
46
+ c = chld.to_hash
47
+ node[c[0].to_sym] = c[1] unless c[1] == nil
48
+ }
49
+ node
50
+ when 'text'
51
+ text.strip.empty? ? nil : text.strip
52
+ else
53
+ nil
54
+ end]
55
+ end
56
+ end
57
+
58
+ class Nokogiri::XML::Document
59
+ def to_hash; Hash[[root.to_hash]]; end
60
+ end
61
+
62
+ class Array
63
+ def to_xml(key)
64
+ map do |v|
65
+ if Hash === v
66
+ v.to_xml(false, key)
67
+ else
68
+ '<%s>%s</%s>' % [key, v, key]
69
+ end
70
+ end.join
71
+ end
72
+ end
73
+
74
+ class Hash
75
+ def to_xml(recurse = false, default_root = 'root')
76
+ attributes = []
77
+ children = []
78
+ each do |k, v|
79
+ if Hash === v
80
+ a, c = v.to_xml(true)
81
+ if c.size > 0
82
+ children << '<%s%s>%s</%s>' % [k, a.size > 0 ? ' ' + a.join(' ') : '', c.join, k]
83
+ else
84
+ children << '<%s%s />' % [k, a.size > 0 ? ' ' + a.join(' ') : '']
85
+ end
86
+ elsif Array === v
87
+ key = v.shift
88
+ children << '<%s>%s</%s>' % [k, v.to_xml(key), k]
89
+ else
90
+ attributes << '%s="%s"' % [k, v]
91
+ end
92
+ end
93
+ if recurse
94
+ [attributes, children]
95
+ else
96
+ if children.size > 1 || attributes.size > 0
97
+ if children.size > 0
98
+ '<%s%s>%s</%s>' % [default_root, attributes.size > 0 ? ' ' + attributes.join(' ') : '', children.join, default_root]
99
+ else
100
+ '<%s %s />' % [default_root, attributes.join(' ')]
101
+ end
102
+ else
103
+ children[0]
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,167 @@
1
+ require 'cocainum'
2
+ require 'cocaine/server/http'
3
+ require 'json'
4
+ require 'nokogiri'
5
+
6
+ module Cocainum
7
+
8
+ class ApiRequest
9
+ attr_accessor :accept, :request, :action, :version, :method, :content_type
10
+
11
+ def initialize
12
+ @accept = :json
13
+ @version = ''
14
+ end
15
+ end
16
+
17
+
18
+ class Api
19
+ extend Cocaine::Http
20
+
21
+ def initialize
22
+ @handlers = {}
23
+ end
24
+
25
+ def on(method, event, handler)
26
+ @handlers[method] = {} unless @handlers.key?(method)
27
+ @handlers[method][event] = handler
28
+ end
29
+
30
+ def prepare_array_for_json(a)
31
+ a.shift
32
+ a.map do |item|
33
+ if Array === item
34
+ prepare_array_for_json(item)
35
+ elsif Hash === item
36
+ prepare_hash_for_json(item)
37
+ else
38
+ item
39
+ end
40
+ end
41
+ end
42
+
43
+ def prepare_hash_for_json(a)
44
+ Hash[a.map do |k, v|
45
+ if Array === v
46
+ v = prepare_array_for_json(v)
47
+ elsif Hash === v
48
+ v = prepare_hash_for_json(v)
49
+ end
50
+ [k, v]
51
+ end]
52
+ end
53
+
54
+ def execute(request, response)
55
+ df = request.read
56
+ df.callback do |rq|
57
+
58
+ EM.synchrony do
59
+ begin
60
+ url = rq.url.to_s
61
+ url_items = url.split('/')
62
+ url_items.shift if url_items[0].empty?
63
+
64
+ api_req = ApiRequest.new
65
+ api_req.method = rq.method
66
+
67
+ # version detection ###########################
68
+ url_items[0].match(/^v[0-9.]+/) { |m|
69
+ api_req.version = m[0]
70
+ url_items.shift
71
+ }
72
+
73
+ # format detection ############################
74
+ format = url.match(/\.([a-z]+)$/)
75
+ if format && %w(json xml).include?(format[1])
76
+ api_req.accept = format[1].to_sym
77
+ elsif rq.headers.include?('Accept')
78
+ header_accept = Hash[rq.headers['Accept'].split(',').map { |row|
79
+ params = row.split(';')
80
+ ver = ''
81
+ row.match(/format=(v[0-9.]+)/) { |m| ver = m[1]}
82
+
83
+ [params[0], {:version => ver}]
84
+ }]
85
+ if header_accept.key?('application/json')
86
+ api_req.accept = :json
87
+ api_req.version = header_accept['application/json'][:version]
88
+ elsif header_accept.key?('application/xml')
89
+ api_req.accept = :xml
90
+ api_req.version = header_accept['application/xml'][:version]
91
+ end
92
+ end
93
+
94
+ # content-type ################################
95
+ api_req.content_type = rq.headers.key?('Content-Type') ? rq.headers['Content-Type'] : 'text/plain'
96
+
97
+ # body parse ##################################
98
+ api_req.request = case api_req.content_type
99
+ when 'application/json'
100
+ JSON.parse(rq.body, {symbolize_names:true})
101
+ when 'application/xml'
102
+ #(Nokogiri::XML(rq.body) do |config|
103
+ # config.options = Nokogiri::XML::ParseOptions.STRICT | Nokogiri::XML::ParseOptions.NONET
104
+ #end).to_hash
105
+
106
+ Nokogiri::XML(rq.body).to_hash
107
+ else
108
+ {}
109
+ end
110
+ api_req.action = url_items[0]
111
+
112
+ api_resp = if @handlers[api_req.method] && @handlers[api_req.method][api_req.action]
113
+ @handlers[api_req.method][api_req.action].call(api_req)
114
+ else
115
+ {:code => 400, :response => {:error_details => 'method unknown ' + api_req.method + ': ' + api_req.action + ' (' + @handlers.inspect + ')'}}
116
+ end
117
+
118
+
119
+ case api_req.accept
120
+ when :json
121
+ api_resp[:response] = prepare_hash_for_json(api_resp[:response])
122
+ msg = JSON.generate(api_resp[:response])
123
+ content_type = 'application/json'
124
+ when :xml
125
+ msg = api_resp[:response].to_xml
126
+ content_type = 'application/xml'
127
+ else
128
+ content_type = 'plain/html'
129
+ msg = 'error'
130
+ end
131
+ =begin
132
+ msg = 'Message: ' + (rq.query.key?('message') ? rq.query['message'][0] : '') + '<br/>Body: ' + rq.body + '<br/>Headers: ' + rq.headers.inspect + '<br/>Test: '
133
+
134
+ service = Cocaine::Synchrony::Service.new 'DummyService'
135
+ ch = service.enqueue('write_dummy', 'sent this message123')
136
+ msg1 = ch.collect(1)
137
+ msg += '<br/>URL: ' + url
138
+ msg += '<br/>Method: ' + api_req.method
139
+ msg += '<br/>Version: ' + api_req.version
140
+ msg += '<br/>Accept: ' + api_req.accept.to_s
141
+ msg += '<br/>Content-type: ' + api_req.content_type
142
+ msg += '<br/>Query: ' + api_req.request.inspect
143
+ msg += '<br/>' + msg1.inspect
144
+ =end
145
+
146
+ headers = [['Content-Type', content_type + '; charset=UTF-8'],
147
+ ['Content-Length', msg.size.to_s],
148
+ ['Expires', 'Thu, 19 Nov 1981 08:52:00 GMT'],
149
+ ['Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0']
150
+ ]
151
+ api_resp[:headers].each {|k, v|
152
+ headers << [k, v]
153
+ }
154
+ rescue Exception=> e
155
+ msg = 'Message: ' + e.message + '<br/>'
156
+ msg += 'Stacktrace:<br/>' + e.backtrace.join('<br/>')
157
+ end
158
+
159
+ response.write_headers(api_resp[:code], headers)
160
+ response.body = msg
161
+ response.close
162
+ end
163
+ end
164
+ end
165
+ http :execute
166
+ end
167
+ end
@@ -0,0 +1,3 @@
1
+ module Cocainum
2
+ VERSION = '0.4'
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocainum
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Elmanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cocaine-framework-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: msgpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Extend for Cocaine Framework
56
+ email:
57
+ - andrey@clodo.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.md
65
+ - cocainum.gemspec
66
+ - lib/cocainum.rb
67
+ - lib/cocainum/api.rb
68
+ - lib/version.rb
69
+ homepage:
70
+ licenses:
71
+ - Ruby
72
+ - LGPLv3
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.14
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Ruby/Cocainum library
94
+ test_files: []