Module: Thron::Routable

Includes:
HTTParty
Included in:
Gateway::Base
Defined in:
lib/thron/routable.rb

Defined Under Namespace

Modules: ClassMethods Classes: NoentRouteError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



14
15
16
17
18
19
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#route(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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