Module: DistelliServiceFrameworkSinatra

Defined in:
lib/distelli/serviceframeworksinatra.rb

Constant Summary collapse

LOGGER =
Logging.logger[:root]

Instance Method Summary collapse

Instance Method Details

#add_object(obj) ⇒ Object



14
15
16
17
18
# File 'lib/distelli/serviceframeworksinatra.rb', line 14

def add_object(obj)
  LOGGER.debug("Registering Model Object: #{obj.inspect.to_s}")
  $xml_marshaller.add_object(obj)
  $json_marshaller.add_object(obj)
end

#marshall_error(error) ⇒ Object



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
89
90
91
92
93
94
95
# File 'lib/distelli/serviceframeworksinatra.rb', line 64

def marshall_error(error)
  response_type = get_response_type(request)
  if error.is_a?(Distelli::BaseException)
    status error.http_code
  else
    log = Logging::Logger['DistelliFramework']
    log.error("Unhandled Exception: "+error.inspect+" "+error.message+" "+error.backtrace.join("    \n"))
    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[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
  headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()

  if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    headers(headers_hash)
    return $json_marshaller.marshall_error(error)
  elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::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[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/distelli/serviceframeworksinatra.rb', line 37

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[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
  headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()

  if extra_headers != nil
    headers_hash.update(extra_headers)
  end

  if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    headers(headers_hash)
    return $json_marshaller.marshall(response)
  elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::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



20
21
22
23
24
# File 'lib/distelli/serviceframeworksinatra.rb', line 20

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

#unmarshall_requestObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/distelli/serviceframeworksinatra.rb', line 26

def unmarshall_request()
  content_type = request[Distelli::ServiceConstants::CONTENT_TYPE_HEADER]
  if content_type == Distelli::ServiceConstants::CONTENT_TYPE_JSON
    return $json_marshaller.unmarshall(request.body)
  elsif content_type == Distelli::ServiceConstants::CONTENT_TYPE_XML
    return $xml_marshaller.unmarshall(request.body)
  else
    return request.body
  end
end