Class: Judopay::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/judopay/model.rb

Overview

Base model for Judopay API model objects

Constant Summary collapse

VALID_PAGING_OPTIONS =
[:sort, :offset, :page_size].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.resource_pathObject

Returns the value of attribute resource_path.



17
18
19
# File 'lib/judopay/model.rb', line 17

def resource_path
  @resource_path
end

.valid_api_methodsObject

Returns the value of attribute valid_api_methods.



17
18
19
# File 'lib/judopay/model.rb', line 17

def valid_api_methods
  @valid_api_methods
end

Class Method Details

.all(options = {}) ⇒ Judopay::Mash

List all records

Parameters:

  • options (Hash) (defaults to: {})

    Paging options (sort, offset and page_size)

Returns:



23
24
25
26
27
28
29
# File 'lib/judopay/model.rb', line 23

def all(options = {})
  check_api_method_is_supported(__method__)
  api = Judopay::API.new
  valid_options = self.valid_options(options).camel_case_keys!
  uri = resource_path + '?' + valid_options.to_query_string
  api.get(uri)
end

.check_api_method_is_supported(method) ⇒ Object

Check if the specified API method is supported by the current model

Raises:

  • (Judopay::Error)

    if the API method is not supported



44
45
46
# File 'lib/judopay/model.rb', line 44

def check_api_method_is_supported(method)
  raise Judopay::ValidationError, 'API method not supported' if valid_api_methods.nil? || !valid_api_methods.include?(method.to_sym)
end

.find(receipt_id) ⇒ Judopay::Mash

Retrieve a specific record

Parameters:

  • receipt_id (Integer)

    ID of particular transaction

Returns:



35
36
37
38
39
# File 'lib/judopay/model.rb', line 35

def find(receipt_id)
  check_api_method_is_supported(__method__)
  api = Judopay::API.new
  api.get(resource_path + receipt_id.to_i.to_s)
end

.valid_options(options) ⇒ Object

Take an paging options hash and filter all but the valid keys



49
50
51
52
53
54
55
56
# File 'lib/judopay/model.rb', line 49

def valid_options(options)
  valid_options = {}
  options.each do |key, value|
    next unless VALID_PAGING_OPTIONS.include?(key)
    valid_options[key] = value
  end
  valid_options
end

Instance Method Details

#check_api_method_is_supported(method) ⇒ Object

Verify if an API method is supported for the current model



97
98
99
# File 'lib/judopay/model.rb', line 97

def check_api_method_is_supported(method)
  self.class.check_api_method_is_supported(method)
end

#check_judo_idObject

Use judo_id from configuration if it hasn’t been explicitly set



102
103
104
105
# File 'lib/judopay/model.rb', line 102

def check_judo_id
  return unless respond_to?('judo_id') && judo_id.nil?
  self.judo_id = Judopay.configuration.judo_id
end

#createJudopay::Mash

Create a new record

Returns:



62
63
64
65
66
67
68
# File 'lib/judopay/model.rb', line 62

def create
  check_api_method_is_supported(__method__)
  check_judo_id
  check_validation
  api = Judopay::API.new
  api.post(resource_path, self)
end

#resource_pathString

Retrieve the current API resource path (e.g. /transactions/payments)

Returns:



84
85
86
# File 'lib/judopay/model.rb', line 84

def resource_path
  self.class.resource_path
end

#valid_api_methodsArray<Symbol>

Retrieve an array of valid API methods for the current model e.g [:find, :create]

Returns:

  • (Array<Symbol>)

    Valid API methods



92
93
94
# File 'lib/judopay/model.rb', line 92

def valid_api_methods
  self.class.valid_api_methods
end

#validateJudopay::Mash

Validates a request

Returns:



73
74
75
76
77
78
79
# File 'lib/judopay/model.rb', line 73

def validate
  check_api_method_is_supported(__method__)
  check_judo_id
  check_validation
  api = Judopay::API.new
  api.post(resource_path + '/validate', self)
end