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(data, status_code, headers, response_spec, info) ⇒ Object

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

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



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

def Handlers.file_download(data, status_code, headers, response_spec, info)

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

  message = response_spec['message'] % info

  RubyAem::Result.new('success', message)
end

.html_authorizable_id(data, status_code, headers, response_spec, info) ⇒ Object

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

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



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

def Handlers.html_authorizable_id(data, status_code, headers, response_spec, info)

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

  status = response_spec['status']
  message = response_spec['message'] % info

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

.json_authorizable_id(data, status_code, headers, response_spec, info) ⇒ Object

Handle JSON response payload containing authorizable ID.

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



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

def Handlers.json_authorizable_id(data, status_code, headers, response_spec, info)

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

  status = response_spec['status']

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

.json_package_filter(data, status_code, headers, response_spec, info) ⇒ Object

Handle package filter JSON payload.

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



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

def Handlers.json_package_filter(data, status_code, headers, response_spec, info)

  json = JSON.parse(data)

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

  message = response_spec['message'] % info

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

end

.json_package_service(data, status_code, headers, response_spec, info) ⇒ Object

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

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



58
59
60
61
62
63
64
65
66
# File 'lib/ruby_aem/handlers/json.rb', line 58

def Handlers.json_package_service(data, status_code, headers, response_spec, info)

  json = JSON.parse(data)

  status = json['success'] == true ? 'success' : 'failure'
  message = json['msg']

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

.simple(data, status_code, headers, response_spec, info) ⇒ Object

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

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



32
33
34
35
36
37
38
# File 'lib/ruby_aem/handlers/simple.rb', line 32

def Handlers.simple(data, status_code, headers, response_spec, info)

  status = response_spec['status']
  message = response_spec['message'] % info

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

.xml_package_list(data, status_code, headers, response_spec, info) ⇒ Object

Handle package list XML by removing non-packages data.

Parameters:

  • data

    data payload

  • status_code

    response HTTP status code

  • headers

    response HTTP headers

  • response_spec

    response specification as configured in conf/spec.yaml

  • info

    additional information

Returns:

  • RubyAem::Result



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

def Handlers.xml_package_list(data, status_code, headers, response_spec, info)

  xml = Nokogiri::XML(data)

  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'] % info
    result = RubyAem::Result.new('success', message)
    result.data = xml.xpath('//crx/response/data/packages')
  else
    result = RubyAem::Result.new('failure', "Unable to retrieve package list, getting status code #{status_code} and status text #{status_text}")
  end

  result
end