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
-
._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
._log_exception(e, path, params) ⇒ Object
Log exceptions to console
93 94 95 96 97 98 99 100 |
# File 'lib/marfa/models/api_model.rb', line 93 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.message} (#{e.class})" p '==================================================' end |
.get_data(params) ⇒ Hash
Get data
21 22 23 |
# File 'lib/marfa/models/api_model.rb', line 21 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
30 31 32 |
# File 'lib/marfa/models/api_model.rb', line 30 def self.get_data_with_pagination(params) get_raw_data_with_pagination(params) end |
.get_raw_data(params) ⇒ Hash
“Raw” data getting
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/marfa/models/api_model.rb', line 39 def self.get_raw_data(params) params[:query] = params[:query].delete_if { |k, v| v.nil? } unless params[:query].nil? result = {} path = params[:path] cache_key = "data_#{path}#{params[:query]}".scan(/[a-zA-Zа-яА-Я0-9]+/).join('_') cache_key = params[:cache_key] unless params[:cache_key].nil? begin if Marfa.cache.exist?(cache_key) result = JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) else response = Rest.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} }) Marfa.cache.set(cache_key, response.body, params[:cache_time] || 7200) result = JSON.parse(response.body, symbolize_names: true) end rescue => exception if [:development, :test].include? Marfa.config.environment _log_exception(exception, path, params) end end result end |
.get_raw_data_with_pagination(params) ⇒ Hash
“Raw” data getting w/pagination headers x_count / x_pages
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/marfa/models/api_model.rb', line 66 def self.get_raw_data_with_pagination(params) params[:query] = params[:query].delete_if { |k, v| v.nil? } unless params[:query].nil? result = {} path = params[:path] cache_key = "data_with_pagination_#{path}#{params[:query]}".scan(/[a-zA-Zа-яА-Я0-9]+/).join('_') cache_key = params[:cache_key] unless params[:cache_key].nil? begin if Marfa.cache.exist?(cache_key) result = JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) else response = Rest.get("#{Marfa.config.api_server}#{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? Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || 7200) end rescue => exception if [:development, :test].include? Marfa.config.environment _log_exception(exception, path, params) end end result end |