Module: RouterHelper

Defined in:
lib/router_helper.rb

Instance Method Summary collapse

Instance Method Details

#formatted_exception_message(ex) ⇒ Object



98
99
100
# File 'lib/router_helper.rb', line 98

def formatted_exception_message ex
  return ex.backtrace.join("\n\t").sub("\n\t", ": #{ex}#{ex.class ? " (#{ex.class})" : ''}\n\t")
end

#generate_third_party_urlObject



93
94
95
96
# File 'lib/router_helper.rb', line 93

def generate_third_party_url
  urls = send("services_#{Rails.env}_urls")
  urls.map {|key,values| values.map {|k,v| define_method("#{key}_host_service_#{k}") { v }}}
end

#kafka_log_end_pointsObject



81
82
83
# File 'lib/router_helper.rb', line 81

def kafka_log_end_points
  tool_urls[Rails.env.to_sym][:kafka]
end

#redis_logObject



89
90
91
# File 'lib/router_helper.rb', line 89

def redis_log
  $redis_log ||= Redis.new(redis_log_end_points)
end

#redis_log_end_pointsObject



85
86
87
# File 'lib/router_helper.rb', line 85

def redis_log_end_points
  tool_urls[Rails.env.to_sym][:redis]
end

#rest_client_url(url, _payload = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/router_helper.rb', line 3

def rest_client_url(url, _payload = {})
  payload = _payload[:params] || {}
  payload[:referer_service] = current_micro_service_name
  headers = {"#{jwt_header_name}" => jwt_header_token}
  headers = headers.merge(_payload[:headers]) if _payload[:headers]
  verb = _payload[:method]
  begin
    data = RestClient::Request.execute(method: verb, url: url, payload: payload, headers: headers)
    data = {code: data.code, data: JSON.parse(data.body), headers: data.headers, cookies: data.cookies}
  rescue RestClient::Unauthorized, RestClient::Forbidden => err
    data = JSON.parse(err.response)
  rescue RestClient::ResourceNotFound => ex
    data = {code: 404, error: "Url not found #{url}\n" + formatted_exception_message(ex)}
  rescue RestClient::InternalServerError => ex
    data = {code: 500, error: "Server side exception\n" + formatted_exception_message(ex) }
  rescue Exception => ex
    data = {code: 503, error: formatted_exception_message(ex) }
  end
  data
end

#services_development_urlsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/router_helper.rb', line 24

def services_development_urls
  @_services_development_urls ||= {
    user: {url: "http://localhost", port: 3000},
    horizontal: {url: "http://localhost", port: 3005},
    content: {url: "http://localhost", port: 3004},
    practice: {url: "http://localhost", port: 3001},
    mocktest: {url: "http://localhost", port: 3002},
    payment: {url: "http://localhost", port: 3003}
  }
end

#services_preprod_urlsObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/router_helper.rb', line 46

def services_preprod_urls
  @_services_preprod_urls ||= {
    user: {url: "http://user-microservice", port: 80},
    horizontal: {url: "http://horizontal-microservice", port: 80},
    content: {url: "http://content-microservice", port: 80},
    mocktest: {url: "http://mocktest-microservice", port: 80},
    practice: {url: "http://practice-microservice", port: 80},
    payment: {url: "http://payment-microservice", port: 80}
  }
end

#services_production_urlsObject



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

def services_production_urls
  @_services_production_urls ||= {
    user: {url: "http://user-microservice", port: 80},
    horizontal: {url: "http://horizontal-microservice", port: 80},
    content: {url: "http://content-microservice", port: 80},
    mocktest: {url: "http://mocktest-microservice", port: 80},
    practice: {url: "http://practice-microservice", port: 80},
    payment: {url: "http://payment-microservice", port: 80}
  }
end

#services_staging_urlsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/router_helper.rb', line 35

def services_staging_urls
  @_services_staging_urls ||= {
    user: {url: "http://user-microservice", port: 80},
    horizontal: {url: "http://horizontal-microservice", port: 80},
    content: {url: "http://content-microservice", port: 80},
    mocktest: {url: "http://mocktest-microservice", port: 80},
    practice: {url: "http://practice-microservice", port: 80},
    payment: {url: "http://payment-microservice", port: 80}
  }
end

#services_uriObject



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

def services_uri
  @_services_uri ||= send("services_#{Rails.env}_urls")
end

#tool_urlsObject



72
73
74
75
76
77
78
79
# File 'lib/router_helper.rb', line 72

def tool_urls
  stage = ['10.140.10.48:9092','10.140.10.103:9092','10.140.10.108:9092']
  prod  = ['10.140.10.178:9092', '10.140.10.159:9092', '10.140.10.236:9092']
  {development: {kafka: stage, redis: {host: "10.140.10.195", port: 6379}},
   staging: {kafka: stage, redis: {host: "10.140.10.195", port: 6379}}, 
   preprod: {kafka: stage, redis: {host: "10.140.10.195", port: 6379}}, 
   production: {kafka: prod, redis: {host: "10.140.10.42", port: 6379}}}
end