Class: Marfa::Models::APIModel
- Includes:
- Helpers::HTTP
- Defined in:
- lib/marfa/models/api_model.rb
Overview
Base model
Defined Under Namespace
Classes: ModelError
Class Method Summary collapse
-
._get_request_result(params) ⇒ Hash
request result without headers.
-
._get_request_result_with_headers(params) ⇒ Hash
request result with headers.
-
._log_exception(e, path, params) ⇒ Object
Log exceptions to console.
-
.get_data(params) ⇒ Hash
Get data.
-
.get_data_with_pagination(params) ⇒ Hash
Get data w/pagination headers x_count / x_pages.
-
.get_raw_data(params) ⇒ Hash
“Raw” data getting.
-
.get_raw_data_with_pagination(params) ⇒ Hash
“Raw” data getting w/pagination headers x_count / x_pages.
Class Method Details
._get_request_result(params) ⇒ Hash
request result without headers
108 109 110 111 |
# File 'lib/marfa/models/api_model.rb', line 108 def self._get_request_result(params) response = Rest.get("#{Marfa.config.api_server}#{params[:path]}", { params: params[:query], headers: {} }) JSON.parse(response.body, symbolize_names: true) end |
._get_request_result_with_headers(params) ⇒ Hash
request result with headers
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/marfa/models/api_model.rb', line 115 def self._get_request_result_with_headers(params) result = {} thread = Thread.new do response = Rest.get("#{Marfa.config.api_server}#{params[:path]}", { params: params[:query], headers: {} }) result[:data] = JSON.parse(response.body, symbolize_names: true) result[:data_count] = response.headers[:x_count].to_i unless response.headers[:x_count].nil? result[:data_pages] = response.headers[:x_pages].to_i unless response.headers[:x_pages].nil? end thread.join result end |
._log_exception(e, path, params) ⇒ Object
Log exceptions to console
97 98 99 100 101 102 103 104 |
# File 'lib/marfa/models/api_model.rb', line 97 def self._log_exception(e, path, params) p '========== Exception while making request: ==========' p "Path: #{Marfa.config.api_server}#{path}" p 'Params:' p params p "#{e.} (#{e.class})" p '==================================================' end |
.get_data(params) ⇒ Hash
Get data
22 23 24 |
# File 'lib/marfa/models/api_model.rb', line 22 def self.get_data(params) get_raw_data(params) end |
.get_data_with_pagination(params) ⇒ Hash
Get data w/pagination headers x_count / x_pages
31 32 33 |
# File 'lib/marfa/models/api_model.rb', line 31 def self.get_data_with_pagination(params) get_raw_data_with_pagination(params) end |
.get_raw_data(params) ⇒ Hash
“Raw” data getting
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/marfa/models/api_model.rb', line 40 def self.get_raw_data(params) params[:query] = params[:query].delete_if { |_, v| v.nil? } unless params[:query].nil? path = params[:path] cache_key = params[:cache_key] begin # don't need cache return _get_request_result(params) if params[:cache_data].blank? || params[:cache_key].blank? # cached data exists return JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) if Marfa.cache.exist?(cache_key) # get result and set to cache result = _get_request_result(params) Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || Marfa.config.cache[:expiration_time]) return result rescue => exception if [:development, :test].include? Marfa.config.environment _log_exception(exception, path, params) end end {} end |
.get_raw_data_with_pagination(params) ⇒ Hash
“Raw” data getting w/pagination headers x_count / x_pages
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/marfa/models/api_model.rb', line 70 def self.get_raw_data_with_pagination(params) params[:query] = params[:query].delete_if { |_, v| v.nil? } unless params[:query].nil? path = params[:path] cache_key = params[:cache_key] begin # don't need cache return _get_request_result_with_headers(params) if params[:cache_data].blank? || params[:cache_key].blank? # cached data exists return JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) if Marfa.cache.exist?(cache_key) result = _get_request_result_with_headers(params) Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || Marfa.config.cache[:expiration_time]) return result rescue => exception if [:development, :test].include? Marfa.config.environment _log_exception(exception, path, params) end end {} end |