Module: RubyAem::Handlers

Defined in:
lib/ruby_aem/handlers/xml.rb,
lib/ruby_aem/handlers/file.rb,
lib/ruby_aem/handlers/html.rb,
lib/ruby_aem/handlers/json.rb,
lib/ruby_aem/handlers/simple.rb

Overview

Response handlers for no payload.

Class Method Summary collapse

Class Method Details

.file_download(response, response_spec, call_params) ⇒ Object

Handle downloaded file by copying from temporary location to file_path call param. The downloaded file in temporary location will then be deleted. data, status_code, and headers are all returned from RubyAem::Client call.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



29
30
31
32
33
34
35
36
37
# File 'lib/ruby_aem/handlers/file.rb', line 29

def Handlers.file_download(response, response_spec, call_params)

  FileUtils.cp(response.body.path, "#{call_params[:file_path]}/#{call_params[:package_name]}-#{call_params[:package_version]}.zip")
  response.body.delete

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.html_authorizable_id(response, response_spec, call_params) ⇒ Object

Parse authorizable ID from response body data. This is used to get the authorizable ID of a newly created user/group.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_aem/handlers/html.rb', line 30

def Handlers.html_authorizable_id(response, response_spec, call_params)

  html = Nokogiri::HTML(response.body)
  authorizable_id = html.xpath('//title/text()').to_s
  authorizable_id.slice! "Content created #{call_params[:path]}"
  call_params[:authorizable_id] = authorizable_id.sub(/^\//, '')

  message = response_spec['message'] % call_params

  RubyAem::Result.new(message, response)
end

.json_authorizable_id(response, response_spec, call_params) ⇒ Object

Handle JSON response payload containing authorizable ID.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_aem/handlers/json.rb', line 30

def Handlers.json_authorizable_id(response, response_spec, call_params)

  json = JSON.parse(response.body)
  authorizable_id = nil
  if json['success'] == true && json['hits'].length == 1
    authorizable_id = json['hits'][0]['name']
    call_params[:authorizable_id] = authorizable_id
    message = response_spec['message'] % call_params
  else
    message = "User/Group #{call_params[:name]} authorizable ID not found"
  end

  result = RubyAem::Result.new(message, response)
  result.data = authorizable_id
  result
end

.json_package_filter(response, response_spec, call_params) ⇒ Object

Handle package filter JSON payload.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    additional call_paramsrmation

Returns:

  • RubyAem::Result



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_aem/handlers/json.rb', line 73

def Handlers.json_package_filter(response, response_spec, call_params)

  json = JSON.parse(response.body)

  filter = []
  json.each do |key, value|
    if json[key]['root'] != nil
      filter.push(json[key]['root'])
    end
  end

  message = response_spec['message'] % call_params

  result = RubyAem::Result.new(message, response)
  result.data = filter
  result

end

.json_package_service(response, response_spec, call_params) ⇒ Object

Handle package JSON payload. Result status is determined directly by success field.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    additional call_paramsrmation

Returns:

  • RubyAem::Result



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby_aem/handlers/json.rb', line 53

def Handlers.json_package_service(response, response_spec, call_params)

  json = JSON.parse(response.body)

  message = json['msg']
  result = RubyAem::Result.new(message, response)

  if json['success'] == true
    result
  else
    raise RubyAem::Error.new(message, result)
  end
end

.simple(response, response_spec, call_params) ⇒ Object

Simple handler by returning result that contains status and message as configured in conf/spec.yaml AS-IS.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



31
32
33
34
# File 'lib/ruby_aem/handlers/simple.rb', line 31

def Handlers.simple(response, response_spec, call_params)
  message = response_spec['message'] % call_params
  RubyAem::Result.new(message, response)
end

.simple_error(response, response_spec, call_params) ⇒ Object

Simple handler with raised error.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result

Raises:



78
79
80
81
# File 'lib/ruby_aem/handlers/simple.rb', line 78

def Handlers.simple_error(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  raise RubyAem::Error.new(result.message, result)
end

.simple_false(response, response_spec, call_params) ⇒ Object

Simple handler with boolean false result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



54
55
56
57
58
# File 'lib/ruby_aem/handlers/simple.rb', line 54

def Handlers.simple_false(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = false
  result
end

.simple_nil(response, response_spec, call_params) ⇒ Object

Simple handler with nil result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



66
67
68
69
70
# File 'lib/ruby_aem/handlers/simple.rb', line 66

def Handlers.simple_nil(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = nil
  result
end

.simple_true(response, response_spec, call_params) ⇒ Object

Simple handler with boolean true result data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



42
43
44
45
46
# File 'lib/ruby_aem/handlers/simple.rb', line 42

def Handlers.simple_true(response, response_spec, call_params)
  result = Handlers.simple(response, response_spec, call_params)
  result.data = true
  result
end

.xml_package_list(response, response_spec, call_params) ⇒ Object

Handle package list XML by removing non-packages data.

Parameters:

  • response

    HTTP response containing status_code, body, and headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_aem/handlers/xml.rb', line 29

def Handlers.xml_package_list(response, response_spec, call_params)

  xml = Nokogiri::XML(response.body)

  status_code = xml.xpath('//crx/response/status/@code').to_s
  status_text = xml.xpath('//crx/response/status/text()').to_s

  if status_code == '200' && status_text == 'ok'
    message = response_spec['message'] % call_params
    result = RubyAem::Result.new(message, response)
    result.data = xml.xpath('//crx/response/data/packages')
  else
    result = RubyAem::Result.new("Unable to retrieve package list, getting status code #{status_code} and status text #{status_text}", response)
  end

  result
end