correios_gem 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/lib/SRO/client.rb +25 -0
  3. data/lib/SRO/helper.rb +118 -0
  4. data/lib/SRO/requests/track_shippings.rb +142 -0
  5. data/lib/SRO/requests/track_shippings_list.rb +144 -0
  6. data/lib/correios_exception.rb +11 -0
  7. data/lib/correios_gem.rb +186 -0
  8. data/lib/credentials.rb +27 -0
  9. data/lib/pricefier/client.rb +20 -0
  10. data/lib/pricefier/helper.rb +72 -0
  11. data/lib/pricefier/requests/calculate_deadline.rb +100 -0
  12. data/lib/pricefier/requests/calculate_deadline_with_date.rb +102 -0
  13. data/lib/pricefier/requests/calculate_deadline_with_restrictions.rb +102 -0
  14. data/lib/pricefier/requests/calculate_price.rb +123 -0
  15. data/lib/pricefier/requests/calculate_price_deadline.rb +138 -0
  16. data/lib/pricefier/requests/calculate_price_deadline_with_date.rb +142 -0
  17. data/lib/pricefier/requests/calculate_price_deadline_with_restrictions.rb +142 -0
  18. data/lib/pricefier/requests/calculate_price_fac.rb +106 -0
  19. data/lib/pricefier/requests/calculate_price_with_date.rb +125 -0
  20. data/lib/pricefier/requests/list_services.rb +87 -0
  21. data/lib/pricefier/requests/list_services_star.rb +87 -0
  22. data/lib/reverse_logistics/client.rb +34 -0
  23. data/lib/reverse_logistics/helper.rb +95 -0
  24. data/lib/reverse_logistics/requests/calculate_ticket_number_check_digit.rb +66 -0
  25. data/lib/reverse_logistics/requests/cancel_shipping.rb +70 -0
  26. data/lib/reverse_logistics/requests/create_shippings.rb +166 -0
  27. data/lib/reverse_logistics/requests/create_shippings_with_collection.rb +152 -0
  28. data/lib/reverse_logistics/requests/request_ticket_numbers.rb +90 -0
  29. data/lib/reverse_logistics/requests/track_shipping.rb +121 -0
  30. data/lib/reverse_logistics/requests/track_shippings_by_date.rb +133 -0
  31. data/lib/sigep/client.rb +29 -0
  32. data/lib/sigep/helper.rb +234 -0
  33. data/lib/sigep/requests/calculate_label_number_check_digit.rb +72 -0
  34. data/lib/sigep/requests/cancel_shipping.rb +72 -0
  35. data/lib/sigep/requests/check_card_status.rb +69 -0
  36. data/lib/sigep/requests/check_service_availability.rb +91 -0
  37. data/lib/sigep/requests/create_shippings.rb +70 -0
  38. data/lib/sigep/requests/request_label_numbers.rb +76 -0
  39. data/lib/sigep/requests/request_shippings_xml.rb +208 -0
  40. data/lib/sigep/requests/search_available_additional_services.rb +62 -0
  41. data/lib/sigep/requests/search_customer.rb +129 -0
  42. data/lib/sigep/requests/search_zip_code.rb +63 -0
  43. data/lib/sigep/requests/track_shippings.rb +145 -0
  44. metadata +145 -0
