Module: WavixApi::BaseMethods

Included in:
V1::BaseDestroy, V1::BaseFind, V1::BaseSearch, V1::Billing::Invoices::Find, V1::Billing::Transactions::Search, V1::Buy::Carts::Checkout, V1::Buy::Carts::Find, V1::Buy::Carts::Update, V1::Buy::Cities::Search, V1::Buy::Countries::Search, V1::Buy::Dids::Search, V1::Buy::Regions::Search, V1::Call::Recordings::MonthStatistics::Details, V1::Call::Recordings::MonthStatistics::Search, V1::Call::Recordings::RecordingSettings, V1::Call::Recordings::RetentionPolicy, V1::Call::Recordings::Search, V1::Call::Recordings::UpdateRecordingSettings, V1::Call::Recordings::UpdateRetentionPolicy, V1::Call::Transcriptions::Find, V1::Call::Transcriptions::Retranscribe, V1::Cdrs::AdvancedSearch, V1::Cdrs::Metrics, V1::Cdrs::Search, V1::E911Records::Create, V1::E911Records::Destroy, V1::E911Records::Search, V1::E911Records::ValidateAddress, V1::Mydids::Cities, V1::Mydids::Destroy, V1::Mydids::Search, V1::Mydids::Update, V1::Mydids::UpdateDestinations, V1::Mydids::UpdateSmsEnabled, V1::Papers::Create, V1::Papers::Find, V1::Profile::AccountConfig, V1::Profile::Find, V1::Profile::Update, V1::SubOrganizations::Create, V1::SubOrganizations::Search, V1::SubOrganizations::Transactions, V1::SubOrganizations::Update, V1::Trunks::Create, V1::Trunks::Search, V1::Trunks::Update, V1::VoiceCampaigns::Create, V2::Messages::AsyncCreate, V2::Messages::Attachments::Find, V2::Messages::Create, V2::Messages::OptOuts::Create, V2::Messages::SenderIds::Create, V2::Messages::SenderIds::Restrictions, V3::Messages::SenderIds::Create
Defined in:
lib/wavix_api/base_methods.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json' }.freeze
PAGINATION_SCHEMA =
{
  page: { type: 'integer' },
  per_page: { type: 'integer' }
}.freeze
ONLY_ID_SCHEMA =
{
  type: 'object',
  properties: {
    id: { type: 'integer' }.freeze
  }.freeze,
  required: %i[id].freeze
}.freeze
ONLY_UUID_SCHEMA =
{
  type: 'object',
  properties: {
    id: { type: 'string' }.freeze
  }.freeze,
  required: %i[id].freeze
}.freeze
AVAILABLE_TRANSCRIPTION_LANGUAGES =
%w[en de es fr it].freeze
BASE_DATE_FORMAT =
'%Y-%m-%d'
ONLY_DIGITS_REGEXP =
/^\d+$/.freeze
IP_REGEXP =
/^\d(\.\d)*$/.freeze
LIST_OF_DIGITS_REGEXP =
/^\d+(,\d+)*$/.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.params_schemaObject

Returns the value of attribute params_schema.



37
38
39
# File 'lib/wavix_api/base_methods.rb', line 37

def params_schema
  @params_schema
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/wavix_api/base_methods.rb', line 9

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/wavix_api/base_methods.rb', line 9

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/wavix_api/base_methods.rb', line 9

def params
  @params
end

Instance Method Details

#callObject



134
135
136
# File 'lib/wavix_api/base_methods.rb', line 134

def call
  self.class.call(params)
end

#delete(path) ⇒ Object



118
119
120
# File 'lib/wavix_api/base_methods.rb', line 118

def delete(path)
  client.delete(path, params: params.merge(additional_params))
end

#download(path, headers: {}, save_path: nil) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/wavix_api/base_methods.rb', line 91

def download(path, headers: {}, save_path: nil)
  client.download(
    path,
    params: params.merge(additional_params),
    headers: DEFAULT_HEADERS.merge(headers),
    save_path: save_path
  )
end

#file_extention(file) ⇒ Object



130
131
132
# File 'lib/wavix_api/base_methods.rb', line 130

def file_extention(file)
  File.extname(file.path).to_s[1..-1]
end

#format_file(file, content_type: 'multipart/form-data') ⇒ Object



126
127
128
# File 'lib/wavix_api/base_methods.rb', line 126

def format_file(file, content_type: 'multipart/form-data')
  Faraday::Multipart::FilePart.new(file.path, content_type)
end

#get(path, headers: {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/wavix_api/base_methods.rb', line 83

def get(path, headers: {})
  client.get(
    path,
    params: params.merge(additional_params),
    headers: DEFAULT_HEADERS.merge(headers)
  )
end

#initialize(params = {}) ⇒ Object



40
41
42
43
44
# File 'lib/wavix_api/base_methods.rb', line 40

def initialize(params = {})
  @params = deep_symbolize_keys(params)
  @id = @params[:id]
  @client = ::WavixApi.client
end

#patch(path) ⇒ Object



114
115
116
# File 'lib/wavix_api/base_methods.rb', line 114

def patch(path)
  client.patch(path, body: params.except(:id), params: additional_params)
end

#post(path, with_file: false, headers: {}) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/wavix_api/base_methods.rb', line 100

def post(path, with_file: false, headers: {})
  client.post(
    path,
    params: additional_params,
    headers: DEFAULT_HEADERS.merge(headers),
    body: params.except(:id),
    with_file: with_file
  )
end

#put(path) ⇒ Object



110
111
112
# File 'lib/wavix_api/base_methods.rb', line 110

def put(path)
  client.put(path, body: params.except(:id), params: additional_params)
end

#raise_error(error) ⇒ Object

Raises:



122
123
124
# File 'lib/wavix_api/base_methods.rb', line 122

def raise_error(error)
  raise ValidationError, error
end

#stringify_dates!(params, fields:, format: BASE_DATE_FORMAT) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wavix_api/base_methods.rb', line 67

def stringify_dates!(params, fields:, format: BASE_DATE_FORMAT)
  fields.each do |field|
    nested = field.to_s.split('.')
    # INFO: in case of nested key. For example: item.date_from, item.nested_item.date_from
    if nested.count > 1
      key = nested.first.to_sym
      stringify_dates!(params[key], fields: nested[1..-1].join('.'), format: format)
    else
      value = params[field.to_sym]
      next if !value.is_a?(::DateTime) && (value.nil? || value.empty?)

      params[field.to_sym] = value.strftime(format)
    end
  end
end

#validate!Object



46
47
48
49
# File 'lib/wavix_api/base_methods.rb', line 46

def validate!
  validate_params! unless self.class.instance_variable_get('@params_schema').nil?
  validate_appid!
end

#validate_dates!(fields, step_params: params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wavix_api/base_methods.rb', line 51

def validate_dates!(fields, step_params: params)
  invalid_fields = fields.select do |field|
    value = step_params[field.to_sym]
    nested = field.to_s.split('.')
    # INFO: in case of nested key. For example: item.date_from, item.nested_item.date_from
    if nested.count > 1
      validate_dates!(nested[1..-1], step_params: step_params[nested.first.to_i])
    else
      !value.is_a?(::DateTime) && (!value.nil? && !value.empty?)
    end
  end
  return true if invalid_fields.empty?

  raise_error("Fields #{invalid_fields} must be a DateTime")
end