Class: FmRest::Spyke::SpykeFormatter

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/fmrest/spyke/spyke_formatter.rb

Overview

Response Faraday middleware for converting FM API's response JSON into Spyke's expected format

Constant Summary collapse

SINGLE_RECORD_RE =
%r(/records/\d+\z).freeze
MULTIPLE_RECORDS_RE =
%r(/records\z).freeze
CONTAINER_RE =
%r(/records/\d+/containers/[^/]+/\d+\z).freeze
FIND_RECORDS_RE =
%r(/_find\b).freeze
SCRIPT_REQUEST_RE =
%r(/script/[^/]+\z).freeze
VALIDATION_ERROR_RANGE =
500..599

Instance Method Summary collapse

Constructor Details

#initialize(app, model) ⇒ SpykeFormatter

Returns a new instance of SpykeFormatter.

Parameters:



36
37
38
39
# File 'lib/fmrest/spyke/spyke_formatter.rb', line 36

def initialize(app, model)
  super(app)
  @model = model
end

Instance Method Details

#on_complete(env) ⇒ Object

Parameters:

  • env (Faraday::Env)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fmrest/spyke/spyke_formatter.rb', line 42

def on_complete(env)
  return unless env.body.is_a?(Hash)

  json = env.body

  case
  when single_record_request?(env)
    env.body = prepare_single_record(json)
  when multiple_records_request?(env), find_request?(env)
    env.body = prepare_collection(json)
  when create_request?(env), update_request?(env), delete_request?(env), container_upload_request?(env)
    env.body = prepare_save_response(json)
  when execute_script_request?(env)
    env.body = build_base_hash(json)
  else
    # Attempt to parse unknown requests too
    env.body = build_base_hash(json)
  end
end