Module: Malartu

Defined in:
lib/malartu.rb,
lib/malartu/error.rb,
lib/malartu/metric.rb,
lib/malartu/version.rb,
lib/malartu/schedule.rb,
lib/malartu/snapshot.rb,
lib/malartu/tracking.rb,
lib/malartu/dashboard.rb,
lib/malartu/portfolio.rb,
lib/malartu/connection.rb,
lib/malartu/tracking/data.rb,
lib/malartu/malartu_object.rb

Defined Under Namespace

Modules: Error, Tracking Classes: Connection, Dashboard, MalartuObject, Metric, Portfolio, Schedule, Snapshot

Constant Summary collapse

API_VERSION =
'v0'.freeze
API_PATH =
'https://api.malartu.co'.freeze
VERSION =
'0.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_versionObject

Returns the value of attribute api_version.



19
20
21
# File 'lib/malartu.rb', line 19

def api_version
  @api_version
end

.apikeyObject

Returns the value of attribute apikey.



19
20
21
# File 'lib/malartu.rb', line 19

def apikey
  @apikey
end

.topicsObject

Returns the value of attribute topics.



19
20
21
# File 'lib/malartu.rb', line 19

def topics
  @topics
end

Class Method Details

.auth_paramsObject



34
35
36
37
# File 'lib/malartu.rb', line 34

def auth_params
  fail 'No apikey present. Set it with `Malartu.apikey =`' if apikey.nil?
  { apikey: apikey }
end

.base_pathObject



39
40
41
# File 'lib/malartu.rb', line 39

def base_path
  "#{API_PATH}/#{version}"
end

.check_for_errors(response, params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/malartu.rb', line 47

def check_for_errors(response, params)
  case response.code
  when 401
    fail Malartu::Error::AuthorizationError.new(
      message: 'Credentials do not match',
      parameters: {
        apikey: params[:apikey]
      },
      json_body: JSON.parse(response.body)
    )
  when 404
    fail Malartu::Error::RecordNotFoundError.new(
      message: 'Record Not Found',
      parameters: {
        id: params[:id],
        model: params[:model]
      },
      json_body: JSON.parse(response.body)
    )
  when 429
    fail Malartu::Error::RateLimitError.new(
      message: 'Rate Limited',
      parameters: {
        apikey: params[:apikey]
      },
      json_body: JSON.parse(response.body)
    )
  when 500
    fail Malartu::Error::ServerError.new(
      message: 'Server Error',
      parameters: params,
      json_body: JSON.parse(response.body)
    )
  end
end

.request(method, path, params = {}, _headers = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/malartu.rb', line 21

def request(method, path, params = {}, _headers = {})
  url = "#{base_path}#{path}"
  req_params = case method.to_s
               when 'get'
                 { params: params.merge(auth_params) }
               else
                 { json: params.merge(auth_params) }
               end
  response = HTTP.send(method, url, req_params)
  check_for_errors(response, params.merge(auth_params))
  JSON.parse(response.body)
end

.versionObject



43
44
45
# File 'lib/malartu.rb', line 43

def version
  api_version || API_VERSION
end