DistelliServiceFrameworkSinatra 1.2 → 1.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.
@@ -1,213 +1,190 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'sinatra/base'
4
3
  require 'logging'
5
4
  require 'distelli/serviceinterface'
6
5
  require 'distelli/servicemarshallers'
7
6
 
8
- module Rack
9
- class CommonLogger
10
- def call(env)
11
- # do nothing
12
- @app.call(env)
13
- end
14
- end
15
- end
7
+ module DistelliServiceFrameworkSinatra
16
8
 
17
- module Distelli
18
- class ServiceBase < Sinatra::Base
9
+ $xml_marshaller = Distelli::XmlMarshaller.new
10
+ $json_marshaller = Distelli::JsonMarshaller.new
19
11
 
20
- # disable :raise_errors
21
- # disable :show_exceptions
22
- # disable :dump_errors
23
- LOGGER = Logging.logger[:root]
24
- LOGGER.level = :debug
12
+ LOGGER = Logging.logger[:root]
25
13
 
26
- def initialize(service_name)
27
- super()
28
- my_layout = Logging::Layouts::Pattern.new(:pattern => "%d:[%l]:[%t]:[%c]:%m\n")
29
- rf_appender = Logging::Appenders::RollingFile.new(service_name, :filename => './logs/'+service_name+'.log', :layout => my_layout, :roll_by => 'date', :age => '3600')
14
+ def add_object(obj)
15
+ LOGGER.debug("Registering Model Object: #{obj.inspect.to_s}")
16
+ $xml_marshaller.add_object(obj)
17
+ $json_marshaller.add_object(obj)
18
+ end
30
19
 
31
- LOGGER.add_appenders(rf_appender)
32
- LOGGER.info("Initialized service: "+service_name)
20
+ def register_model(obj_list)
21
+ obj_list.each do |obj|
22
+ add_object(obj)
23
+ end
24
+ end
33
25
 
34
- @xml_marshaller = Distelli::XmlMarshaller.new
35
- @json_marshaller = Distelli::JsonMarshaller.new
26
+ def unmarshall_request()
27
+ content_type = request[Distelli::ServiceConstants::CONTENT_TYPE_HEADER]
28
+ if content_type == Distelli::ServiceConstants::CONTENT_TYPE_JSON
29
+ return $json_marshaller.unmarshall(request.body)
30
+ elsif content_type == Distelli::ServiceConstants::CONTENT_TYPE_XML
31
+ return $xml_marshaller.unmarshall(request.body)
32
+ else
33
+ return request.body
36
34
  end
35
+ end
37
36
 
38
- def add_object(obj)
39
- LOGGER.info("Added model object: "+obj.inspect.to_s)
40
- @xml_marshaller.add_object(obj)
41
- @json_marshaller.add_object(obj)
37
+ def marshall_response(response, extra_headers=nil, http_code=nil)
38
+ response_type = get_response_type(request)
39
+ if http_code != nil
40
+ status http_code
42
41
  end
43
42
 
44
- def register_model(obj_list)
45
- obj_list.each do |obj|
46
- add_object(obj)
47
- end
43
+ headers_hash = Hash.new
44
+ headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
45
+ headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
46
+
47
+ if extra_headers != nil
48
+ headers_hash.update(extra_headers)
48
49
  end
49
50
 
50
- def unmarshall_request()
51
- content_type = request[ServiceConstants::CONTENT_TYPE_HEADER]
52
- if content_type == ServiceConstants::CONTENT_TYPE_JSON
53
- return @json_marshaller.unmarshall(request.body)
54
- elsif content_type == ServiceConstants::CONTENT_TYPE_XML
55
- return @xml_marshaller.unmarshall(request.body)
56
- else
57
- return request.body
58
- end
51
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
52
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
53
+ headers(headers_hash)
54
+ return $json_marshaller.marshall(response)
55
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
56
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_XML
57
+ headers(headers_hash)
58
+ return $xml_marshaller.marshall(response)
59
+ else
60
+ raise StandardError.new("Invalid Response type: "+response_type)
59
61
  end
62
+ end
60
63
 
