jetra 1.0.2 → 1.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d2a37d746863b2076fd043a0561b673caea40e3
4
- data.tar.gz: 8a0fb45d82a95c46ed9f24173625bb817d3a3c94
3
+ metadata.gz: b57cb07162126a3b98c207192feb0d11aa99d2a9
4
+ data.tar.gz: c49c37e2682d0d3c4dd44df5caed680ea555574e
5
5
  SHA512:
6
- metadata.gz: 3c106b9c11254e4d03c24a6425b06fd9755874c7cad4076b018845427f22ca1c52d3ccf75fb43499d6c92502214415bbea9653021f64ec4755159702378ce116
7
- data.tar.gz: b38d6d356c2cc54d2fe1370a34bff21e1ea887ca10cbc5b253a4ea92db933fe25a012df616d91a0e3e9181d917d8594cefe67fa25b3ded4050cf4d6786387828
6
+ metadata.gz: a6652e95eda371af3a81a84858d3d3672f29243d4f92cc01eb89716fb671eadf5516be3fc05358969d2d6b065497f513207f5b8261eed20c09ffa7acecd6b8d8
7
+ data.tar.gz: c0b6d6b7afb037679a2a41a501e8d232680bdb3ceff55da2aa38b8ddc7c3aec5659aa8b8d2e4c396fe20ae9e595425545714e833bd8a91571875aeb580fa3650
@@ -24,18 +24,18 @@ module Jetra
24
24
 
25
25
  request = Rack::Request.new(env)
26
26
 
27
- route = request.path_info
28
- route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
29
- route[0] = '' if route[0]=="/" #remove first '/' char
30
-
31
- sym_route = route.to_sym
32
-
33
27
  params = indifferent_params(request.params)
34
28
 
35
29
  if @custom_block
36
30
  @custom_block.call(request, params)
37
31
  end
38
32
 
33
+ route = request.path_info
34
+ route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
35
+ route[0] = '' if route[0]=="/" #remove first '/' char
36
+
37
+ sym_route = route.to_sym
38
+
39
39
  if @routes.include?(sym_route)
40
40
  res = @app.call(sym_route, params)
41
41
  else
