class BsellerRuby::Base

Attributes

response[R]

Public Class Methods

call(method, params) click to toggle source
# File lib/bseller_ruby/base.rb, line 35
def call(method, params)
  @method = method
  params.merge!(authentication) if params.class.eql?(Hash)
  Response.new method, api.call(method, message: params)
end
collection_name() click to toggle source
# File lib/bseller_ruby/base.rb, line 31
def collection_name
  @collection_name = "#{resource_name}s"
end
create(method, params) click to toggle source
# File lib/bseller_ruby/base.rb, line 41
def create(method, params)
  new response: call(method, params)
end
get(resource, params = {}) click to toggle source
# File lib/bseller_ruby/base.rb, line 45
def get(resource, params = {})
  json_parse(execute(:get, "#{resource}?#{to_params(params)}"))
end
new(params) click to toggle source
# File lib/bseller_ruby/base.rb, line 13
def initialize(params)
  params.each do |key, value|
    instance_variable_set "@#{key}", value
    define_singleton_method(key) { instance_variable_get "@#{key}" }
  end
end
post(path, body, &block) click to toggle source
# File lib/bseller_ruby/base.rb, line 49
def post(path, body, &block)
  json_parse(execute(:post, path, body: body, &block))
end
put(resource, path, payload) click to toggle source
# File lib/bseller_ruby/base.rb, line 53
def put(resource, path, payload)
  json_parse(execute(:put, "#{resource}/#{path}?a=1", body: payload))
end

Private Class Methods

api() click to toggle source
# File lib/bseller_ruby/base.rb, line 82
def self.api
  @api = Savon.client wsdl: "#{ws['soap_endpoint']}/#{get_wsdl}?wsdl", log: true, read_timeout: 600, open_timeout: 600 do
    convert_request_keys_to :camelcase
  end
end
authentication() click to toggle source
# File lib/bseller_ruby/base.rb, line 96
def self.authentication
  unless @method =~ /pagamento/
    @authentication = { 'idCia' => ws['cia_id'], 'usuario' => ws['username'], 'senha' => ws['password'] }
    @method.to_s.end_with?('pedido') ? {} : @authentication
  end
  @authentication || {}
end
camelize(key) click to toggle source
# File lib/bseller_ruby/base.rb, line 74
def self.camelize(key)
  key.to_s.split(/_/).map{ |word| word.capitalize }.join('')
end
execute(method, path, params = {}, &block) click to toggle source
# File lib/bseller_ruby/base.rb, line 60
def self.execute(method, path, params = {}, &block)
  begin
    params[:payload] = JSON.generate(params[:body]) if params[:body]
  rescue
    params[:payload] = params[:body]
  end
  headers = { accept: 'application/json', content_type: 'application/json', 'X-AUTH-TOKEN' => ws['json_token'] }
    RestClient::Request.execute({ verify_ssl: false, method: method, url: "#{ws['json_endpoint']}/#{path}&api_key=#{ws['json_token']}", headers: headers, :timeout => 600, :open_timeout => 600}.merge(params), &block)
end
get_wsdl() click to toggle source
# File lib/bseller_ruby/base.rb, line 88
def self.get_wsdl
  @method =~ /pagamento/ ? 'CPPagamento' : 'CPPedido'
end
json_parse(value) click to toggle source
# File lib/bseller_ruby/base.rb, line 78
def self.json_parse(value)
  value.class.eql?(String) ? JSON.parse(value) : JSON.parse(value.response)
end
to_params(params) click to toggle source
# File lib/bseller_ruby/base.rb, line 70
def self.to_params(params)
  params.map { |key, value| "#{key}=#{value}" }.join "&"
end
ws() click to toggle source
# File lib/bseller_ruby/base.rb, line 92
def self.ws
  BsellerRuby.config['ws']
end