61
- def marshall_response(response, extra_headers=nil, http_code=nil)
62
- response_type = get_response_type(request)
63
- if http_code != nil
64
- status http_code
65
- end
66
-
67
- headers_hash = Hash.new
68
- headers_hash[ServiceConstants::SERVER_HEADER] = "DistelliWS"
69
- headers_hash[ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
70
-
71
- if extra_headers != nil
72
- headers_hash.update(extra_headers)
73
- end
74
-
75
- if response_type == ServiceConstants::RESPONSE_TYPE_JSON
76
- headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
77
- headers(headers_hash)
78
- return @json_marshaller.marshall(response)
79
- elsif response_type == ServiceConstants::RESPONSE_TYPE_XML
80
- headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_XML
81
- headers(headers_hash)
82
- return @xml_marshaller.marshall(response)
83
- else
84
- raise StandardError.new("Invalid Response type: "+response_type)
85
- end
64
+ def marshall_error(error)
65
+ response_type = get_response_type(request)
66
+ if error.is_a?(Distelli::BaseException)
67
+ status error.http_code
68
+ else
69
+ log = Logging::Logger['DistelliFramework']
70
+ log.error("Unhandled Exception: "+error.inspect+" "+error.message+" "+error.backtrace.join(" \n"))
71
+ error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError")
72
+ status error.http_code
86
73
  end
87
74
 
88
- def marshall_error(error)
89
- response_type = get_response_type(request)
90
- if error.is_a?(Distelli::BaseException)
91
- status error.http_code
92
- else
93
- error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError")
94
- status error.http_code
95
- end
96
-
97
- headers_hash = Hash.new
98
- headers_hash[ServiceConstants::SERVER_HEADER] = "DistelliWS"
99
- headers_hash[ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
100
-
101
- if response_type == ServiceConstants::RESPONSE_TYPE_JSON
102
- headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
103
- headers(headers_hash)
104
- return @json_marshaller.marshall_error(error)
105
- elsif response_type == ServiceConstants::RESPONSE_TYPE_XML
106
- headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
107
- headers(headers_hash)
108
- return @xml_marshaller.marshall_error(error)
109
- else
110
- LOGGER.error("Invalid Response Type: "+response_type)
111
- error = ServerError.new("Invalid response type: "+response_type)
112
- status error.http_code
113
- headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
114
- headers(headers_hash)
115
- return @json_marshaller.marshall_error(error)
116
- end
75
+ headers_hash = Hash.new
76
+ headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
77
+ headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
78
+
79
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
80
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
81
+ headers(headers_hash)
82
+ return $json_marshaller.marshall_error(error)
83
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
84
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
85
+ headers(headers_hash)
86
+ return $xml_marshaller.marshall_error(error)
87
+ else
88
+ LOGGER.error("Invalid Response Type: "+response_type)
89
+ error = ServerError.new("Invalid response type: "+response_type)
90
+ status error.http_code
91
+ headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
92
+ headers(headers_hash)
93
+ return $json_marshaller.marshall_error(error)
117
94
  end
95
+ end
118
96
 
119
- private
120
- def validate_response_type(response_type)
121
- if response_type == nil
122
- return ServiceConstants::RESPONSE_TYPE_JSON
123
- end
124
- if response_type == ServiceConstants::RESPONSE_TYPE_JSON
125
- return response_type
126
- elsif response_type == ServiceConstants::RESPONSE_TYPE_XML
127
- return response_type
128
- else
129
- LOGGER.error("Invalid response type: "+response_type+" Defaulting to "+ServiceConstants::RESPONSE_TYPE_JSON)
130
- return ServiceConstants::RESPONSE_TYPE_JSON
131
- end
97
+ private
98
+ def validate_response_type(response_type)
99
+ if response_type == nil
100
+ return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
101
+ end
102
+ if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
103
+ return response_type
104
+ elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
105
+ return response_type
106
+ else
107
+ LOGGER.error("Invalid response type: "+response_type+" Defaulting to "+Distelli::ServiceConstants::RESPONSE_TYPE_JSON)
108
+ return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
132
109
  end
110
+ end
133
111
 
134
- def get_request_id()
135
- request_id = request[ServiceConstants::REQUEST_ID_HEADER]
136
- if request_id != nil
137
- return request_id
138
- end
139
- params = request.params()
140
- request_id = params[ServiceConstants::REQUEST_ID_PARAM]
141
- if request_id != nil
142
- return request_id
143
- end
144
-
145
- # Create a new request id
146
- request_id = SecureRandom.uuid
147
- request[ServiceConstants::REQUEST_ID_HEADER] = request_id
112
+ def get_request_id()
113
+ request_id = request[Distelli::ServiceConstants::REQUEST_ID_HEADER]
114
+ if request_id != nil
148
115
  return request_id
149
116
  end
150
-
151
- def get_response_type(request)
152
- response_type = request[ServiceConstants::RESPONSE_TYPE_HEADER]
153
- if response_type != nil
154
- return validate_response_type(response_type)
155
- end
156
-
157
- response_type = nil
158
- params = request.params()
159
- if params != nil
160
- response_type = params[ServiceConstants::RESPONSE_TYPE_PARAM]
161
- end
162
- return validate_response_type(response_type)
117
+ params = request.params()
118
+ request_id = params[Distelli::ServiceConstants::REQUEST_ID_PARAM]
119
+ if request_id != nil
120
+ return request_id
163
121
  end
164
122
 
165
- def get_operation(request)
166
- # First check to see if there is the operation header.
167
- # If there is then thats the operation
168
- # If not then check to see if there is the operation query param
169
- # If it is then thats the operation
170
- # else the operation is null
171
- op_name = request[ServiceConstants::OPERATION_HEADER]
172
- if op_name != nil
173
- return op_name
174
- end
175
-
176
- params = request.params()
177
- if params == nil
178
- return nil
179
- end
180
- return params[ServiceConstants::OPERATION_PARAM]
181
- end
123
+ # Create a new request id
124
+ request_id = SecureRandom.uuid
125
+ request[Distelli::ServiceConstants::REQUEST_ID_HEADER] = request_id
126
+ return request_id
127
+ end
182
128
 
183
- ######################################
184
- # Error Handlers
185
- ######################################
186
- error do
187
- # Marshall the error as a server error
188
- ex = env['sinatra.error']
189
- LOGGER.error('Unhandled Exception: '+ex.inspect+ex.backtrace.join("\n"))
190
- return marshall_error(ex)
129
+ def get_response_type(request)
130
+ response_type = request[Distelli::ServiceConstants::RESPONSE_TYPE_HEADER]
131
+ if response_type != nil
132
+ return validate_response_type(response_type)
191
133
  end
192
134
 
193
- error Distelli::ClientError do
194
- # Marshall the Client Error Exception and set the response code
195
- ce = env['sinatra.error']
196
- marshall_error(ce)
135
+ response_type = nil
136
+ params = request.params()
137
+ if params != nil
138
+ response_type = params[Distelli::ServiceConstants::RESPONSE_TYPE_PARAM]
197
139
  end
140
+ return validate_response_type(response_type)
141
+ end
198
142
 
199
- error Distelli::ServerError do
200
- # Marshall the Server Error Exception and set the response code
201
- se = env['sinatra.error']
202
- return marshall_error(se)
143
+ def get_operation(request)
144
+ # First check to see if there is the operation header.
145
+ # If there is then thats the operation
146
+ # If not then check to see if there is the operation query param
147
+ # If it is then thats the operation
148
+ # else the operation is null
149
+ op_name = request[Distelli::ServiceConstants::OPERATION_HEADER]
150
+ if op_name != nil
151
+ return op_name
203
152
  end
204
153
 
205
- not_found do
206
- # Marshall a Client Error Exception and set the 400 response code
207
- op_name = get_operation(request)
208
-
209
- error = MalformedRequest.new(request.request_method+" not supported on resource: "+request.path)
210
- return marshall_error(error)
154
+ params = request.params()
155
+ if params == nil
156
+ return nil
211
157
  end
158
+ return params[Distelli::ServiceConstants::OPERATION_PARAM]
212
159
  end
160
+
161
+ ######################################
162
+ # Error Handlers
163
+ ######################################
164
+ # error do
165
+ # # Marshall the error as a server error
166
+ # ex = env['sinatra.error']
167
+ # LOGGER.error('Unhandled Exception: '+ex.inspect+ex.backtrace.join("\n"))
168
+ # return marshall_error(ex)
169
+ # end
170
+
171
+ # error Distelli::ClientError do
172
+ # # Marshall the Client Error Exception and set the response code
173
+ # ce = env['sinatra.error']
174
+ # marshall_error(ce)
175
+ # end
176
+
177
+ # error Distelli::ServerError do
178
+ # # Marshall the Server Error Exception and set the response code
179
+ # se = env['sinatra.error']
180
+ # return marshall_error(se)
181
+ # end
182
+
183
+ # not_found do
184
+ # # Marshall a Client Error Exception and set the 400 response code
185
+ # op_name = get_operation(request)
186
+
187
+ # error = MalformedRequest.new(request.request_method+" not supported on resource: "+request.path)
188
+ # return marshall_error(error)
189
+ # end
213
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DistelliServiceFrameworkSinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  version: '0'
87
87
  requirements: []
88
88
  rubyforge_project:
89
- rubygems_version: 1.8.23
89
+ rubygems_version: 1.8.24
90
90
  signing_key:
91
91
  specification_version: 3
92
92
  summary: Distelli Service Framework classes for Sinatra