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
- #base_url(url = nil) ⇒ Object
- #build_query(query) ⇒ Object
- #connection_option(name, value) ⇒ Object
- #connection_options ⇒ Object
- #default_options ⇒ Object
- #generate_route_code(method_name, path, options = {}) ⇒ Object
- #get_arguments(value) ⇒ Object
- #get_array_arguments(list) ⇒ Object
- #get_hash_arguments(hsh) ⇒ Object
- #header(name, value = nil, &block) ⇒ Object
- #headers ⇒ Object
- #http_method(method_name) ⇒ Object
- #query_builder(builder = nil) ⇒ Object
- #query_param(name, value) ⇒ Object
- #query_params ⇒ Object
- #requires_body?(http_method, options) ⇒ Boolean
- #route(method_name, path, options = {}) ⇒ Object
Instance Method Details
#base_url(url = nil) ⇒ Object
30 31 32 33 34 |
# File 'lib/client_api_builder/router.rb', line 30 def base_url(url = nil) return [:base_url] unless url add_value_to_class_method(:default_options, base_url: url) end |
#build_query(query) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/client_api_builder/router.rb', line 72 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
48 49 50 51 52 |
# File 'lib/client_api_builder/router.rb', line 48 def connection_option(name, value) = [:connection_options].dup [name] = value add_value_to_class_method(:default_options, connection_options: ) end |
#connection_options ⇒ Object
64 65 66 |
# File 'lib/client_api_builder/router.rb', line 64 def [:connection_options] end |
#default_options ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/client_api_builder/router.rb', line 20 def { base_url: nil, headers: {}, connection_options: {}, query_params: {}, query_builder: Hash.method_defined?(:to_query) ? :to_query : :query_params }.freeze end |
#generate_route_code(method_name, path, options = {}) ⇒ Object
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 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/client_api_builder/router.rb', line 146 def generate_route_code(method_name, path, = {}) http_method = [:method] || http_method(method_name) path_arguments = [] path.gsub!(/:([a-z0-9_]+)/i) do |_| path_arguments << $1 "#\{#{$1}\}" end has_body_param = [:body].nil? && requires_body?(http_method, ) query = if [:query] query_arguments = get_arguments([:query]) str = [:query].inspect str.gsub!(/"__\|\|(.+?)\|\|__"/) { $1 } str else query_arguments = [] 'nil' end body = if [:body] has_body_param = false body_arguments = get_arguments([:body]) str = [: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 [:expected_response_codes] [:expected_response_codes] elsif [:expected_response_code] [[: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
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/client_api_builder/router.rb', line 135 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/client_api_builder/router.rb', line 119 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/client_api_builder/router.rb', line 103 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
42 43 44 45 46 |
# File 'lib/client_api_builder/router.rb', line 42 def header(name, value = nil, &block) headers = [:headers].dup headers[name] = value || block add_value_to_class_method(:default_options, headers: headers) end |
#headers ⇒ Object
60 61 62 |
# File 'lib/client_api_builder/router.rb', line 60 def headers [:headers] end |
#http_method(method_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/client_api_builder/router.rb', line 83 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
36 37 38 39 40 |
# File 'lib/client_api_builder/router.rb', line 36 def query_builder(builder = nil) return [:query_builder] unless builder add_value_to_class_method(:default_options, query_builder: builder) end |
#query_param(name, value) ⇒ Object
54 55 56 57 58 |
# File 'lib/client_api_builder/router.rb', line 54 def query_param(name, value) query_params = [:query_params].dup query_params[name] = value || block add_value_to_class_method(:default_options, query_params: query_params) end |
#query_params ⇒ Object
68 69 70 |
# File 'lib/client_api_builder/router.rb', line 68 def query_params [:query_params] end |
#requires_body?(http_method, options) ⇒ Boolean
96 97 98 99 100 101 |
# File 'lib/client_api_builder/router.rb', line 96 def requires_body?(http_method, ) return ![:no_body] if .key?(:no_body) return [:has_body] if .key?(:has_body) REQUIRED_BODY_HTTP_METHODS.include?(http_method) end |
#route(method_name, path, options = {}) ⇒ Object
215 216 217 |
# File 'lib/client_api_builder/router.rb', line 215 def route(method_name, path, = {}) self.class_eval generate_route_code(method_name, path, ), __FILE__, __LINE__ end |