lhc 8.1.1 → 9.0.0

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: b44be8ec6523410f4c54ef36a395e1087030257a
4
- data.tar.gz: 27f513d349a857372dba50b22b70bd36b9c5766d
3
+ metadata.gz: 7db64d4475586e832f57e7eda776d657d4d01971
4
+ data.tar.gz: bceb7e12c53148d19dff2909cef16d41f8d5094b
5
5
  SHA512:
6
- metadata.gz: f37ea9b11ecb0385761daefbe496be873c3a171c7f89ef04ce4e47e547e7cd5ed355905372d1f90dffe8cf24871a229260614a1001354c4b9eb4603a9df5dc1b
7
- data.tar.gz: afca207fb2293a769242ad8ee9d80b5b9db4625cbe7ab6962c0128f15b2db0f0fa6141ace620d6d3c4bc1b59f35d16e47b40cb6c90357f071de91142bd2c2986
6
+ metadata.gz: c2303382dcfa92216ee760eb7cd2daf53e58df1f3e026e1b6ebda16b1da786d0344992e1a133ed35c4c6bff7d2915ac761e9e8a46b237ac2caa87ba97de20ca9
7
+ data.tar.gz: 704a83dcf65173dffbb9cd7458a81c1bef2e5f4718688a0dc588d54d2694b1ca8a259e7043eb8545978b4aeedcaf94702db99a3d325548737ca3eae2fc66ca46
data/README.md CHANGED
@@ -31,6 +31,15 @@ LHC.json.get(options)
31
31
 
32
32
  Currently supported formats: `json`
33
33
 
34
+ If formats are used, headers for `Content-Type` and `Accept` are set by lhc, but also http bodies are translated by LHC, so you can pass bodies as ruby objects:
35
+
36
+ ```ruby
37
+ LHC.json.post('http://slack', body: { text: 'Hi there' })
38
+ # Content-Type: application/json
39
+ # Accept: application/json
40
+ # Translates body to "{\"text\":\"Hi there\"}" before sending
41
+ ```
42
+
34
43
  ## A request from scratch
35
44
 
36
45
  ```ruby
@@ -88,12 +97,13 @@ LHC.get('http://local.ch', followlocation: true)
88
97
 
89
98
  ## Transfer data through the body
90
99
 
91
- Data that is transfered using the HTTP request body is transfered as you provide it.
92
- Also consider setting the http header for content-type.
100
+ Data that is transfered using the HTTP request body is transfered using the selected format, or the default `json`, so you need to provide it as a ruby object.
101
+
102
+ Also consider setting the http header for content-type or use one of the provided formats, like `LHC.json`.
93
103
 
94
104
  ```ruby
95
105
  LHC.post('http://datastore/v2/feedbacks',
96
- body: feedback.to_json,
106
+ body: feedback,
97
107
  headers: { 'Content-Type' => 'application/json' }
98
108
  )
