microservice_dsl 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a2bf369380148747520edd76b31a7c2b0432636
4
- data.tar.gz: aa81d1d988315d09114519949fae3e1ef87be783
3
+ metadata.gz: 68f80331aa78c068cd49a0857cfdab759b92e526
4
+ data.tar.gz: b8151235152bd251750c315ef3956e370bb438a1
5
5
  SHA512:
6
- metadata.gz: 6c57fa06ad3810d1d3c69909477e38015062dd3147a88bb16a586be82666aae314a77114d7dbbf5ad8a8044b4b4ce0e06fa948cbf1b219345a151b662f07e653
7
- data.tar.gz: adf413de93464447b9fa80d287536819e4710f48552fdbd8906b1cd6e3cbf17e179ca5af3d62c0310c0f9b5827657535d9c184cb4c86470ddf9c1ce8c59547a8
6
+ metadata.gz: 2d3b0ce97cd1f75f76d90a783dd5c4636c81f929d5a6e4d5428078dff37c6e93e2e18e013df40365192a1b95cade93337eef1552e35422d93dec56ac63b38838
7
+ data.tar.gz: 0093d08eab116fa76adc104c3faeb9df18843e2e94d8072a541a25b431a6bb94e6f2e08df9cadd01b8dfcd9f7b52b516971c0b8d90d2c2b9a7201bea46b63110
@@ -1,8 +1,8 @@
1
- require "microservice_dsl/version"
2
- require "typhoeus"
3
- require "json"
4
- require "digest"
5
- require "redis"
1
+ require 'microservice_dsl/version'
2
+ require 'typhoeus'
3
+ require 'json'
4
+ require 'digest'
5
+ require 'redis'
6
6
 
7
7
  module Kernel
8
8
  private
@@ -13,18 +13,20 @@ module Kernel
13
13
  url = ENV["#{microservice.upcase}_URL"] || "http://#{microservice}.#{ENV['MS_DOMAIN'] || Rails.env}:#{ENV["MS_#{microservice.upcase}_PORT"] || '3000'}"
14
14
  url << "/#{args[:path]}" if args[:path]
15
15
  body = (args[:body].is_a? String) ? args[:body] : args[:body].to_json if args[:body]
16
- headers = MicroserviceDSL.default_headers.merge(args[:headers] || {}).merge({MicroserviceDSL.hop_header => MicroserviceDSL.hop_string})
16
+ headers = MicroserviceDSL.default_headers.merge(args[:headers] || {}).merge(MicroserviceDSL.hop_header => MicroserviceDSL.hop_string)
17
17
  method = args[:method] || :get
18
- req = Typhoeus::Request.new(url, method: method, headers: headers, body: body, params: args[:params], timeout: ENV['MS_DEFAULT_TIMEOUT'] || 10)
19
-
20
- if method.to_s.downcase == "get"
18
+ timeout = ENV['MS_DEFAULT_TIMEOUT'].try(:to_f) || 0
19
+ timeout = 10 if timeout == 0
20
+ req = Typhoeus::Request.new(url, method: method, headers: headers, body: body, params: args[:params], timeout: timeout)
21
+
22
+ if method.to_s.casecmp('get').zero?
21
23
  req.on_complete do |res|
22
- if res.code == 200 && etag = res.headers["etag"]
24
+ if res.code == 200 && etag = res.headers['etag']
23
25
  MicroserviceDSL.set_cache(url, etag, res)
24
26
  end
25
27
  end
26
28
  if cached = MicroserviceDSL.get_cache(url, req)
27
- req.options[:headers]["If-None-Match"] = cached[:etag]
29
+ req.options[:headers]['If-None-Match'] = cached[:etag]
28
30
  req.on_complete do |res|
29
31
  if res.code == 304
30
32
  res.options[:response_body] = cached[:response][:body]
@@ -33,12 +35,12 @@ module Kernel
33
35
  end
34
36
  end
35
37
  end
36
-
38
+
37
39
  end
38
40
  req
39
41
  end
40
42
 
41
- def call_microservice(microservice, args={})
43
+ def call_microservice(microservice, args = {})
42
44
  request = prepare_microservice_request microservice, args
43
45
  response = request.run
44
46
  [response.body, response.headers['content-type'], response.code]
@@ -47,19 +49,19 @@ end
47
49
 
48
50
  module MicroserviceDSL
49
51
  def self.redis_reset!
50
- @@redis=nil
52
+ @@redis = nil
51
53
  end
52
-
54
+
53
55
  def self.redis
54
56
  return nil unless caching?
55
- splitted = ENV["MS_CACHE_URL"].split(":")
57
+ splitted = ENV['MS_CACHE_URL'].split(':')
56
58
  host = splitted[0]
57
- port = splitted[1] || "6379"
59
+ port = splitted[1] || '6379'
58
60
  @@redis ||= Redis.new(host: host, port: port)
59
61
  end
60
-
62
+
61
63
  def self.caching?
62
- if ENV["MS_CACHE_URL"]
64
+ if ENV['MS_CACHE_URL']
63
65
  return true
64
66
  else
65
67
  return false
