Class: BsellerRuby::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/bseller_ruby/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.api ⇒ Object
-
.authentication ⇒ Object
-
.call(method, params) ⇒ Object
-
.camelize(key) ⇒ Object
-
.collection_name ⇒ Object
-
.create(method, params) ⇒ Object
-
.execute(method, path, params = {}, &block) ⇒ Object
-
.get(resource, params = {}) ⇒ Object
-
.get_wsdl ⇒ Object
-
.json_parse(value) ⇒ Object
-
.post(path, body, &block) ⇒ Object
-
.put(resource, path, payload) ⇒ Object
-
.to_params(params) ⇒ Object
-
.ws ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(params) ⇒ Base
Returns a new instance of Base.
13
14
15
16
17
18
|
# 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
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
11
12
13
|
# File 'lib/bseller_ruby/base.rb', line 11
def response
@response
end
|
Class Method Details
.api ⇒ Object
82
83
84
85
86
|
# 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 ⇒ Object
96
97
98
99
100
101
102
|
# 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
|
.call(method, params) ⇒ Object
35
36
37
38
39
|
# 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
|
.camelize(key) ⇒ Object
74
75
76
|
# File 'lib/bseller_ruby/base.rb', line 74
def self.camelize(key)
key.to_s.split(/_/).map{ |word| word.capitalize }.join('')
end
|
.collection_name ⇒ Object
31
32
33
|
# File 'lib/bseller_ruby/base.rb', line 31
def collection_name
@collection_name = "#{resource_name}s"
end
|
.create(method, params) ⇒ Object
41
42
43
|
# File 'lib/bseller_ruby/base.rb', line 41
def create(method, params)
new response: call(method, params)
end
|
.execute(method, path, params = {}, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
|
# 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
= { 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: }.merge(params), &block)
end
|
.get(resource, params = {}) ⇒ Object
45
46
47
|
# File 'lib/bseller_ruby/base.rb', line 45
def get(resource, params = {})
json_parse(execute(:get, "#{resource}?#{to_params(params)}"))
end
|
.get_wsdl ⇒ Object
88
89
90
|
# File 'lib/bseller_ruby/base.rb', line 88
def self.get_wsdl
@method =~ /pagamento/ ? 'CPPagamento' : 'CPPedido'
end
|
.json_parse(value) ⇒ Object
78
79
80
|
# 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
|
.post(path, body, &block) ⇒ Object
49
50
51
|
# 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) ⇒ Object
53
54
55
|
# File 'lib/bseller_ruby/base.rb', line 53
def put(resource, path, payload)
json_parse(execute(:put, "#{resource}/#{path}?a=1", body: payload))
end
|
.to_params(params) ⇒ Object
70
71
72
|
# File 'lib/bseller_ruby/base.rb', line 70
def self.to_params(params)
params.map { |key, value| "#{key}=#{value}" }.join "&"
end
|
.ws ⇒ Object
92
93
94
|
# File 'lib/bseller_ruby/base.rb', line 92
def self.ws
BsellerRuby.config['ws']
end
|