lhc 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +36 -0
  3. data/Gemfile +11 -0
  4. data/README.md +116 -0
  5. data/Rakefile +25 -0
  6. data/docs/configuration.md +57 -0
  7. data/docs/exceptions.md +68 -0
  8. data/docs/interceptors.md +90 -0
  9. data/docs/request.md +24 -0
  10. data/docs/response.md +19 -0
  11. data/lhc.gemspec +30 -0
  12. data/lib/lhc.rb +16 -0
  13. data/lib/lhc/concerns/lhc/basic_methods.rb +42 -0
  14. data/lib/lhc/config.rb +45 -0
  15. data/lib/lhc/endpoint.rb +53 -0
  16. data/lib/lhc/error.rb +63 -0
  17. data/lib/lhc/errors/client_error.rb +73 -0
  18. data/lib/lhc/errors/server_error.rb +28 -0
  19. data/lib/lhc/errors/timeout.rb +4 -0
  20. data/lib/lhc/errors/unknown_error.rb +4 -0
  21. data/lib/lhc/interceptor.rb +9 -0
  22. data/lib/lhc/interceptor_processor.rb +24 -0
  23. data/lib/lhc/request.rb +91 -0
  24. data/lib/lhc/response.rb +52 -0
  25. data/lib/lhc/version.rb +3 -0
  26. data/script/ci/build.sh +19 -0
  27. data/spec/basic_methods/delete_spec.rb +34 -0
  28. data/spec/basic_methods/get_spec.rb +37 -0
  29. data/spec/basic_methods/post_spec.rb +42 -0
  30. data/spec/basic_methods/put_spec.rb +48 -0
  31. data/spec/basic_methods/request_spec.rb +19 -0
  32. data/spec/config/endpoints_spec.rb +49 -0
  33. data/spec/config/placeholders_spec.rb +32 -0
  34. data/spec/dummy/README.rdoc +28 -0
  35. data/spec/dummy/Rakefile +6 -0
  36. data/spec/dummy/app/assets/images/.keep +0 -0
  37. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  38. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  39. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  40. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  41. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy/app/mailers/.keep +0 -0
  43. data/spec/dummy/app/models/.keep +0 -0
  44. data/spec/dummy/app/models/concerns/.keep +0 -0
  45. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  46. data/spec/dummy/bin/bundle +3 -0
  47. data/spec/dummy/bin/rails +4 -0
  48. data/spec/dummy/bin/rake +4 -0
  49. data/spec/dummy/config.ru +4 -0
  50. data/spec/dummy/config/application.rb +14 -0
  51. data/spec/dummy/config/boot.rb +5 -0
  52. data/spec/dummy/config/environment.rb +5 -0
  53. data/spec/dummy/config/environments/development.rb +34 -0
  54. data/spec/dummy/config/environments/production.rb +75 -0
  55. data/spec/dummy/config/environments/test.rb +39 -0
  56. data/spec/dummy/config/initializers/assets.rb +8 -0
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  59. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  60. data/spec/dummy/config/initializers/inflections.rb +16 -0
  61. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  62. data/spec/dummy/config/initializers/session_store.rb +3 -0
  63. data/spec/dummy/config/initializers/wrap_parameters.rb +9 -0
  64. data/spec/dummy/config/locales/en.yml +23 -0
  65. data/spec/dummy/config/routes.rb +56 -0
  66. data/spec/dummy/config/secrets.yml +22 -0
  67. data/spec/dummy/lib/assets/.keep +0 -0
  68. data/spec/dummy/log/.keep +0 -0
  69. data/spec/dummy/public/404.html +67 -0
  70. data/spec/dummy/public/422.html +67 -0
  71. data/spec/dummy/public/500.html +66 -0
  72. data/spec/dummy/public/favicon.ico +0 -0
  73. data/spec/endpoint/compile_spec.rb +25 -0
  74. data/spec/endpoint/placeholders_spec.rb +14 -0
  75. data/spec/endpoint/remove_interpolated_params_spec.rb +16 -0
  76. data/spec/error/find_spec.rb +56 -0
  77. data/spec/error/response_spec.rb +17 -0
  78. data/spec/error/timeout_spec.rb +14 -0
  79. data/spec/interceptors/after_request_spec.rb +21 -0
  80. data/spec/interceptors/after_response_spec.rb +42 -0
  81. data/spec/interceptors/before_request_spec.rb +21 -0
  82. data/spec/interceptors/before_response_spec.rb +21 -0
  83. data/spec/interceptors/default_interceptors_spec.rb +15 -0
  84. data/spec/interceptors/define_spec.rb +29 -0
  85. data/spec/interceptors/response_competition_spec.rb +40 -0
  86. data/spec/interceptors/return_response_spec.rb +39 -0
  87. data/spec/rails_helper.rb +4 -0
  88. data/spec/request/error_handling_spec.rb +52 -0
  89. data/spec/request/headers_spec.rb +11 -0
  90. data/spec/request/option_dup_spec.rb +12 -0
  91. data/spec/request/parallel_requests_spec.rb +15 -0
  92. data/spec/request/url_patterns_spec.rb +19 -0
  93. data/spec/response/body_spec.rb +16 -0
  94. data/spec/response/code_spec.rb +16 -0
  95. data/spec/response/data_spec.rb +18 -0
  96. data/spec/response/headers_spec.rb +18 -0
  97. data/spec/response/success_spec.rb +12 -0
  98. data/spec/response/time_spec.rb +16 -0
  99. data/spec/spec_helper.rb +4 -0
  100. data/spec/support/fixtures/json/feedback.json +11 -0
  101. data/spec/support/fixtures/json/feedbacks.json +164 -0
  102. data/spec/support/fixtures/json/localina_content_ad.json +23 -0
  103. data/spec/support/load_json.rb +3 -0
  104. data/spec/support/reset_config.rb +7 -0
  105. data/spec/timeouts/no_signal_spec.rb +13 -0
  106. data/spec/timeouts/timings_spec.rb +59 -0
  107. metadata +313 -0
