Module: ClientApiBuilder::Router::ClassMethods

Defined in:
lib/client_api_builder/router.rb

Constant Summary collapse

REQUIRED_BODY_HTTP_METHODS =
[
  :post,
  :put,
  :patch
]

Instance Method Summary collapse

Instance Method Details

#base_url(url = nil) ⇒ Object



29
30
31
32
33
# File 'lib/client_api_builder/router.rb', line 29

def base_url(url = nil)
  return default_options[:base_url] unless url

  add_value_to_class_method(:default_options, base_url: url)
end

#build_query(query) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/client_api_builder/router.rb', line 61

def build_query(query)
  case query_builder
  when :to_query
    query.to_query
  when :query_params
    ClientApiBuilder::QueryParams.to_query(query)
  else
    query_builder.call(query)
  end
end

#connection_option(name, value) ⇒ Object



47
48
49
50
51
# File 'lib/client_api_builder/router.rb', line 47

def connection_option(name, value)
  connection_options = default_options[:connection_options].dup
  connection_options[name] = value
  add_value_to_class_method(:default_options, connection_options: connection_options)
end

#connection_options ⇒ Object



57
58
59
# File 'lib/client_api_builder/router.rb', line 57

def connection_options
  default_options[:connection_options]
end

#default_options ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/client_api_builder/router.rb', line 20

def default_options
  {
    base_url: nil,
    headers: {},
    connection_options: {},
    query_builder: Hash.method_defined?(:to_query) ? :to_query : :query_params
  }.freeze
end

#generate_route_code(method_name, path, options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/client_api_builder/router.rb', line 135

def generate_route_code(method_name, path, options = {})
  http_method = options[:method] || http_method(method_name)

  path_arguments = []
  path.gsub!(/:([a-z0-9_]+)/i) do |_|
    path_arguments << $1
    "#\{#{$1}\}"
  end

  has_body_param = options[:body].nil? && requires_body?(http_method, options)

  query =
    if options[:query]
      query_arguments = get_arguments(options[:query])
      str = options[:query].inspect
      str.gsub!(/"__\|\|(.+?)\|\|__"/) { $1 }
      str
    else
      query_arguments = []
      'nil'
    end

  body =
    if options[:body]
      has_body_param = false
      body_arguments = get_arguments(options[:body])
      str = options[:body].inspect
      str.gsub!(/"__\|\|(.+?)\|\|__"/) { $1 }
      str
    else
      body_arguments = []
      has_body_param ? 'body' : 'nil'
    end

  query_arguments.map!(&:to_s)
  body_arguments.map!(&:to_s)
  named_arguments = path_arguments + query_arguments + body_arguments
  named_arguments.uniq!

  expected_response_codes =
    if options[:expected_response_codes]
      options[:expected_response_codes]
    elsif options[:expected_response_code]
      [options[:expected_response_code]]
    else
      []
    end
  expected_response_codes.map!(&:to_s)

  method_args = named_arguments.map { |arg_name| "#{arg_name}:" }
  method_args += ['body:'] if has_body_param
  method_args += ['**__options__', '&block']

  code = "def #{method_name}(" + method_args.join(', ') + ")\n"
  code += "  __path__ = \"#{path}\"\n"
  code += "  __query__ = #{query}\n"
  code += "  __body__ = #{body}\n"
  code += "  __expected_response_codes__ = #{expected_response_codes.inspect}\n"
  code += "  __uri__ = build_uri(__path__, __query__, __options__)\n"
  code += "  __body__ = build_body(__body__, __options__)\n"
  code += "  __headers__ = build_headers(__options__)\n"
  code += "  __connection_options__ = build_connection_options(__options__)\n"
  code += "  @response = request(method: #{http_method.inspect}, uri: __uri__, body: __body__, headers: __headers__, connection_options: __connection_options__)\n"
  code += "  expected_response_code!(@response, __expected_response_codes__, __options__)\n"
  code += "  handle_response(@response, __options__, &block)\n"
  code += "end\n"
  code
end

#get_arguments(value) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/client_api_builder/router.rb', line 124

def get_arguments(value)
  case value
  when Hash
    get_hash_arguments(value)
  when Array
    get_array_arguments(value)
  else
    []
  end
end

#get_array_arguments(list) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/client_api_builder/router.rb', line 108

def get_array_arguments(list)
  arguments = []
  list.each_with_index do |v, idx|
    case v
    when Symbol
      list[idx] = "__||#{v}||__"
      arguments << v
    when Hash
      arguments += get_hash_arguments(v)
    when Array
      arguments += get_array_arguments(v)
    end
  end
  arguments
end

#get_hash_arguments(hsh) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/client_api_builder/router.rb', line 92

def get_hash_arguments(hsh)
  arguments = []
  hsh.each do |k, v|
    case v
    when Symbol
      hsh[k] = "__||#{v}||__"
      arguments << v
    when Hash
      arguments += get_hash_arguments(v)
    when Array
      arguments += get_array_arguments(v)
    end
  end
  arguments
end

#header(name, value = nil, &block) ⇒ Object



41
42
43
44
45
# File 'lib/client_api_builder/router.rb', line 41

def header(name, value = nil, &block)
  headers = default_options[:headers].dup
  headers[name] = value || block
  add_value_to_class_method(:default_options, headers: headers)
end

#headers ⇒ Object



53
54
55
# File 'lib/client_api_builder/router.rb', line 53

def headers
  default_options[:headers]
end

#http_method(method_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/client_api_builder/router.rb', line 72

def http_method(method_name)
  case method_name.to_s
  when /^(?:post|create|add|insert)/i
    :post
  when /^(?:put|update|modify|change)/i
    :put
  when /^(?:delete|remove)/i
    :delete
  else
    :get
  end
end

#query_builder(builder = nil) ⇒ Object



35
36
37
38
39
# File 'lib/client_api_builder/router.rb', line 35

def query_builder(builder = nil)
  return default_options[:query_builder] unless builder

  add_value_to_class_method(:default_options, query_builder: builder)
end

#requires_body?(http_method, options) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/client_api_builder/router.rb', line 85

def requires_body?(http_method, options)
  return !options[:no_body] if options.key?(:no_body)
  return options[:has_body] if options.key?(:has_body)

  REQUIRED_BODY_HTTP_METHODS.include?(http_method)
end

#route(method_name, path, options = {}) ⇒ Object



204
205
206
# File 'lib/client_api_builder/router.rb', line 204

def route(method_name, path, options = {})
  self.class_eval generate_route_code(method_name, path, options), __FILE__, __LINE__
end