Class: Distelli::ServiceBase
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Distelli::ServiceBase
- Defined in:
- lib/distelli/serviceframeworksinatra.rb
Constant Summary collapse
- LOGGER =
disable :raise_errors disable :show_exceptions disable :dump_errors
Logging.logger[:root]
Instance Method Summary collapse
- #add_object(obj) ⇒ Object
-
#initialize(service_name) ⇒ ServiceBase
constructor
A new instance of ServiceBase.
- #marshall_error(error) ⇒ Object
- #marshall_response(response, extra_headers = nil, http_code = nil) ⇒ Object
- #register_model(obj_list) ⇒ Object
- #unmarshall_request ⇒ Object
Constructor Details
#initialize(service_name) ⇒ ServiceBase
Returns a new instance of ServiceBase.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 26 def initialize(service_name) super() my_layout = Logging::Layouts::Pattern.new(:pattern => "%d:[%l]:[%t]:[%c]:%m\n") rf_appender = Logging::Appenders::RollingFile.new(service_name, :filename => './logs/'+service_name+'.log', :layout => my_layout, :roll_by => 'date', :age => '3600') LOGGER.add_appenders(rf_appender) LOGGER.info("Initialized service: "+service_name) @xml_marshaller = Distelli::XmlMarshaller.new @json_marshaller = Distelli::JsonMarshaller.new end |
Instance Method Details
#add_object(obj) ⇒ Object
38 39 40 41 42 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 38 def add_object(obj) LOGGER.info("Added model object: "+obj.inspect.to_s) @xml_marshaller.add_object(obj) @json_marshaller.add_object(obj) end |
#marshall_error(error) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 88 def marshall_error(error) response_type = get_response_type(request) if error.is_a?(Distelli::BaseException) status error.http_code else error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError") status error.http_code end headers_hash = Hash.new headers_hash[ServiceConstants::SERVER_HEADER] = "DistelliWS" headers_hash[ServiceConstants::REQUEST_ID_HEADER] = get_request_id() if response_type == ServiceConstants::RESPONSE_TYPE_JSON headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON headers(headers_hash) return @json_marshaller.marshall_error(error) elsif response_type == ServiceConstants::RESPONSE_TYPE_XML headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON headers(headers_hash) return @xml_marshaller.marshall_error(error) else LOGGER.error("Invalid Response Type: "+response_type) error = ServerError.new("Invalid response type: "+response_type) status error.http_code headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON headers(headers_hash) return @json_marshaller.marshall_error(error) end end |
#marshall_response(response, extra_headers = nil, http_code = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 61 def marshall_response(response, extra_headers=nil, http_code=nil) response_type = get_response_type(request) if http_code != nil status http_code end headers_hash = Hash.new headers_hash[ServiceConstants::SERVER_HEADER] = "DistelliWS" headers_hash[ServiceConstants::REQUEST_ID_HEADER] = get_request_id() if extra_headers != nil headers_hash.update(extra_headers) end if response_type == ServiceConstants::RESPONSE_TYPE_JSON headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON headers(headers_hash) return @json_marshaller.marshall(response) elsif response_type == ServiceConstants::RESPONSE_TYPE_XML headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_XML headers(headers_hash) return @xml_marshaller.marshall(response) else raise StandardError.new("Invalid Response type: "+response_type) end end |
#register_model(obj_list) ⇒ Object
44 45 46 47 48 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 44 def register_model(obj_list) obj_list.each do |obj| add_object(obj) end end |
#unmarshall_request ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/distelli/serviceframeworksinatra.rb', line 50 def unmarshall_request() content_type = request[ServiceConstants::CONTENT_TYPE_HEADER] if content_type == ServiceConstants::CONTENT_TYPE_JSON return @json_marshaller.unmarshall(request.body) elsif content_type == ServiceConstants::CONTENT_TYPE_XML return @xml_marshaller.unmarshall(request.body) else return request.body end end |