Class: PayPro::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_pro/util.rb

Class Method Summary collapse

Class Method Details

.entity_class(string) ⇒ Object

Searches the entities folder for a valid entity class based on a string. This string will be returned in the API as the ‘type’ parameter.

If the class cannot be found it will fallback to the base class Entity.



10
11
12
13
14
15
16
# File 'lib/pay_pro/util.rb', line 10

def entity_class(string)
  parts = string.split('_')
  class_name = parts.map { |part| part.downcase.capitalize }.join
  PayPro.const_get(class_name, false)
rescue NameError
  Entity
end

.normalize_api_attributes(attributes) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/pay_pro/util.rb', line 18

def normalize_api_attributes(attributes)
  new_attributes = attributes.dup

  new_attributes.delete('type')
  new_attributes['links'] = new_attributes.delete('_links') if new_attributes['_links']
  new_attributes
end

.to_entity(data, api_client:, params: {}) ⇒ Object

Creates an Enity class or returns the data if the data cannot be converted to an entity.

It will try to find an API entity class, if it cannot be found it will fallback to the default Entity class.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pay_pro/util.rb', line 30

def to_entity(data, api_client:, params: {})
  case data
  when Array
    data.map { |i| to_entity(i, api_client: api_client) }
  when Hash
    if data.key?('type')
      entity = entity_class(data['type']).create_from_data(data, api_client: api_client)
      entity.filters = params if entity.is_a?(PayPro::List)
      entity
    else
      data.transform_values { |value| to_entity(value, api_client: api_client) }
    end
  else
    data
  end
end