@@ -0,0 +1,52 @@
1
+ require 'typhoeus'
2
+
3
+ # The response contains the raw response (typhoeus)
4
+ # and provides functionality to access response data.
5
+ class LHC::Response
6
+
7
+ attr_accessor :request
8
+
9
+ # A response is initalized with the underlying raw response (typhoeus in our case)
10
+ # and the associated request.
11
+ def initialize(raw, request)
12
+ self.request = request
13
+ self.raw = raw
14
+ end
15
+
16
+ # Access response data.
17
+ # Cache parsing.
18
+ def data
19
+ @data ||= JSON.parse(raw.body, object_class: OpenStruct)
20
+ @data
21
+ end
22
+
23
+ def body
24
+ raw.body
25
+ end
26
+
27
+ def code
28
+ raw.code
29
+ end
30
+
31
+ def headers
32
+ raw.headers
33
+ end
34
+
35
+ # Provides response time in ms.
36
+ def time
37
+ (raw.time || 0) * 1000
38
+ end
39
+
40
+ def timeout?
41
+ raw.timed_out?
42
+ end
43
+
44
+ def success?
45
+ raw.success?
46
+ end
47
+
48
+ private
49
+
50
+ attr_accessor :raw
51
+
52
+ end
@@ -0,0 +1,3 @@
1
+ module LHC
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ #!/bin/bash -e
2
+
3
+ # custom vars
4
+ # RUBY = ruby-1.9.3-p125
5
+ # GEMSET = location-app-${GitHub pull request id|branch name}
6
+ # standard vars
7
+ # https://jenkins.intra.local.ch/env-vars.html/
8
+
9
+ if [ -z "$SKIP_RVM" ]; then
10
+ [[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
11
+ RUBY=${RUBY:-ruby-2.1.2}
12
+ GEMSET=${GEMSET:-LHC}
13
+ rvm use ${RUBY}@${GEMSET} --create
14
+ gem install bundler --no-rdoc --no-ri
15
+ fi
16
+
17
+ bundle install --local --quiet || bundle install --quiet
18
+ RAILS_ENV=test bundle exec rspec
19
+ rvm --force gemset delete ${RUBY}@${GEMSET}
@@ -0,0 +1,34 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'delete' do
6
+
7
+ let(:feedback) do
8
+ {
9
+ recommended: true,
10
+ source_id: 'aaa',
11
+ content_ad_id: '1z-5r1fkaj'
12
+ }
13
+ end
14
+
15
+ before(:each) do
16
+ stub_request(:delete, "http://datastore.lb-service/v2/feedbacks/12121")
17
+ .to_return(status: 200, body: feedback.to_json, headers: {'Content-Encoding' => 'UTF-8'})
18
+ end
19
+
20
+ it 'does a delete request when providing a complete url' do
21
+ LHC.delete('http://datastore.lb-service/v2/feedbacks/12121')
22
+ end
23
+
24
+ it 'it makes response data available in a rails way' do
25
+ response = LHC.delete('http://datastore.lb-service/v2/feedbacks/12121')
26
+ expect(response.data.recommended).to eq true
27
+ end
28
+
29
+ it 'provides response headers' do
30
+ response = LHC.delete('http://datastore.lb-service/v2/feedbacks/12121')
31
+ expect(response.headers).to be
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'get' do
6
+
7
+ before(:each) do
8
+ stub_request(:get, 'http://datastore.lb-service/v2/feedbacks?has_reviews=true')
9
+ .to_return(status: 200, body: { total: 99 }.to_json, headers: {'Content-Encoding' => 'UTF-8'})
10
+ end
11
+
12
+ let(:parameters) do
13
+ { has_reviews: true }
14
+ end
15
+
16
+ it 'does a get request when providing a complete url' do
17
+ LHC.get('http://datastore.lb-service/v2/feedbacks', params: parameters)
18
+ end
19
+
20
+ it 'does a get request when providing the name of a configured endpoint' do
21
+ url = 'http://:datastore/v2/feedbacks'
22
+ options = { params: { datastore: 'datastore.lb-service' } }
23
+ LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
24
+ LHC.get(:feedbacks, params: parameters)
25
+ end
26
+
27
+ it 'it makes response data available in a rails way' do
28
+ response = LHC.get('http://datastore.lb-service/v2/feedbacks', params: parameters)
29
+ expect(response.data.total).to eq 99
30
+ end
31
+
32
+ it 'provides response headers' do
33
+ response = LHC.get('http://datastore.lb-service/v2/feedbacks', params: parameters)
34
+ expect(response.headers).to be
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'post' do
6
+
7
+ let(:feedback) do
8
+ {
9
+ recommended: true,
10
+ source_id: 'aaa',
11
+ content_ad_id: '1z-5r1fkaj'
12
+ }
13
+ end
14
+
15
+ before(:each) do
16
+ stub_request(:post, "http://datastore.lb-service/v2/feedbacks")
17
+ .with(body: feedback.to_json)
18
+ .to_return(status: 200, body: feedback.to_json, headers: {'Content-Encoding' => 'UTF-8'})
19
+ end
20
+
21
+ it 'does a post request when providing a complete url' do
22
+ LHC.post('http://datastore.lb-service/v2/feedbacks', body: feedback.to_json)
23
+ end
24
+
25
+ it 'does a post request when providing the name of a configured endpoint' do
26
+ url = 'http://:datastore/v2/feedbacks'
27
+ options = { params: { datastore: 'datastore.lb-service' } }
28
+ LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
29
+ LHC.post(:feedbacks, body: feedback.to_json)
30
+ end
31
+
32
+ it 'it makes response data available in a rails way' do
33
+ response = LHC.post('http://datastore.lb-service/v2/feedbacks', body: feedback.to_json)
34
+ expect(response.data.source_id).to eq 'aaa'
35
+ end
36
+
37
+ it 'provides response headers' do
38
+ response = LHC.post('http://datastore.lb-service/v2/feedbacks', body: feedback.to_json)
39
+ expect(response.headers).to be
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'post' do
6
+
7
+ let(:feedback) do
8
+ {
9
+ recommended: false,
10
+ source_id: 'aaa',
11
+ content_ad_id: '1z-5r1fkaj'
12
+ }
13
+ end
14
+
15
+ let(:change) do
16
+ {
17
+ recommended: false
18
+ }
19
+ end
20
+
21
+ before(:each) do
22
+ stub_request(:put, "http://datastore.lb-service/v2/feedbacks")
23
+ .with(body: change.to_json)
24
+ .to_return(status: 200, body: feedback.merge(change).to_json, headers: {'Content-Encoding' => 'UTF-8'})
25
+ end
26
+
27
+ it 'does a post request when providing a complete url' do
28
+ LHC.put('http://datastore.lb-service/v2/feedbacks', body: change.to_json)
29
+ end
30
+
31
+ it 'does a post request when providing the name of a configured endpoint' do
32
+ url = 'http://:datastore/v2/feedbacks'
33
+ options = { params: { datastore: 'datastore.lb-service' } }
34
+ LHC.configure { |c| c.endpoint(:feedbacks, url, options) }
35
+ LHC.put(:feedbacks, body: change.to_json)
36
+ end
37
+
38
+ it 'it makes response data available in a rails way' do
39
+ response = LHC.put('http://datastore.lb-service/v2/feedbacks', body: change.to_json)
40
+ expect(response.data.recommended).to eq false
41
+ end
42
+
43
+ it 'provides response headers' do
44
+ response = LHC.put('http://datastore.lb-service/v2/feedbacks', body: change.to_json)
45
+ expect(response.headers).to be
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'request' do
6
+
7
+ let(:total) { 99 }
8
+
9
+ before(:each) do
10
+ stub_request(:get, "http://datastore.lb-service/v2/feedbacks?has_reviews=true")
11
+ .to_return(status: 200, body: { total: total }.to_json, headers: {'Content-Encoding' => 'UTF-8'})
12
+ end
13
+
14
+ it 'does a request returning a response' do
15
+ response = LHC.request(url: 'http://datastore.lb-service/v2/feedbacks', params: { has_reviews: true }, method: :get)
16
+ expect(response.data.total).to eq total
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'configured endpoints' do
6
+
7
+ let(:url) { 'http://analytics.lb-service/track/:entity_id/w/:type' }
8
+
9
+ let(:options) do
10
+ {
11
+ params: { env: 'PROD' },
12
+ followlocation: false
13
+ }
14
+ end
15
+
16
+ before(:each) do
17
+ LHC.configure do |c|
18
+ c.endpoint(:kpi_tracker, url, options)
19
+ end
20
+ end
21
+
22
+ it 'configures urls to be able to access them by name later' do
23
+ expect(LHC.config.endpoints[:kpi_tracker].url).to eq url
24
+ expect(LHC.config.endpoints[:kpi_tracker].options).to eq options
25
+ end
26
+
27
+ it 'compile url' do
28
+ stub_request(:get, 'http://analytics.lb-service/track/123/w/request?env=PROD')
29
+ response = LHC.get(:kpi_tracker, params: { entity_id: 123, type: 'request' })
30
+ expect(response.request.options[:followlocation]).to eq false
31
+ end
32
+
33
+ it 'gets overwritten by explicit request options' do
34
+ stub_request(:get, 'http://analytics.lb-service/track/123/w/request?env=STG')
35
+ response = LHC.get(:kpi_tracker, params: { entity_id: 123, type: 'request', env: 'STG' })
36
+ end
37
+
38
+ it 'raises in case of claching endpoint names' do
39
+ expect(->{
40
+ LHC.config.endpoint(:kpi_tracker, 'http://kpi-tracker.lb-service')
41
+ }).to raise_error 'Endpoint already exists for that name'
42
+ end
43
+
44
+ it 'enforces endpoint name to be a symbol' do
45
+ LHC.configure { |c| c.endpoint('datastore', 'http://datastore.lb-service') }
46
+ expect(LHC.config.endpoints[:datastore].url).to eq 'http://datastore.lb-service'
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+
5
+ context 'configuration of placeholders' do
6
+
7
+ it 'uses values for placeholders defined globally' do
8
+ LHC.configure { |c| c.placeholder(:datastore, 'http://datastore.lb-service/v2') }
9
+ stub_request(:get, "http://datastore.lb-service/v2/feedbacks")
10
+ LHC.get(':datastore/feedbacks')
11
+ end
12
+
13
+ it 'uses explicit values first' do
14
+ LHC.configure {|c| c.placeholder(:campaign_id, '123') }
15
+ stub_request(:get, 'http://datastore-stg.lb-service/v2/campaign/456/feedbacks')
16
+ url = 'http://datastore-stg.lb-service/v2/campaign/:campaign_id/feedbacks'
17
+ LHC.get(url, params: { campaign_id: '456' })
18
+ end
19
+
20
+ it 'raises in case of claching placeholder name' do
21
+ LHC.configure { |c| c.placeholder(:datastore, 'http://datastore.lb-service') }
22
+ expect(->{
23
+ LHC.config.placeholder(:datastore, 'http://datastore-stg.lb-service')
24
+ }).to raise_error 'Placeholder already exists for that name'
25
+ end
26
+
27
+ it 'enforces placeholder name to be a symbol' do
28
+ LHC.configure { |c| c.placeholder('datatore', 'http://datastore.lb-service') }
29
+ expect(LHC.config.placeholders[:datatore]).to eq 'http://datastore.lb-service'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes