module Thron::Routable
Public Class Methods
included(klass)
click to toggle source
# File lib/thron/routable.rb, line 14 def self.included(klass) klass.extend ClassMethods klass.class_eval do include HTTParty end end
info(host, query, body, route, token_id, dash)
click to toggle source
# File lib/thron/routable.rb, line 21 def self.info(host, query, body, route, token_id, dash) info = [ "\n", "*" * 50, 'HTTP REQUEST:', " * host: #{host}", " * url: #{route.url}", " * verb: #{route.verb.upcase}", " * query: #{query.inspect}", " * body: #{body.inspect}", " * headers: #{route.headers(token_id: token_id, dash: dash)}", "*" * 50, "\n" ] puts info if Config::logger::verbose Thron::logger.debug info.join("\n") end
Public Instance Methods
route(options = {}) { |response| ... }
click to toggle source
# File lib/thron/routable.rb, line 49 def route(options = {}) to = options[:to] query = options.fetch(:query) { {} } body = options.fetch(:body) { {} } token_id = options[:token_id] dash = options.fetch(:dash) { true } params = options[:params].to_a route = fetch_route(to, params) body = body.to_json if !body.empty? && route.json? self.class.circuit_breaker.monitor do raw = self.class.send(route.verb, route.url, { query: query, body: body, headers: route.headers(token_id: token_id, dash: dash) }) Routable::info(self.class.default_options[:base_uri], query, body, route, token_id, dash) Response::new(raw).tap do |response| yield(response) if response.is_200? && block_given? end end rescue CircuitBreaker::OpenError Thron::logger.error "Circuit breaker is open for process #{$$}" Response::new(OpenStruct::new(code: 200)) end
Private Instance Methods
fetch_route(to, params)
click to toggle source
# File lib/thron/routable.rb, line 76 def fetch_route(to, params) self.class.routes.fetch(to) { fail NoentRouteError, "#{to} route does not exist!" }.call(params) end