Class: Marfa::Models::APIModel

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Helpers::HTTP
Defined in:
lib/marfa/models/api_model.rb

Overview

Base model

Defined Under Namespace

Classes: ModelError

Class Method Summary collapse

Class Method Details

._get_request_result(params) ⇒ Hash

request result without headers

Returns:



119
120
121
122
# File 'lib/marfa/models/api_model.rb', line 119

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

Returns:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/marfa/models/api_model.rb', line 126

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



108
109
110
111
112
113
114
115
# File 'lib/marfa/models/api_model.rb', line 108

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



24
25
26
# File 'lib/marfa/models/api_model.rb', line 24

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



33
34
35
# File 'lib/marfa/models/api_model.rb', line 33

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:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/marfa/models/api_model.rb', line 42

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

Parameters:

  • params (Hash)
    • options hash

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/marfa/models/api_model.rb', line 72

def self.get_raw_data_with_pagination(params)
  params[:query] = params[:query].delete_if { |_, v| v.nil? } unless params[:query].nil?
  params[:query][:page] = 1 if params[:query][:page].blank?

  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
  # Avoid range errors
  rescue RestClient::RangeNotSatisfiable
    params[:query][:page] = 1

    # try to get x_pages
    res = _get_request_result_with_headers(params)

    raise PagesRangeError.new('Page is out of range', res[:data_pages])
  rescue => exception
    if [:development, :test].include? Marfa.config.environment
      _log_exception(exception, path, params)
    end
  end

  {}
end