Class: Distelli::ServiceBase

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/distelli/serviceframeworksinatra.rb

Constant Summary collapse

LOGGER =

Parse the json config file and get the application name and version to set the log file name

Logging.logger[:root]

Instance Method Summary collapse

Constructor Details

#initializeServiceBase

Returns a new instance of ServiceBase.



34
35
36
37
38
# File 'lib/distelli/serviceframeworksinatra.rb', line 34

def initialize()
  super()
  @xml_marshaller = Distelli::XmlMarshaller.new
  @json_marshaller = Distelli::JsonMarshaller.new
end

Instance Method Details

#add_object(obj) ⇒ Object



40
41
42
43
44
# File 'lib/distelli/serviceframeworksinatra.rb', line 40

def add_object(obj)
  LOGGER.debug("Adding Model object: "+obj.inspect.to_s)
  @xml_marshaller.add_object(obj)
  @json_marshaller.add_object(obj)
end

#marshall_error(error) ⇒ Object



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
118
119
# File 'lib/distelli/serviceframeworksinatra.rb', line 90

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/distelli/serviceframeworksinatra.rb', line 63

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



46
47
48
49
50
# File 'lib/distelli/serviceframeworksinatra.rb', line 46

def register_model(obj_list)
  obj_list.each do |obj|
    add_object(obj)
  end
end

#unmarshall_requestObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/distelli/serviceframeworksinatra.rb', line 52

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