@@ -72,65 +74,61 @@ module MicroserviceDSL
72
74
  data = {
73
75
  etag: etag,
74
76
  response: {
75
- body: res.body,
76
- code: res.code,
77
- headers: res.headers
78
- }
77
+ body: res.body,
78
+ code: res.code,
79
+ headers: res.headers
80
+ }
79
81
  }
80
82
  puts data
81
83
  redis.set([redis_hash_name, cache_key(url, res.request)].join(':'), JSON.dump(data))
82
84
  end
83
-
85
+
84
86
  def self.cache_key(url, req)
85
87
  @@taggers ||= nil
86
- url = url.gsub(":", "|")
87
- unless @@taggers
88
- url
88
+ url = url.tr(':', '|')
89
+ if @@taggers
90
+ @@taggers.map { |t| Digest::MD5.hexdigest(req.options[:headers][t]) }.join('/') + '-' + url
89
91
  else
90
- @@taggers.map {|t| Digest::MD5.hexdigest(req.options[:headers][t])}.join("/") + "-" + url
92
+ url
91
93
  end
92
94
  end
93
-
95
+
94
96
  def self.get_etag(url, req)
95
97
  return nil unless caching?
96
98
  if val = get_cache(url, req)
97
99
  val[:etag]
98
- else
99
- nil
100
100
  end
101
101
  end
102
-
102
+
103
103
  def self.has_cache?(url, etag, req)
104
104
  return nil unless caching?
105
105
  (stored_etag = get_etag(url, req)) && (stored_etag == etag) && stored_etag
106
106
  end
107
-
107
+
108
108
  def self.get_cache(url, req)
109
109
  return nil unless caching?
110
110
  if val = redis.get([redis_hash_name, cache_key(url, req)].join(':'))
111
111
  JSON.parse(val, symbolize_names: true)
112
- else
113
- nil
114
112
  end
115
113
  end
116
-
114
+
117
115
  def self.redis_hash_name
118
- ENV["MS_CACHE_HASH"] || "msdsl"
116
+ ENV['MS_CACHE_HASH'] || 'msdsl'
119
117
  end
120
-
118
+
121
119
  def self.cache_taggers=(taggers)
122
120
  raise unless taggers.is_a?(Array)
123
121
  @@taggers = taggers
124
122
  end
125
-
123
+
126
124
  def self.hop_header
127
- "X-Hop-Count"
125
+ 'X-Hop-Count'
128
126
  end
129
-
127
+
130
128
  def self.rack_hop_header
131
- "HTTP_#{self.hop_header.upcase.gsub("-", "_")}"
129
+ "HTTP_#{hop_header.upcase.tr('-', '_')}"
132
130
  end
133
-
131
+
134
132
  def self.default_headers
135
133
  Thread.current[:microservice_dsl_default_headers] || {}
136
134
  end
@@ -138,39 +136,39 @@ module MicroserviceDSL
138
136
  def self.default_headers=(headers = {})
139
137
  Thread.current[:microservice_dsl_default_headers] = headers
140
138
  end
141
-
139
+
142
140
  def self.current_hop=(hop)
143
141
  Thread.current[:microservice_dsl_current_hop] = hop
144
142
  end
145
-
143
+
146
144
  def self.current_hop
147
- Thread.current[:microservice_dsl_current_hop] = "1" unless Thread.current[:microservice_dsl_current_hop]
145
+ Thread.current[:microservice_dsl_current_hop] = '1' unless Thread.current[:microservice_dsl_current_hop]
148
146
  Thread.current[:microservice_dsl_current_hop]
149
147
  end
150
-
148
+
151
149
  def self.next_hop=(hop)
152
150
  Thread.current[:microservice_dsl_next_hop] = hop
153
151
  end
154
-
152
+
155
153
  def self.next_hop
156
154
  Thread.current[:microservice_dsl_next_hop] = 0 unless Thread.current[:microservice_dsl_next_hop]
157
155
  Thread.current[:microservice_dsl_next_hop] += 1
158
156
  end
159
-
157
+
160
158
  def self.hop_string
161
- [self.current_hop, self.next_hop].join(".")
159
+ [current_hop, next_hop].join('.')
162
160
  end
163
-
161
+
164
162
  def self.hashable_string_for(obj)
165
163
  case obj
166
164
  when Hash
167
- hashable_string_for(obj.sort_by {|sub_obj| sub_obj.first.to_s})
165
+ hashable_string_for(obj.sort_by { |sub_obj| sub_obj.first.to_s })
168
166
  when Array
169
- obj.map {|sub_obj| hashable_string_for(sub_obj)}.to_s
167
+ obj.map { |sub_obj| hashable_string_for(sub_obj) }.to_s
170
168
  else
171
169
  obj.to_s
172
170
  end
173
171
  end
174
172
  end
175
173
 
176
- require "microservice_dsl/railtie" if defined?(Rails)
174
+ require 'microservice_dsl/railtie' if defined?(Rails)
@@ -1,3 +1,3 @@
1
1
  module MicroserviceDSL
2
- VERSION = "0.3.0"
2
+ VERSION = '0.3.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microservice_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincenzo Ferrara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-17 00:00:00.000000000 Z
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  version: '0'
151
151
  requirements: []
152
152
  rubyforge_project:
153
- rubygems_version: 2.5.1
153
+ rubygems_version: 2.4.6
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: Little DSL for interact in a microservice environment