class Telefacil::Base

Constants

API_BASE

 API BASE URLS

API_CANARIAS

Public Class Methods

api_canarias() click to toggle source
# File lib/telefacil/base.rb, line 15
def self.api_canarias
  @@api_canarias ||= nil
end
api_canarias=( api_canarias ) click to toggle source
# File lib/telefacil/base.rb, line 19
def self.api_canarias=( api_canarias )
  @@api_canarias = api_canarias
end
autenticado?() click to toggle source
# File lib/telefacil/base.rb, line 39
def self.autenticado?
  (principal.nil? || password.nil?) ? false : true
end
configurar() { |self| ... } click to toggle source
# File lib/telefacil/base.rb, line 11
def self.configurar
  yield(self)
end
password() click to toggle source
# File lib/telefacil/base.rb, line 27
def self.password
  @@password ||= nil
end
password=( password ) click to toggle source
# File lib/telefacil/base.rb, line 35
def self.password=( password )
  @@password = password
end
principal() click to toggle source
# File lib/telefacil/base.rb, line 23
def self.principal
  @@principal ||= nil
end
principal=( principal ) click to toggle source
# File lib/telefacil/base.rb, line 31
def self.principal=( principal )
  @@principal = principal
end

Private Class Methods

api_endpoint(api_endpoint = nil) click to toggle source
# File lib/telefacil/base.rb, line 56
def self.api_endpoint(api_endpoint = nil)
  @_api_endpoint ||= api_endpoint
end
api_model(api_model = nil) click to toggle source
# File lib/telefacil/base.rb, line 60
def self.api_model(api_model = nil)
  @_api_model ||= api_model
end
api_singular(api_singular = nil) click to toggle source
# File lib/telefacil/base.rb, line 64
def self.api_singular(api_singular = nil)
  @_api_singular ||= api_singular
end
atributo_modelo() click to toggle source
# File lib/telefacil/base.rb, line 110
def self.atributo_modelo
  self.api_singular.nil? ? "#{self.api_model}" : "#{self.api_singular}"
end
construir_opts_query(params, accion) click to toggle source
# File lib/telefacil/base.rb, line 68
def self.construir_opts_query(params, accion)
  opts = {
    :query => {
      :principal => principal,
      :pass => password,
      :tipo_salida => 'xml'
    }
  }
  opts[:query][:accion] = accion if accion
  opts[:query].merge!(params)    if !params.empty?
  opts
end
construir_url() click to toggle source
# File lib/telefacil/base.rb, line 81
def self.construir_url
  (api_canarias.nil? ? API_BASE : API_CANARIAS) + self.api_endpoint
end
convertir_a_array(respuesta) click to toggle source
# File lib/telefacil/base.rb, line 118
def self.convertir_a_array(respuesta)
  if !respuesta.is_a?(Array)
    respuesta = [respuesta].compact
  end
  respuesta
end
http_response(url, opts) click to toggle source
# File lib/telefacil/base.rb, line 85
def self.http_response(url, opts)
  respuesta = HTTParty.get(url, opts)
  xml      = respuesta.parsed_response
  parsear_a_os(xml)
end
parse(datos) click to toggle source
# File lib/telefacil/base.rb, line 95
def self.parse(datos)
  d = datos.send("#{plural(self.api_model)}")

  hay_resultado = (d.res) && (d.res == "1")
  no_hay_error  = (d.error) && (d.error == "0")

  if (hay_resultado)||(no_hay_error)
    respuesta = d.send(atributo_modelo)
    respuesta = convertir_a_array(respuesta)
    Exito.new(respuesta)
  else
    Error.new(d.msg)
  end
end
parsear_a_os(xml) click to toggle source
# File lib/telefacil/base.rb, line 91
def self.parsear_a_os(xml)
  JSON.parse(xml.to_json, object_class: OpenStruct)
end
plural(str) click to toggle source
# File lib/telefacil/base.rb, line 114
def self.plural(str)
  self.api_model + "s"
end
request(params={}, accion=nil) click to toggle source
# File lib/telefacil/base.rb, line 45
def self.request(params={}, accion=nil)
  raise Telefacil::AutenticacionError unless autenticado?
  opts  = construir_opts_query(params, accion)
  url   = construir_url
  respuesta = http_response(url, opts)
  respuesta_parseada = parse(respuesta)
rescue StandardError => e
  puts 'Telefacil Error: '+ e.message
  false
end