@@ -0,0 +1,79 @@
1
+
2
+ require 'thrift'
3
+
4
+ require "jetra/adapter/thrift/jetra_types"
5
+ require "jetra/adapter/thrift/jetra_constants"
6
+ require "jetra/adapter/thrift/service"
7
+
8
+ require "set"
9
+ require 'json'
10
+
11
+ module Jetra
12
+
13
+ class ThriftAdapter
14
+
15
+ def initialize(app, &custom_block)
16
+ @app = app
17
+
18
+ @routes = Set.new
19
+ @app.routes.each_key do |route|
20
+ @routes << route
21
+ end
22
+
23
+ @custom_block = custom_block
24
+
25
+ end
26
+
27
+ def call(request)
28
+
29
+ if @custom_block
30
+ @custom_block.call(request)
31
+ end
32
+
33
+ route = request.route || ""
34
+
35
+ params = parse_params(request.params)
36
+
37
+ sym_route = route.to_sym
38
+
39
+ if @routes.include?(sym_route)
40
+ res = @app.call(sym_route, params)
41
+ else
42
+ params[:route] = route
43
+ res = @app.call(:not_found, params)
44
+ end
45
+
46
+ response = Thrift::Response.new
47
+ response.status = res.status
48
+ response.body = res.body.to_json
49
+ response
50
+ end
51
+
52
+ def parse_params(params)
53
+ indifferent_params(JSON.load(params).to_h)
54
+ rescue => boom
55
+ {}
56
+ end
57
+
58
+ # Enable string or symbol key access to the nested params hash.
59
+ def indifferent_params(object)
60
+ case object
61
+ when Hash
62
+ new_hash = indifferent_hash
63
+ object.each { |key, value| new_hash[key] = indifferent_params(value) }
64
+ new_hash
65
+ when Array
66
+ object.map { |item| indifferent_params(item) }
67
+ else
68
+ object
69
+ end
70
+ end
71
+
72
+ # Creates a Hash with indifferent access.
73
+ def indifferent_hash
74
+ Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.10.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ module Jetra
8
+ module Thrift
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.10.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ module Jetra
8
+ module Thrift
9
+ class Request
10
+ include ::Thrift::Struct, ::Thrift::Struct_Union
11
+ ROUTE = 1
12
+ PARAMS = 2
13
+
14
+ FIELDS = {
15
+ ROUTE => {:type => ::Thrift::Types::STRING, :name => 'route'},
16
+ PARAMS => {:type => ::Thrift::Types::STRING, :name => 'params'}
17
+ }
18
+
19
+ def struct_fields; FIELDS; end
20
+
21
+ def validate
22
+ end
23
+
24
+ ::Thrift::Struct.generate_accessors self
25
+ end
26
+
27
+ class Response
28
+ include ::Thrift::Struct, ::Thrift::Struct_Union
29
+ STATUS = 1
30
+ BODY = 2
31
+
32
+ FIELDS = {
33
+ STATUS => {:type => ::Thrift::Types::I32, :name => 'status'},
34
+ BODY => {:type => ::Thrift::Types::STRING, :name => 'body'}
35
+ }
36
+
37
+ def struct_fields; FIELDS; end
38
+
39
+ def validate
40
+ end
41
+
42
+ ::Thrift::Struct.generate_accessors self
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,81 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.10.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ module Jetra
8
+ module Thrift
9
+
10
+ module Service
11
+
12
+ class Client
13
+ include ::Thrift::Client
14
+
15
+ def call(request)
16
+ send_call(request)
17
+ return recv_call()
18
+ end
19
+
20
+ def send_call(request)
21
+ send_message('call', Call_args, :request => request)
22
+ end
23
+
24
+ def recv_call()
25
+ result = receive_message(Call_result)
26
+ return result.success unless result.success.nil?
27
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'call failed: unknown result')
28
+ end
29
+
30
+ end
31
+
32
+ class Processor
33
+ include ::Thrift::Processor
34
+
35
+ def process_call(seqid, iprot, oprot)
36
+ args = read_args(iprot, Call_args)
37
+ result = Call_result.new()
38
+ result.success = @handler.call(args.request)
39
+ write_result(result, oprot, 'call', seqid)
40
+ end
41
+
42
+ end
43
+
44
+ # HELPER FUNCTIONS AND STRUCTURES
45
+
46
+ class Call_args
47
+ include ::Thrift::Struct, ::Thrift::Struct_Union
48
+ REQUEST = 1
49
+
50
+ FIELDS = {
51
+ REQUEST => {:type => ::Thrift::Types::STRUCT, :name => 'request', :class => ::Jetra::Thrift::Request}
52
+ }
53
+
54
+ def struct_fields; FIELDS; end
55
+
56
+ def validate
57
+ end
58
+
59
+ ::Thrift::Struct.generate_accessors self
60
+ end
61
+
62
+ class Call_result
63
+ include ::Thrift::Struct, ::Thrift::Struct_Union
64
+ SUCCESS = 0
65
+
66
+ FIELDS = {
67
+ SUCCESS => {:type => ::Thrift::Types::STRUCT, :name => 'success', :class => ::Jetra::Thrift::Response}
68
+ }
69
+
70
+ def struct_fields; FIELDS; end
71
+
72
+ def validate
73
+ end
74
+
75
+ ::Thrift::Struct.generate_accessors self
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,26 @@
1
+ #用于封装基于jetra的thrift接口客户端,方便使用方调用
2
+
3
+ module Jetra
4
+
5
+ class ThriftClient
6
+
7
+ def initialize(thriftService, availableMethods)
8
+
9
+ @thriftService = thriftService
10
+
11
+ #方法列表是由使用方通过参数配置的。
12
+ availableMethods.each do |methodName|
13
+ add_client_method(methodName)
14
+ end
15
+
16
+ end
17
+
18
+ def add_client_method(methodName)
19
+ define_singleton_method(methodName) do |params={}|
20
+ @thriftService.call(methodName, params)
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Jetra
2
2
 
3
- Version = "1.0.2"
3
+ Version = "1.0.3"
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jetra
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-25 00:00:00.000000000 Z
11
+ date: 2017-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: micro DSL
14
14
  email:
@@ -19,9 +19,14 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/jetra.rb
21
21
  - lib/jetra/adapter/rack.rb
22
+ - lib/jetra/adapter/thrift.rb
23
+ - lib/jetra/adapter/thrift/jetra_constants.rb
24
+ - lib/jetra/adapter/thrift/jetra_types.rb
25
+ - lib/jetra/adapter/thrift/service.rb
22
26
  - lib/jetra/application.rb
23
27
  - lib/jetra/base.rb
24
28
  - lib/jetra/builder.rb
29
+ - lib/jetra/client/thrift.rb
25
30
  - lib/jetra/combiner.rb
26
31
  - lib/jetra/middleware/sample.rb
27
32
  - lib/jetra/middleware/validater.rb
@@ -46,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
51
  version: '0'
47
52
  requirements: []
48
53
  rubyforge_project:
49
- rubygems_version: 2.6.8
54
+ rubygems_version: 2.5.2
50
55
  signing_key:
51
56
  specification_version: 4
52
57
  summary: make it easy to write micro service