Class: Natero::Base

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/natero/base.rb

Direct Known Subclasses

Account, Event, Metric, User

Constant Summary collapse

BASE_URI =
'https://api.natero.com'
VERSION_URI =
'/api/v2'
REQUIRED_PARAMS =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#serialize, #to_h, #unserialize

Constructor Details

#initialize(params, raw_response = nil) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
# File 'lib/natero/base.rb', line 41

def initialize(params, raw_response = nil)
  missing_params = REQUIRED_PARAMS - params.keys
  raise ArgumentError.new("Missing required params #{missing_params.join(', ')}") unless missing_params.empty?

  load_model_properties
  clean_params(params)
  populate_properties(params)

  @raw_response = raw_response
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



12
13
14
# File 'lib/natero/base.rb', line 12

def raw_response
  @raw_response
end

Class Method Details

.endpoint(*params) ⇒ Object



18
19
20
21
# File 'lib/natero/base.rb', line 18

def self.endpoint(*params)
  params = [endpoint_path, params, Natero.api_key_uri].flatten.compact.map(&:to_s)
  Natero.full_endpoint_uri(BASE_URI, VERSION_URI, params)
end

.endpoint_pathObject

Raises:

  • (NotImplementedError)


23
24
25
26
27
# File 'lib/natero/base.rb', line 23

def self.endpoint_path
  raise NotImplementedError.new( 'This method needs to be overridden in a child class.  Proper implementation '\
      'should return an array where each index contains a different part of the path.  For example: '\
      '[\'test\', \'best\'] becomes \'/test/best/\'.' )
end

.json_data(body) ⇒ Object



29
30
31
# File 'lib/natero/base.rb', line 29

def self.json_data(body)
  { body: body, headers: json_headers }
end

.json_headersObject



33
34
35
# File 'lib/natero/base.rb', line 33

def self.json_headers
  { 'Content-Type': 'application/json' }
end

.request_helperObject



37
38
39
# File 'lib/natero/base.rb', line 37

def self.request_helper
  Natero::RequestHelper.new(self)
end

Instance Method Details

#to_jsonObject



52
53
54
# File 'lib/natero/base.rb', line 52

def to_json
  serialize
end