Class: Swiftner::API::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/swiftner/API/service.rb

Overview

The ‘Service` class acts as a base class for API operations and data encapsulation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, client = self.class.client) ⇒ Service

Returns a new instance of Service.

Raises:



30
31
32
33
34
35
36
# File 'lib/swiftner/API/service.rb', line 30

def initialize(attributes = {}, client = self.class.client)
  raise Swiftner::Error, "Client must be set" if client.nil?

  @id = attributes["id"]
  @details = attributes
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/swiftner/API/service.rb', line 7

def client
  @client
end

#detailsObject (readonly)

Returns the value of attribute details.



7
8
9
# File 'lib/swiftner/API/service.rb', line 7

def details
  @details
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/swiftner/API/service.rb', line 7

def id
  @id
end

Class Method Details

.build(details) ⇒ Object



9
10
11
# File 'lib/swiftner/API/service.rb', line 9

def self.build(details)
  new(details)
end

.clientObject



13
14
15
# File 'lib/swiftner/API/service.rb', line 13

def self.client
  Swiftner.configuration.client
end

.map_collection(response) ⇒ Object



26
27
28
# File 'lib/swiftner/API/service.rb', line 26

def self.map_collection(response)
  response.map { |item| build(item) }
end

.validate_required(attributes, *keys) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/swiftner/API/service.rb', line 17

def self.validate_required(attributes, *keys)
  attr_str_keys = attributes.transform_keys(&:to_s)
  missing_keys = keys.map(&:to_s).reject { |key| attr_str_keys.key?(key) }

  return unless missing_keys.any?

  raise ArgumentError, "Key(s) '#{missing_keys.join(", ")}' are missing in attributes. #{attributes.inspect}"
end