Class: Marfa::Models::APIModel

Inherits:
Object
  • Object
show all
Defined in:
lib/marfa/models/api_model.rb

Overview

Base model

Defined Under Namespace

Classes: ModelError

Class Method Summary collapse

Class Method Details

._log_exception(e, path, params) ⇒ Object

Log exceptions to console



91
92
93
94
95
96
97
98
# File 'lib/marfa/models/api_model.rb', line 91

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

Examples:

BaseModel.get_data({ path: 'category/list' })

Parameters:

  • params (Hash)

    options hash

Returns:

  • (Hash)

    data from API



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

Examples:

BaseModel.get_data({ path: 'category/list' })

Parameters:

  • params (Hash)

    options hash

Returns:

  • (Hash)

    data from API



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

Examples:

self.get_raw_data({ path: 'category/list' })

Parameters:

  • params (Hash)
    • options hash

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/marfa/models/api_model.rb', line 37

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 = RestClient.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

Parameters:

  • params (Hash)
    • options hash

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/marfa/models/api_model.rb', line 64

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 = 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?
      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