Module: ClientApiBuilder::Router
- Defined in:
- lib/client_api_builder/router.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #base_url(options) ⇒ Object
- #build_body(body, options) ⇒ Object
- #build_connection_options(options) ⇒ Object
- #build_headers(options) ⇒ Object
- #build_query(query, options) ⇒ Object
- #build_uri(path, query, options) ⇒ Object
- #expected_response_code!(response, expected_response_codes, options) ⇒ Object
- #handle_response(response, options, &block) ⇒ Object
- #parse_response(response, options) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/client_api_builder/router.rb', line 6 def self.included(base) base.extend InheritanceHelper::Methods base.extend ClassMethods base.include ::ClientApiBuilder::NetHTTP::Request base.attr_reader :response end |
Instance Method Details
#base_url(options) ⇒ Object
209 210 211 |
# File 'lib/client_api_builder/router.rb', line 209 def base_url() self.class.base_url end |
#build_body(body, options) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/client_api_builder/router.rb', line 246 def build_body(body, ) return unless body return body if body.is_a?(String) body.merge!([:body]) if [:body] body.to_json end |
#build_connection_options(options) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/client_api_builder/router.rb', line 233 def () if [:connection_options] self.class..merge([:connection_options]) else self.class. end end |
#build_headers(options) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/client_api_builder/router.rb', line 213 def build_headers() headers = {} add_header_proc = proc do |name, value| headers[name] = if value.is_a?(Proc) instance_eval(&value) elsif value.is_a?(Symbol) send(value) else value end end self.class.headers.each(&add_header_proc) [:headers]&.each(&add_header_proc) headers end |
#build_query(query, options) ⇒ Object
241 242 243 244 |
# File 'lib/client_api_builder/router.rb', line 241 def build_query(query, ) query.merge!([:query]) if [:query] self.class.build_query(query) end |
#build_uri(path, query, options) ⇒ Object
254 255 256 257 258 |
# File 'lib/client_api_builder/router.rb', line 254 def build_uri(path, query, ) uri = URI(base_url() + path) uri.query = build_query(query, ) if query uri end |
#expected_response_code!(response, expected_response_codes, options) ⇒ Object
260 261 262 263 264 265 |
# File 'lib/client_api_builder/router.rb', line 260 def expected_response_code!(response, expected_response_codes, ) return if expected_response_codes.empty? && response.kind_of?(Net::HTTPSuccess) return if expected_response_codes.include?(response.code) raise(::ClientApiBuilder::UnexpectedResponse.new("unexpected response code #{response.code}", response)) end |
#handle_response(response, options, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/client_api_builder/router.rb', line 271 def handle_response(response, , &block) data = case [:return] when :response response when :body response.body else parse_response(response, ) end if block instance_exec(data, &block) else data end end |
#parse_response(response, options) ⇒ Object
267 268 269 |
# File 'lib/client_api_builder/router.rb', line 267 def parse_response(response, ) response.body && JSON.parse(response.body) end |