Class: Marfa::Models::APIModel
- Inherits:
-
Object
- Object
- Marfa::Models::APIModel
- 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
75 76 77 78 79 80 81 82 |
# File 'lib/marfa/models/api_model.rb', line 75 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
19 20 21 |
# File 'lib/marfa/models/api_model.rb', line 19 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
28 29 30 |
# File 'lib/marfa/models/api_model.rb', line 28 def self.get_data_with_pagination(params) get_raw_data_with_pagination(params) end |
.get_raw_data(params) ⇒ Hash
“Raw” data getting
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/marfa/models/api_model.rb', line 37 def self.get_raw_data(params) result = {} path = params[:path] begin response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} }) result = JSON.parse(response.body, symbolize_names: true) 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/marfa/models/api_model.rb', line 56 def self.get_raw_data_with_pagination(params) result = {} path = params[:path] begin response = RestClient.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? rescue => exception if [:development, :test].include? Marfa.config.environment _log_exception(exception, path, params) end end result end |