99
109
  ```
@@ -10,12 +10,16 @@ module LHC::Formats
10
10
  super(options)
11
11
  end
12
12
 
13
- def as_json(response)
14
- parse(response, Hash)
13
+ def as_json(input)
14
+ parse(input, Hash)
15
15
  end
16
16
 
17
- def as_open_struct(response)
18
- parse(response, OpenStruct)
17
+ def as_open_struct(input)
18
+ parse(input, OpenStruct)
19
+ end
20
+
21
+ def to_body(input)
22
+ input.to_json
19
23
  end
20
24
 
21
25
  def to_s
@@ -28,10 +32,10 @@ module LHC::Formats
28
32
 
29
33
  private
30
34
 
31
- def parse(response, object_class)
32
- ::JSON.parse(response.body, object_class: object_class)
35
+ def parse(input, object_class)
36
+ ::JSON.parse(input, object_class: object_class)
33
37
  rescue ::JSON::ParserError => e
34
- raise LHC::ParserError.new(e.message, response)
38
+ raise LHC::ParserError.new(e.message, input)
35
39
  end
36
40
  end
37
41
  end
@@ -11,16 +11,13 @@ class LHC::Zipkin < LHC::Interceptor
11
11
  def before_request
12
12
  return unless dependencies?
13
13
  ZipkinTracer::TraceContainer.with_trace_id(trace_id) do
14
- # add headers even if the current trace_id should not be sampled
15
14
  B3_HEADERS.each { |method, header| request.headers[header] = trace_id.send(method).to_s }
16
- # only sample the current call if we're instructed to do so
17
15
  start_trace! if ::Trace.tracer && trace_id.sampled?
18
16
  end
19
17
  end
20
18
 
21
19
  def after_response
22
- # only sample the current call if we're instructed to do so
23
- return unless dependencies? && trace_id.sampled?
20
+ return unless dependencies?
24
21
  end_trace!
25
22
  end
26
23
 
@@ -42,7 +39,6 @@ class LHC::Zipkin < LHC::Interceptor
42
39
  @trace_id ||= ZipkinTracer::TraceGenerator.new.next_trace_id
43
40
  end
44
41
 
45
- # register a new span with zipkin tracer
46
42
  def span
47
43
  @span ||= ::Trace.tracer.start_span(trace_id, url.path)
48
44
  end
@@ -14,12 +14,12 @@ class LHC::Request
14
14
  self.errors_ignored = options.fetch(:ignored_errors, [])
15
15
  self.options = options.deep_dup || {}
16
16
  self.error_handler = options.delete :error_handler
17
+ self.format = options.delete('format') || LHC::Formats::JSON.new
17
18
  use_configured_endpoint!
18
19
  generate_url_from_template!
19
20
  self.interceptors = LHC::Interceptors.new(self)
20
21
  interceptors.intercept(:before_raw_request)
21
22
  self.raw = create_request
22
- self.format = options.delete('format') || LHC::Formats::JSON.new
23
23
  interceptors.intercept(:before_request)
24
24
  run! if self_executing && !response
25
25
  end
@@ -58,7 +58,10 @@ class LHC::Request
58
58
  end
59
59
 
60
60
  def create_request
61
- request = Typhoeus::Request.new(optionally_encoded_url(options), typhoeusize(options))
61
+ request = Typhoeus::Request.new(
62
+ optionally_encoded_url(options),
63
+ translate_body(typhoeusize(options))
64
+ )
62
65
  request.on_headers do
63
66
  interceptors.intercept(:after_request)
64
67
  interceptors.intercept(:before_response)
@@ -67,6 +70,12 @@ class LHC::Request
67
70
  request
68
71
  end
69
72
 
73
+ def translate_body(options)
74
+ return options if options.fetch(:body, nil).blank?
75
+ options[:body] = format.to_body(options[:body])
76
+ options
77
+ end
78
+
70
79
  def encode_url(url)
71
80
  return url if url.nil?
72
81
  URI.escape(url)
@@ -100,8 +109,8 @@ class LHC::Request
100
109
  def generate_url_from_template!
101
110
  endpoint = LHC::Endpoint.new(options[:url])
102
111
  params =
103
- if options[:body] && options[:body].length && (options[:headers] || {}).fetch('Content-Type', nil) == 'application/json'
104
- JSON.parse(options[:body]).merge(options[:params] || {}).deep_symbolize_keys
112
+ if format && options[:body]&.length
113
+ options[:body].merge(options[:params] || {}).deep_symbolize_keys
105
114
  else
106
115
  options[:params]
107
116
  end
@@ -2,7 +2,7 @@
2
2
  # but made accssible in the ruby world
3
3
  module LHC::Response::Data::Base
4
4
  def as_json
5
- @json ||= (@data || response.format.as_json(response))
5
+ @json ||= (@data || response.format.as_json(response.body))
6
6
  end
7
7
 
8
8
  def as_open_struct
@@ -10,7 +10,7 @@ module LHC::Response::Data::Base
10
10
  if @data
11
11
  JSON.parse(@data.to_json, object_class: OpenStruct)
12
12
  else
13
- response.format.as_open_struct(response)
13
+ response.format.as_open_struct(response.body)
14
14
  end
15
15
  end
16
16
 
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= '8.1.1'
2
+ VERSION ||= '9.0.0'
3
3
  end
@@ -17,23 +17,23 @@ describe LHC do
17
17
  end
18
18
 
19
19
  it 'does a post request when providing a complete url' do
20
- LHC.post('http://datastore/v2/feedbacks', body: feedback.to_json)
20
+ LHC.post('http://datastore/v2/feedbacks', body: feedback)
21
21
  end
22
22
 
23
23
  it 'does a post request when providing the name of a configured endpoint' do
24
24
  url = 'http://{+datastore}/v2/feedbacks'
25
25
  options = { params: { datastore: 'datastore' } }
26
26
  LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
27
- LHC.post(:feedbacks, body: feedback.to_json)
27
+ LHC.post(:feedbacks, body: feedback)
28
28
  end
29
29
 
30
30
  it 'it makes response data available in a rails way' do
31
- response = LHC.post('http://datastore/v2/feedbacks', body: feedback.to_json)
31
+ response = LHC.post('http://datastore/v2/feedbacks', body: feedback)
32
32
  expect(response.data.source_id).to eq 'aaa'
33
33
  end
34
34
 
35
35
  it 'provides response headers' do
36
- response = LHC.post('http://datastore/v2/feedbacks', body: feedback.to_json)
36
+ response = LHC.post('http://datastore/v2/feedbacks', body: feedback)
37
37
  expect(response.headers).to be
38
38
  end
39
39
  end
@@ -23,23 +23,23 @@ describe LHC do
23
23
  end
24
24
 
25
25
  it 'does a post request when providing a complete url' do
26
- LHC.put('http://datastore/v2/feedbacks', body: change.to_json)
26
+ LHC.put('http://datastore/v2/feedbacks', body: change)
27
27
  end
28
28
 
29
29
  it 'does a post request when providing the name of a configured endpoint' do
30
30
  url = 'http://{+datastore}/v2/feedbacks'
31
31
  options = { params: { datastore: 'datastore' } }
32
32
  LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
33
- LHC.put(:feedbacks, body: change.to_json)
33
+ LHC.put(:feedbacks, body: change)
34
34
  end
35
35
 
36
36
  it 'it makes response data available in a rails way' do
37
- response = LHC.put('http://datastore/v2/feedbacks', body: change.to_json)
37
+ response = LHC.put('http://datastore/v2/feedbacks', body: change)
38
38
  expect(response.data.recommended).to eq false
39
39
  end
40
40
 
41
41
  it 'provides response headers' do
42
- response = LHC.put('http://datastore/v2/feedbacks', body: change.to_json)
42
+ response = LHC.put('http://datastore/v2/feedbacks', body: change)
43
43
  expect(response.headers).to be
44
44
  end
45
45
  end
@@ -19,7 +19,7 @@ describe LHC do
19
19
  options = {
20
20
  url: "http://datastore/v2/feedbacks",
21
21
  method: :post,
22
- body: "{}",
22
+ body: {},
23
23
  headers: { 'Content-Type' => 'application/json' }
24
24
  }
25
25
  expect { LHC.request(options) }.not_to raise_error
@@ -13,7 +13,7 @@ describe LHC::Zipkin do
13
13
  trace_id: 'trace_id',
14
14
  parent_id: 'parent_id',
15
15
  span_id: 'span_id',
16
- sampled: true,
16
+ sampled: 'sampled',
17
17
  flags: 'flags'
18
18
  )
19
19
  end
@@ -23,7 +23,7 @@ describe LHC::Zipkin do
23
23
  expect(headers['X-B3-TraceId']).to eq('trace_id')
24
24
  expect(headers['X-B3-ParentSpanId']).to eq('parent_id')
25
25
  expect(headers['X-B3-SpanId']).to eq('span_id')
26
- expect(headers['X-B3-Sampled']).to eq('true')
26
+ expect(headers['X-B3-Sampled']).to eq('sampled')
27
27
  expect(headers['X-B3-Flags']).to eq('flags')
28
28
  end
29
29
  end
@@ -49,60 +49,4 @@ describe LHC::Zipkin do
49
49
  expect(headers['X-B3-Flags']).to be_nil
50
50
  end
51
51
  end
52
-
53
- describe 'creating new spans' do
54
- context 'sampled? is false' do
55
- before(:all) do
56
- ::ZipkinTracer::TraceContainer.setup_mock(
57
- trace_id: 'trace_id',
58
- parent_id: 'parent_id',
59
- span_id: 'span_id',
60
- sampled: false,
61
- flags: 'flags'
62
- )
63
- end
64
-
65
- it 'adds the proper X-B3 headers' do
66
- headers = LHC.get(:local).request.headers
67
- expect(headers['X-B3-TraceId']).to eq('trace_id')
68
- expect(headers['X-B3-ParentSpanId']).to eq('parent_id')
69
- expect(headers['X-B3-SpanId']).to eq('span_id')
70
- expect(headers['X-B3-Sampled']).to eq('false')
71
- expect(headers['X-B3-Flags']).to eq('flags')
72
- end
73
-
74
- it 'does not register a new span' do
75
- # ensure no span was registered, by checking no call on span
76
- expect_any_instance_of(described_class).not_to receive(:span).and_call_original
77
- LHC.get(:local)
78
- end
79
- end
80
-
81
- context 'sampled? is true' do
82
- before(:all) do
83
- ::ZipkinTracer::TraceContainer.setup_mock(
84
- trace_id: 'trace_id',
85
- parent_id: 'parent_id',
86
- span_id: 'span_id',
87
- sampled: true,
88
- flags: 'flags'
89
- )
90
- end
91
-
92
- it 'adds the proper X-B3 headers' do
93
- headers = LHC.get(:local).request.headers
94
- expect(headers['X-B3-TraceId']).to eq('trace_id')
95
- expect(headers['X-B3-ParentSpanId']).to eq('parent_id')
96
- expect(headers['X-B3-SpanId']).to eq('span_id')
97
- expect(headers['X-B3-Sampled']).to eq('true')
98
- expect(headers['X-B3-Flags']).to eq('flags')
99
- end
100
-
101
- it 'does register a new span' do
102
- # ensure a span was registered, by checking call on span
103
- expect_any_instance_of(described_class).to receive(:span).at_least(1).times.and_call_original
104
- LHC.get(:local)
105
- end
106
- end
107
- end
108
52
  end
@@ -18,6 +18,6 @@ describe LHC::Request do
18
18
 
19
19
  it 'considers body when compiling urls' do
20
20
  stub_request(:post, "http://datastore:8080/v2/places/123")
21
- LHC.json.post('http://datastore:8080/v2/places/{id}', body: { id: 123 }.to_json)
21
+ LHC.json.post('http://datastore:8080/v2/places/{id}', body: { id: 123 })
22
22
  end
23
23
  end
@@ -42,7 +42,7 @@ module ZipkinTracer
42
42
  end
43
43
 
44
44
  def sampled
45
- ZipkinTracer::TraceContainer.current.sampled
45
+ 'sampled'
46
46
  end
47
47
 
48
48
  def flags
@@ -50,7 +50,7 @@ module ZipkinTracer
50
50
  end
51
51
 
52
52
  def sampled?
53
- ZipkinTracer::TraceContainer.current.sampled
53
+ true
54
54
  end
55
55
  end
56
56
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.1.1
4
+ version: 9.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-19 00:00:00.000000000 Z
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -368,7 +368,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
368
368
  requirements:
369
369
  - Ruby >= 2.0.0
370
370
  rubyforge_project:
371
- rubygems_version: 2.6.14
371
+ rubygems_version: 2.6.12
372
372
  signing_key:
373
373
  specification_version: 4
374
374
  summary: LocalHttpClient