@@ -0,0 +1,142 @@
1
+ require 'savon'
2
+ require 'nokogiri'
3
+
4
+ require_relative '../client'
5
+ require_relative '../helper'
6
+ require_relative '../../correios_exception.rb'
7
+
8
+ module Correios
9
+ module Pricefier
10
+ class CalculatePriceDeadlineWithRestrictions < CorreiosException
11
+ HELPER = Helper.new
12
+ CLIENT = Client.new
13
+
14
+ def initialize(data = {})
15
+ @credentials = Correios.credentials
16
+
17
+ @show_request = data[:show_request]
18
+ @service_codes = data[:service_codes]
19
+ @source_zip_code = data[:source_zip_code]
20
+ @target_zip_code = data[:target_zip_code]
21
+ @object = data[:object]
22
+ @own_hands = data[:own_hands]
23
+ @receipt_notification = data[:receipt_notification]
24
+ @declared_value = data[:declared_value]
25
+ @reference_date = data[:reference_date]
26
+ super()
27
+ end
28
+
29
+ def request
30
+ puts xml if @show_request == true
31
+ begin
32
+ format_response(CLIENT.client.call(:calc_preco,
33
+ soap_action: 'http://tempuri.org/CalcPrecoPrazoRestricao',
34
+ xml: xml).to_hash)
35
+ rescue Savon::SOAPFault => error
36
+ generate_exception(error)
37
+ rescue Savon::HTTPError => error
38
+ if error.http.code == 401
39
+ generate_exception("Unauthorized (#{error.http.code}).")
40
+ end
41
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def xml
48
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
49
+ xml['soap'].Envelope(HELPER.namespaces) do
50
+ xml['soap'].Body do
51
+ xml['ns1'].CalcPrecoPrazoRestricao do
52
+ xml.nCdEmpresa @credentials.administrative_code
53
+ xml.sDsSenha @credentials.sigep_password
54
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
55
+ xml.sCepOrigem @source_zip_code
56
+ xml.sCepDestino @target_zip_code
57
+ xml.nCdFormato HELPER.object_type(@object[:type])
58
+ xml.nVlPeso @object[:weight].to_f/1000
59
+ xml.nVlComprimento @object[:length] || 0
60
+ xml.nVlAltura @object[:height] || 0
61
+ xml.nVlLargura @object[:width] || 0
62
+ xml.nVlDiametro @object[:diameter] || 0
63
+ xml.sCdMaoPropria HELPER.convert_bool_to_string(@own_hands)
64
+ xml.sCdAvisoRecebimento HELPER.convert_bool_to_string(
65
+ @receipt_notification
66
+ )
67
+ xml.nVlValorDeclarado @declared_value || 0
68
+ xml.sDtCalculo HELPER.convert_date_to_string(@reference_date)
69
+ end
70
+ end
71
+ end
72
+ end.doc.root.to_xml
73
+ end
74
+
75
+ def format_response(response)
76
+ response = response[:calc_preco_prazo_restricao_response][:calc_preco_prazo_restricao_result]
77
+
78
+ services = response[:servicos][:c_servico]
79
+ services = [services] if services.is_a?(Hash)
80
+
81
+ formatted_services = []
82
+ services.each do |service|
83
+ formatted_services << format_service(service)
84
+ end
85
+
86
+ {
87
+ services: formatted_services
88
+ }
89
+ end
90
+
91
+ def format_service(service)
92
+ if [0, 10, 11].include?(service[:erro].to_i)
93
+ delivery_on_saturdays = HELPER.convert_string_to_bool(
94
+ service[:entrega_sabado]
95
+ )
96
+
97
+ {
98
+ code: service[:codigo],
99
+ prices: {
100
+ additional_serivces: {
101
+ own_hands: HELPER.convert_string_to_float(
102
+ service[:valor_mao_propria]
103
+ ),
104
+ receipt_notification: HELPER.convert_string_to_float(
105
+ service[:valor_aviso_recebimento]
106
+ ),
107
+ declared_value: HELPER.convert_string_to_float(
108
+ service[:valor_valor_declarado]
109
+ )
110
+ },
111
+ only_shipping: HELPER.convert_string_to_float(
112
+ service[:valor_sem_adicionais]
113
+ ),
114
+ total: HELPER.convert_string_to_float(service[:valor])
115
+ },
116
+ delivery_at_home: HELPER.convert_string_to_bool(
117
+ service[:entrega_domiciliar]
118
+ ),
119
+ delivery_on_saturdays: delivery_on_saturdays,
120
+ deadline: {
121
+ days: service[:prazo_entrega].to_i,
122
+ date: HELPER.calculate_deadline_date(
123
+ service[:prazo_entrega].to_i,
124
+ delivery_on_saturdays,
125
+ @reference_date
126
+ )
127
+ },
128
+ note: service[:obs_fim]
129
+ }
130
+ else
131
+ {
132
+ code: service[:codigo],
133
+ error: {
134
+ code: service[:erro],
135
+ description: service[:msg_erro]
136
+ }
137
+ }
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,106 @@
1
+ require 'savon'
2
+ require 'nokogiri'
3
+
4
+ require_relative '../client'
5
+ require_relative '../helper'
6
+ require_relative '../../correios_exception.rb'
7
+
8
+ module Correios
9
+ module Pricefier
10
+ class CalculatePriceFAC < CorreiosException
11
+ HELPER = Helper.new
12
+ CLIENT = Client.new
13
+
14
+ def initialize(data = {})
15
+ @show_request = data[:show_request]
16
+ @service_codes = data[:service_codes]
17
+ @weight = data[:weight]
18
+ @reference_date = data[:reference_date]
19
+ super()
20
+ end
21
+
22
+ def request
23
+ puts xml if @show_request == true
24
+ begin
25
+ format_response(CLIENT.client.call(:calc_preco_fac,
26
+ soap_action: 'http://tempuri.org/CalcPrecoFAC',
27
+ xml: xml).to_hash)
28
+ rescue Savon::SOAPFault => error
29
+ generate_exception(error)
30
+ rescue Savon::HTTPError => error
31
+ if error.http.code == 401
32
+ generate_exception("Unauthorized (#{error.http.code}).")
33
+ end
34
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def xml
41
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
42
+ xml['soap'].Envelope(HELPER.namespaces) do
43
+ xml['soap'].Body do
44
+ xml['ns1'].CalcPrecoFAC do
45
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
46
+ xml.nVlPeso @weight
47
+ xml.strDataCalculo HELPER.convert_date_to_string(
48
+ @reference_date
49
+ )
50
+ end
51
+ end
52
+ end
53
+ end.doc.root.to_xml
54
+ end
55
+
56
+ def format_response(response)
57
+ response = response[:calc_preco_fac_response][:calc_preco_fac_result]
58
+
59
+ services = response[:servicos][:c_servico]
60
+ services = [services] if services.is_a?(Hash)
61
+
62
+ formatted_services = []
63
+ services.each do |service|
64
+ formatted_services << format_service(service)
65
+ end
66
+
67
+ {
68
+ services: formatted_services
69
+ }
70
+ end
71
+
72
+ def format_service(service)
73
+ if [0, 10, 11].include?(service[:erro].to_i)
74
+ {
75
+ code: service[:codigo],
76
+ prices: {
77
+ additional_serivces: {
78
+ own_hands: HELPER.convert_string_to_float(
79
+ service[:valor_mao_propria]
80
+ ),
81
+ receipt_notification: HELPER.convert_string_to_float(
82
+ service[:valor_aviso_recebimento]
83
+ ),
84
+ declared_value: HELPER.convert_string_to_float(
85
+ service[:valor_valor_declarado]
86
+ ),
87
+ },
88
+ only_shipping: HELPER.convert_string_to_float(
89
+ service[:valor_sem_adicionais]
90
+ ),
91
+ total: HELPER.convert_string_to_float(service[:valor])
92
+ }
93
+ }
94
+ else
95
+ {
96
+ code: service[:codigo],
97
+ error: {
98
+ code: service[:erro],
99
+ description: service[:msg_erro]
100
+ }
101
+ }
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,125 @@
1
+ require 'savon'
2
+ require 'nokogiri'
3
+
4
+ require_relative '../client'
5
+ require_relative '../helper'
6
+ require_relative '../../correios_exception.rb'
7
+
8
+ module Correios
9
+ module Pricefier
10
+ class CalculatePriceWithDate < CorreiosException
11
+ HELPER = Helper.new
12
+ CLIENT = Client.new
13
+
14
+ def initialize(data = {})
15
+ @credentials = Correios.credentials
16
+
17
+ @show_request = data[:show_request]
18
+ @service_codes = data[:service_codes]
19
+ @source_zip_code = data[:source_zip_code]
20
+ @target_zip_code = data[:target_zip_code]
21
+ @object = data[:object]
22
+ @own_hands = data[:own_hands]
23
+ @receipt_notification = data[:receipt_notification]
24
+ @declared_value = data[:declared_value]
25
+ @reference_date = data[:reference_date]
26
+ super()
27
+ end
28
+
29
+ def request
30
+ puts xml if @show_request == true
31
+ begin
32
+ format_response(CLIENT.client.call(:calc_preco_data,
33
+ soap_action: 'http://tempuri.org/CalcPrecoData',
34
+ xml: xml).to_hash)
35
+ rescue Savon::SOAPFault => error
36
+ generate_exception(error)
37
+ rescue Savon::HTTPError => error
38
+ if error.http.code == 401
39
+ generate_exception("Unauthorized (#{error.http.code}).")
40
+ end
41
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def xml
48
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
49
+ xml['soap'].Envelope(HELPER.namespaces) do
50
+ xml['soap'].Body do
51
+ xml['ns1'].CalcPrecoData do
52
+ xml.nCdEmpresa @credentials.administrative_code
53
+ xml.sDsSenha @credentials.sigep_password
54
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
55
+ xml.sCepOrigem @source_zip_code
56
+ xml.sCepDestino @target_zip_code
57
+ xml.nCdFormato HELPER.object_type(@object[:type])
58
+ xml.nVlPeso @object[:weight].to_f/1000
59
+ xml.nVlComprimento @object[:length] || 0
60
+ xml.nVlAltura @object[:height] || 0
61
+ xml.nVlLargura @object[:width] || 0
62
+ xml.nVlDiametro @object[:diameter] || 0
63
+ xml.sCdMaoPropria HELPER.convert_bool_to_string(@own_hands)
64
+ xml.sCdAvisoRecebimento HELPER.convert_bool_to_string(
65
+ @receipt_notification
66
+ )
67
+ xml.nVlValorDeclarado @declared_value || 0
68
+ xml.sDtCalculo HELPER.convert_date_to_string(@reference_date)
69
+ end
70
+ end
71
+ end
72
+ end.doc.root.to_xml
73
+ end
74
+
75
+ def format_response(response)
76
+ response = response[:calc_preco_data_response][:calc_preco_data_result]
77
+
78
+ services = response[:servicos][:c_servico]
79
+ services = [services] if services.is_a?(Hash)
80
+
81
+ formatted_services = []
82
+ services.each do |service|
83
+ formatted_services << format_service(service)
84
+ end
85
+
86
+ {
87
+ services: formatted_services
88
+ }
89
+ end
90
+
91
+ def format_service(service)
92
+ if [0, 10, 11].include?(service[:erro].to_i)
93
+ {
94
+ code: service[:codigo],
95
+ prices: {
96
+ additional_serivces: {
97
+ own_hands: HELPER.convert_string_to_float(
98
+ service[:valor_mao_propria]
99
+ ),
100
+ receipt_notification: HELPER.convert_string_to_float(
101
+ service[:valor_aviso_recebimento]
102
+ ),
103
+ declared_value: HELPER.convert_string_to_float(
104
+ service[:valor_valor_declarado]
105
+ ),
106
+ },
107
+ only_shipping: HELPER.convert_string_to_float(
108
+ service[:valor_sem_adicionais]
109
+ ),
110
+ total: HELPER.convert_string_to_float(service[:valor])
111
+ }
112
+ }
113
+ else
114
+ {
115
+ code: service[:codigo],
116
+ error: {
117
+ code: service[:erro],
118
+ description: service[:msg_erro]
119
+ }
120
+ }
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,87 @@
1
+ require 'savon'
2
+ require 'nokogiri'
3
+
4
+ require_relative '../client'
5
+ require_relative '../helper'
6
+ require_relative '../../correios_exception.rb'
7
+
8
+ module Correios
9
+ module Pricefier
10
+ class ListServices < CorreiosException
11
+ HELPER = Helper.new
12
+ CLIENT = Client.new
13
+
14
+ def initialize(data = {})
15
+ @show_request = data[:show_request]
16
+ super()
17
+ end
18
+
19
+ def request
20
+ puts xml if @show_request == true
21
+ begin
22
+ format_response(CLIENT.client.call(:calc_preco,
23
+ soap_action: 'http://tempuri.org/ListaServicos',
24
+ xml: xml).to_hash)
25
+ rescue Savon::SOAPFault => error
26
+ generate_exception(error)
27
+ rescue Savon::HTTPError => error
28
+ if error.http.code == 401
29
+ generate_exception("Unauthorized (#{error.http.code}).")
30
+ end
31
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def xml
38
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
39
+ xml['soap'].Envelope(HELPER.namespaces) do
40
+ xml['soap'].Body do
41
+ xml['ns1'].ListaServicos
42
+ end
43
+ end
44
+ end.doc.root.to_xml
45
+ end
46
+
47
+ def format_response(response)
48
+ response = response[:lista_servicos_response][:lista_servicos_result]
49
+
50
+ services = response[:servicos_calculo][:c_servicos_calculo]
51
+ services = [services] if services.is_a?(Hash)
52
+
53
+ formatted_services = []
54
+ services.each do |service|
55
+ formatted_services << format_service(service)
56
+ end
57
+
58
+ {
59
+ services: formatted_services
60
+ }
61
+ end
62
+
63
+ def format_service(service)
64
+ if service[:erro].to_i == 0
65
+ {
66
+ code: service[:codigo],
67
+ description: service[:descricao].strip,
68
+ calculate_price: HELPER.convert_string_to_bool(
69
+ service[:calcula_preco]
70
+ ),
71
+ calculate_deadline: HELPER.convert_string_to_bool(
72
+ service[:calcula_prazo]
73
+ )
74
+ }
75
+ else
76
+ {
77
+ code: service[:codigo],
78
+ error: {
79
+ code: service[:erro],
80
+ description: service[:msg_erro]
81
+ }
82
+ }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end