Class: Ievkit::Client
- Inherits:
-
Object
- Object
- Ievkit::Client
- Defined in:
- lib/ievkit/client.rb
Instance Attribute Summary collapse
-
#iev_url_jobs ⇒ Object
readonly
Returns the value of attribute iev_url_jobs.
-
#iev_url_list_tests ⇒ Object
readonly
Returns the value of attribute iev_url_list_tests.
-
#iev_url_prefix ⇒ Object
readonly
Returns the value of attribute iev_url_prefix.
-
#iev_url_prefix_admin ⇒ Object
readonly
Returns the value of attribute iev_url_prefix_admin.
-
#iev_url_suffix ⇒ Object
readonly
Returns the value of attribute iev_url_suffix.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
-
#referential ⇒ Object
readonly
Returns the value of attribute referential.
Instance Method Summary collapse
- #get_stats ⇒ Object
- #init_connection(url) ⇒ Object
- #init_files(options) ⇒ Object
-
#initialize(referential) ⇒ Client
constructor
A new instance of Client.
- #list_tests(action, format) ⇒ Object
- #prepare_post_request(type, options) ⇒ Object
- #prepare_request(url, http_method, disable_cache = false) ⇒ Object
Constructor Details
#initialize(referential) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 12 13 |
# File 'lib/ievkit/client.rb', line 5 def initialize(referential) @payload = {} @referential = referential @iev_url_prefix = init_iev_url_prefix @iev_url_prefix_admin = init_iev_url_prefix_admin @iev_url_list_tests = init_iev_url_list_tests @iev_url_jobs = init_iev_url_jobs @redis = Redis.new end |
Instance Attribute Details
#iev_url_jobs ⇒ Object (readonly)
Returns the value of attribute iev_url_jobs.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def iev_url_jobs @iev_url_jobs end |
#iev_url_list_tests ⇒ Object (readonly)
Returns the value of attribute iev_url_list_tests.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def iev_url_list_tests @iev_url_list_tests end |
#iev_url_prefix ⇒ Object (readonly)
Returns the value of attribute iev_url_prefix.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def iev_url_prefix @iev_url_prefix end |
#iev_url_prefix_admin ⇒ Object (readonly)
Returns the value of attribute iev_url_prefix_admin.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def iev_url_prefix_admin @iev_url_prefix_admin end |
#iev_url_suffix ⇒ Object (readonly)
Returns the value of attribute iev_url_suffix.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def iev_url_suffix @iev_url_suffix end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def redis @redis end |
#referential ⇒ Object (readonly)
Returns the value of attribute referential.
3 4 5 |
# File 'lib/ievkit/client.rb', line 3 def referential @referential end |
Instance Method Details
#get_stats ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ievkit/client.rb', line 82 def get_stats @payload = { key: ENV['iev_admin_key'] } init_connection(@iev_url_prefix_admin) begin response = @connection.get('get_monthly_stats', @payload) parse_response(response) rescue => e Ievkit::Log.logger.fatal("Unable to contact IEV server: #{e.}") return false end end |
#init_connection(url) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/ievkit/client.rb', line 62 def init_connection(url) @connection = Faraday.new(url: url) do |conn| conn.request :multipart if @payload.any? conn.headers = headers conn.response :json, content_type: 'application/json' conn.adapter Faraday.default_adapter end end |
#init_files(options) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ievkit/client.rb', line 71 def init_files() return unless if [:iev_params].present? && File.file?([:iev_params]) @payload[:file[0]] = Faraday::UploadIO.new([:iev_params], 'application/json', 'parameters.json') end iev_file = [:iev_file] return if iev_file.blank? || !File.file?(iev_file) filename = File.basename(iev_file) @payload[:file[1]] = Faraday::UploadIO.new(iev_file, 'application/zip', filename) end |
#list_tests(action, format) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ievkit/client.rb', line 94 def list_tests(action, format) init_connection(@iev_url_list_tests) begin response = @connection.get("#{action}/#{format}", @payload) parse_response(response) rescue => e Ievkit::Log.logger.fatal("Unable to contact IEV server: #{e.}") return false end end |
#prepare_post_request(type, options) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ievkit/client.rb', line 15 def prepare_post_request(type, ) init_files() @iev_url_suffix = init_iev_url_suffix(type) init_connection(iev_url_prefix) begin response = @connection.post(@iev_url_suffix, @payload) parse_response(response) rescue => e Ievkit::Log.logger.fatal("Unable to contact IEV server: #{e.}") return false end end |
#prepare_request(url, http_method, disable_cache = false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ievkit/client.rb', line 28 def prepare_request(url, http_method, disable_cache = false) unless disable_cache cache_key = [url, http_method.to_s].join('_') begin response_cached = @redis.cache(cache_key) return response_cached if response_cached rescue => e Ievkit::Log.logger.fatal("Unable to contact Redis server: #{e.}") end end init_connection(url) begin response = @connection.send(http_method) unless disable_cache cache_control = response.headers['cache-control'] max_age = 0 no_cache = true if cache_control max_age = cache_control[/max-age=(.*)/, 1].to_i no_cache = false if max_age > 0 && cache_control[/no-transform/] end return parse_response(response) if no_cache @redis.cache(cache_key, max_age) { parse_response(response) } else return parse_response(response) end rescue => e Ievkit::Log.logger.fatal("Unable to contact IEV server: #{e.}") return false end end |