Class: RegApi2::RequestContract

Inherits:
Object
  • Object
show all
Defined in:
lib/reg_api2/request_contract.rb

Overview

Contract for API requests. Checks for specified required fields. Also checks for optional fields. Take in care :re option.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RequestContract) initialize(opts = {})

Returns a new instance of RequestContract



15
16
17
# File 'lib/reg_api2/request_contract.rb', line 15

def initialize(opts = {})
  @opts = opts
end

Instance Attribute Details

- (Hash) opts (readonly)

Options of contract.

Returns:

  • (Hash)

    Options hash.



13
14
15
# File 'lib/reg_api2/request_contract.rb', line 13

def opts
  @opts
end

Instance Method Details

- (Hash) fields_to_validate

Gets fields to validate

Returns:

  • (Hash)

    Fields to validate.



32
33
34
35
36
37
# File 'lib/reg_api2/request_contract.rb', line 32

def fields_to_validate
  required_fields = to_hash opts[:required]
  optional_fields = to_hash opts[:optional]
  required_fields.keys.each { |key| required_fields[key][:required] = true }
  optional_fields.merge(required_fields)
end

- (Object) to_hash(arr)

Normalizes required and optional fields to the form of Hash with options.

Parameters:

  • arr (NilClass, Hash, Array, etc.)

    Something to normalize.



21
22
23
24
25
26
27
28
# File 'lib/reg_api2/request_contract.rb', line 21

def to_hash arr
  return {}   if arr.nil?
  return arr  if arr.kind_of?(Hash)
  arr = [ arr.to_sym ]  unless arr.kind_of?(Array)
  ret = {}
  arr.each { |key| ret[key.to_sym] = {} }
  ret
end

- (Hash) validate(form)

Validates specified form with required and optional fields.

Parameters:

  • form (Hash)

    Form to validate.

Returns:

  • (Hash)

    Updated form.

Raises:

  • ContractError



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/reg_api2/request_contract.rb', line 106

def validate(form)
  fields = fields_to_validate
  return form  if fields.empty?

  validate_presence_of_required_fields form, fields

  fields.each_pair do |key, opts|
    next  if !form.has_key?(key) || form[key].nil?

    form[key] = validate_re key, form[key], opts
    form[key] = validate_iso_date key, form[key], opts
    form[key] = validate_ipaddr key, form[key], opts
  end

  form
end

- (String) validate_ipaddr(key, value, opts)

Validates specified value with ipaddr field.

Parameters:

  • key (Object)

    Value to validate.

  • value (Object)

    Value to validate.

  • opts (Hash)

    opts with optional ipaddr field.

Returns:

  • (String)

    Updated value



73
74
75
76
77
78
# File 'lib/reg_api2/request_contract.rb', line 73

def validate_ipaddr key, value, opts
  if opts[:ipaddr] == true && value.kind_of?(String)
    value = IPAddr.new(value)
  end
  value.to_s
end

- (Object) validate_iso_date(key, value, opts)

Validates specified value with re field.

Parameters:

  • key (Object)

    Value to validate.

  • value (Object)

    Value to validate.

  • opts (Hash)

    opts with optional re field.

Returns:

  • (Object)

    Updated value



61
62
63
64
65
66
# File 'lib/reg_api2/request_contract.rb', line 61

def validate_iso_date key, value, opts
  if opts[:iso_date]
    return value.strftime("%Y-%m-%d")  if value.respond_to?(:strftime)
  end
  value
end

- (Object) validate_presence_of_required_fields(form, fields)

Validates specified form for presence of all required fields. return void

Parameters:

  • form (Hash)

    Form to validate.

  • fields (Hash)

    Fields to test.

Raises:

  • ContractError



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/reg_api2/request_contract.rb', line 85

def validate_presence_of_required_fields form, fields
  absent_fields = []
  fields.each_pair do |key, opts|
    next  unless opts[:required]
    if !form.has_key?(key) || form[key].nil?
      absent_fields << key
    end
  end
  unless absent_fields.empty?
    raise RegApi2::ContractError.new(
      "Required fields missed: #{absent_fields.join(', ')}",
      absent_fields
    )
  end
  nil
end

- (Object) validate_re(key, value, opts)

Validates specified value with re field.

Parameters:

  • key (Object)

    Value to validate.

  • value (Object)

    Value to validate.

  • opts (Hash)

    opts with optional re field.

Raises:

  • ContractError



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reg_api2/request_contract.rb', line 44

def validate_re key, value, opts
  if opts[:re]
    if value.to_s !~ opts[:re]
      raise RegApi2::ContractError.new(
        "Field #{key} mismatch regular expression: #{value}",
        key
      )
    end
  end
  value
end