DistelliServiceFrameworkRails 1.0

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,148 @@
1
+ require 'distelli/serviceinterface'
2
+ require 'distelli/servicemarshallers'
3
+
4
+ module DistelliServiceFrameworkRails
5
+
6
+ $xml_marshaller = Distelli::XmlMarshaller.new
7
+ $json_marshaller = Distelli::JsonMarshaller.new
8
+
9
+ def get_request_id()
10
+ request_id = request.headers[Distelli::ServiceConstants::REQUEST_ID_HEADER]
11
+ if request_id != nil
12
+ return request_id
13
+ end
14
+ params = request.parameters
15
+ request_id = params[Distelli::ServiceConstants::REQUEST_ID_PARAM]
16
+ if request_id != nil
17
+ return request_id
18
+ end
19
+
20
+ # Create a new request id
21
+ request_id = SecureRandom.uuid
22
+ request.headers[Distelli::ServiceConstants::REQUEST_ID_HEADER] = request_id
23
+ return request_id
24
+ end
25
+
26
+ def get_response_type()
27
+ response_type = request.headers[Distelli::ServiceConstants::RESPONSE_TYPE_HEADER]
28
+ if response_type != nil
29
+ return validate_response_type(response_type)
30
+ end
31
+
32
+ response_type = nil
33
+ params = request.parameters
34
+ if params != nil
35
+ response_type = params[Distelli::ServiceConstants::RESPONSE_TYPE_PARAM]
36
+ end
37
+ return validate_response_type(response_type)
38
+ end
39
+
40
+ def get_operation()
41
+ # First check to see if there is the operation header.
42
+ # If there is then thats the operation
43
+ # If not then check to see if there is the operation query param
44
+ # If it is then thats the operation
45
+ # else the operation is null
46
+ op_name = request.headers[Distelli::ServiceConstants::OPERATION_HEADER]
47
+ if op_name != nil
48
+ return op_name
49
+ end
50
+
51
+ params = request.parameters
52
+ if params == nil
53
+ return nil
54
+ end
55
+ return params[Distelli::ServiceConstants::OPERATION_PARAM]
56
+ end
57
+
58
+ # def handle_stuff(status, content_type, body)
59
+ # response.status = status
60
+ # response.headers["Content-Type"] = content_type
61
+ # # render :inline => body, :status => status
62
+ # self.response_body = "DistelliSFR Body: "+body
63
+ # end
64
+
65
+ def validate_response_type(response_type)
66
+ if response_type == nil
67
+ return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
68
+ end
69
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
70
+ return response_type
71
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
72
+ return response_type
73
+ else
74
+ # LOGGER.error("Invalid response type: "+response_type+" Defaulting to "+ServiceConstants::RESPONSE_TYPE_JSON)
75
+ return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
76
+ end
77
+ end
78
+
79
+ def unmarshall_request()
80
+ content_type = request.headers[Distelli::ServiceConstants::CONTENT_TYPE_HEADER]
81
+ if content_type == Distelli::ServiceConstants::CONTENT_TYPE_JSON
82
+ return $json_marshaller.unmarshall(request.body)
83
+ elsif content_type == Distelli::ServiceConstants::CONTENT_TYPE_XML
84
+ return $xml_marshaller.unmarshall(request.body)
85
+ else
86
+ return request.body
87
+ end
88
+ end
89
+
90
+ def marshall_response(svc_response, extra_headers=nil, http_code=nil)
91
+ response_type = get_response_type()
92
+ if http_code != nil
93
+ response.status = http_code
94
+ end
95
+
96
+ headers_hash = Hash.new
97
+ headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
98
+ headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
99
+
100
+ if extra_headers != nil
101
+ headers_hash.update(extra_headers)
102
+ end
103
+
104
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
105
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
106
+ response.headers.update(headers_hash)
107
+ self.response_body = $json_marshaller.marshall(svc_response)
108
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
109
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_XML
110
+ response.headers.update(headers_hash)
111
+ self.response_body = $xml_marshaller.marshall(svc_response)
112
+ else
113
+ raise StandardError.new("Invalid Response type: "+response_type)
114
+ end
115
+ end
116
+
117
+ def marshall_error(error)
118
+ response_type = get_response_type()
119
+ if error.is_a?(Distelli::BaseException)
120
+ response.status = error.http_code
121
+ else
122
+ error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError")
123
+ response.status = error.http_code
124
+ end
125
+
126
+ headers_hash = Hash.new
127
+ headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
128
+ headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
129
+
130
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
131
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
132
+ response.headers.update(headers_hash)
133
+ self.response_body = $json_marshaller.marshall_error(error)
134
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
135
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
136
+ response.headers.update(headers_hash)
137
+ self.response_body = $xml_marshaller.marshall_error(error)
138
+ else
139
+ LOGGER.error("Invalid Response Type: "+response_type)
140
+ error = ServerError.new("Invalid response type: "+response_type)
141
+ response.status = error.http_code
142
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
143
+ response.headers.update(headers_hash)
144
+ self.response_body = $json_marshaller.marshall_error(error)
145
+ end
146
+ end
147
+
148
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DistelliServiceFrameworkRails
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rahul Singh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: DistelliServiceInterface
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Distelli Service Framework for Ruby on Rails based servers
47
+ email: rsingh@distelli.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/distelli/serviceframeworkrails.rb
53
+ homepage: http://www.distelli.com/
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.23
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Distelli Service Framework classes for Rails
77
+ test_files: []