Top Level Namespace

Defined Under Namespace

Modules: Kapellmeister Classes: Array, Hash, Object

Instance Method Summary collapse

Instance Method Details

#generate_full_path(original_path, data) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/kapellmeister/requests_extension.rb', line 63

def generate_full_path(original_path, data)
  path = generate_path(original_path, data)
  query = data.delete(:query_params)&.to_query
  return "?#{query}" if path.blank?

  [path, query].compact_blank!.join('?')
end

#generate_path(original_path, data) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/kapellmeister/requests_extension.rb', line 71

def generate_path(original_path, data)
  return nil unless original_path

  original_path.split('/').map do |part|
    next part unless part.include? '%<'

    data.delete(part.match(/%<(.*)>/).to_a.last.to_sym)
  end.join('/').squeeze('/')
end

#generate_routes(json_scheme) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/kapellmeister/base.rb', line 37

def generate_routes(json_scheme)
  json_scheme.dup.each_with_object({}) do |(key, value), scheme|
    scheme[key] = value.delete(:scheme) if (value.is_a?(Hash) && value.key?(:scheme)) || value.is_a?(String)
    next if value.nil? || value.empty?

    generate_routes(value).map { |deep_key, deep_value| mapping(deep_key, deep_value, key, scheme) }
  end
rescue TypeError
  raise "It seems like wrong routes scheme. #{json_scheme}"
end

#mapping(deep_key, deep_value, key, scheme) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kapellmeister/base.rb', line 48

def mapping(deep_key, deep_value, key, scheme)
  old_path = deep_value[:path].presence || deep_key.to_s
  name = old_path.to_s.split('/').map { |part| part.gsub(/%<.*?>/, '') }.reject(&:empty?)
  use_name_wrapper, use_path_wrapper = use_wrappers?(deep_value.delete(:wrappers))

  deep_value[:path] = use_path_wrapper ? [key, old_path].join('/') : old_path
  new_key = if name.size == 1 && !use_name_wrapper
              deep_key
            else
              use_name_wrapper ? [key, deep_key].join('_').to_sym : deep_key
            end
  scheme[new_key] = deep_value
end

#parsed_query(params, data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kapellmeister/requests_extension.rb', line 35

def parsed_query(params, data)
  return [data, []] if params.blank?

  hash_data, filtered_query = *split_hashes(params)
  required_empty_query, default_data = *hash_data.partition { |elem| elem.values.compact_blank.blank? }
  data = filtered_query.zip([]).to_h.compact_blank.merge(data) if data.is_a?(Hash)
  _optional_data, default_data = *split_optional(default_data)
  data = data.merge(default_data) if !data.blank? || !default_data.blank?

  [data, filtered_query + required_empty_query.map(&:keys).flatten + default_data.keys.flatten]
end

#split_hashes(params) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/kapellmeister/requests_extension.rb', line 47

def split_hashes(params)
  return [[], []] if params.blank?

  case params
  when Array then params.compact_blank.partition { |elem| elem.is_a?(Hash) }
  when Hash then [[params], []]
  else [[], []]
  end
end

#split_optional(params) ⇒ Object



57
58
59
60
61
# File 'lib/kapellmeister/requests_extension.rb', line 57

def split_optional(params)
  return [{}, {}] if params.blank?

  params.inject(:merge).partition { |key, _value|  key == :optional }.map(&:to_h)
end

#to_bool!(value) ⇒ Object



77
78
79
80
81
# File 'lib/kapellmeister/base.rb', line 77

def to_bool!(value)
  convert_options = { 'true' => true, true => true, 'false' => false, false => false }

  convert_options[value]
end

#use_wrappers?(wrappers) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kapellmeister/base.rb', line 62

def use_wrappers?(wrappers)
  default = [true, false]
  return default if wrappers.nil?

  if wrappers.key?(:all)
    all = to_bool!(wrappers[:all])
    default = [all, all]
  else
    default = [to_bool!(wrappers[:name]), default[1]] if wrappers.key?(:name)
    default = [default[0], to_bool!(wrappers[:path])] if wrappers.key?(:path)
  end

  default
end

#valid_body?(data, body) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
# File 'lib/kapellmeister/requests_extension.rb', line 81

def valid_body?(data, body)
  return true if body.blank? || body.is_a?(Hash)

  schema = Object.const_get(body).schema
  result = schema.call(data)
  return data if result.success?

  raise ArgumentError, result.errors.to_h
end

#valid_query?(data, query) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kapellmeister/requests_extension.rb', line 91

def valid_query?(data, query)
  return true if query.blank?

  required_keys = query.map(&:to_sym)

  from_data = data.slice(*required_keys)
  data.except!(*required_keys)
  data[:query_params] ||= {}
  data[:query_params] = data[:query_params].to_h.merge!(from_data)

  different_keys = data[:query_params].transform_keys(&:to_sym)
  return true if required_keys.all? { |key| different_keys.key? key.to_sym }

  raise ArgumentError, "Query params needs keys #{required_keys}"
end