Class: Idcf::JsonHyperSchema::Expands::LinkInfoBase

Inherits:
Object
  • Object
show all
Defined in:
lib/idcf/json_hyper_schema/expands/link_info_base.rb

Overview

Link Info Base json schema v4

Direct Known Subclasses

LinkInfoV4

Constant Summary collapse

FULL_HREF_REGEXP =
Regexp.new('\A[a-zA-Z]*:?//').freeze
PARAMS_REGEXP =
Regexp.new(':(.+)|\{([^\}\?]+)\}').freeze
BIND_PARAMS_REGEXP =
Regexp.new('\A#(/|[a-zA-Z_\-])*\Z').freeze
QUERY_PARAMS_REGEXP =
Regexp.new('\{\?(.*)\}').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ LinkInfoBase

Returns a new instance of LinkInfoBase.



13
14
15
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 13

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 7

def data
  @data
end

Instance Method Details

#descriptionObject

description

Returns:

  • String



20
21
22
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 20

def description
  @data.description.nil? ? '' : @data.description
end

#hrefObject

href string

Returns:

  • String



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 48

def href
  href = @data.href
  return '' if href.nil?
  l = []
  href.split('/').each do |v|
    next if v.empty?
    l << v.gsub(PARAMS_REGEXP, '%s').gsub(QUERY_PARAMS_REGEXP, '')
  end
  href_head = href =~ FULL_HREF_REGEXP ? '' : base_href
  "#{href_head}/#{l.join('/')}"
end

#make_params(args) ⇒ Object

make params The price of the rest except for a query parameter

Parameters:

  • args

Returns:

  • Hash



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 130

def make_params(args)
  {}.tap do |result|
    param = args.deep_dup
    next unless param.class == Hash
    param = param.stringify_keys
    make_query_params(args).each do |qk, _qv|
      param.delete(qk)
    end
    properties.each do |pk, _pv|
      result[pk] = param[pk] if param.key?(pk)
    end
  end
end

#make_uri(url_params, params, host = nil) ⇒ Object

make uri

Parameters:

  • url_params (Array)
  • params (Hash)
  • host (String) (defaults to: nil)

Returns:

  • String



115
116
117
118
119
120
121
122
123
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 115

def make_uri(url_params, params, host = nil)
  uri          = URI(make_url(url_params, host))
  query_params = []
  [uri.query, make_query_params(params).to_param].each do |param|
    query_params << param if param.present?
  end
  uri.query = query_params.join('&')
  uri.to_s
end

#methodObject

method

Returns:

  • String



34
35
36
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 34

def method
  @data.method.nil? ? 'get' : @data.method.to_s.downcase
end

#method?(val) ⇒ Boolean

is_method

Returns:

  • (Boolean)

    String



41
42
43
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 41

def method?(val)
  method == val.to_s.downcase
end

#propertiesObject

properties

Returns:

  • Hash



94
95
96
97
98
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 94

def properties
  d = @data.schema
  return {} if d.nil? || d.properties.nil?
  d.properties.deep_dup
end

#query_param_namesObject

http query params ex) /hoge/id/sec?name : [“name”]

Returns:

  • Array



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 79

def query_param_names
  result = [].tap do |list|
    href = @data.href
    next if href.nil?
    params = href =~ QUERY_PARAMS_REGEXP
    next if params.nil?
    list.concat(Regexp.last_match(1).split(','))
  end
  result.concat(properties.keys) if method?('get')
  result.uniq
end

#requiredObject

required

Returns:

  • Array



103
104
105
106
107
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 103

def required
  d = @data.schema
  return [] if d.nil? || d.required.nil?
  d.required.deep_dup
end

#titleObject

title

Returns:

  • String



27
28
29
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 27

def title
  @data.title.nil? ? '' : @data.title
end

#url_param_namesObject

url params ex) /hoge/id/sec?name : [“id”, “sec”]

Returns:

  • Array



64
65
66
67
68
69
70
71
72
73
# File 'lib/idcf/json_hyper_schema/expands/link_info_base.rb', line 64

def url_param_names
  [].tap do |result|
    href = @data.href
    next if href.nil?
    href.split('/').each do |v|
      str = url_param_str(v)
      result << str unless str.empty?
    end
  end
end