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,102 @@
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 CalculateDeadlineWithRestrictions < 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
+ @source_zip_code = data[:source_zip_code]
18
+ @target_zip_code = data[:target_zip_code]
19
+ @reference_date = data[:reference_date]
20
+ super()
21
+ end
22
+
23
+ def request
24
+ puts xml if @show_request == true
25
+ begin
26
+ format_response(CLIENT.client.call(:calc_prazo_restricao,
27
+ soap_action: 'http://tempuri.org/CalcPrazoRestricao',
28
+ xml: xml).to_hash)
29
+ rescue Savon::SOAPFault => error
30
+ generate_exception(error)
31
+ rescue Savon::HTTPError => error
32
+ if error.http.code == 401
33
+ generate_exception("Unauthorized (#{error.http.code}).")
34
+ end
35
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def xml
42
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
43
+ xml['soap'].Envelope(HELPER.namespaces) do
44
+ xml['soap'].Body do
45
+ xml['ns1'].CalcPrazoRestricao do
46
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
47
+ xml.sCepOrigem @source_zip_code
48
+ xml.sCepDestino @target_zip_code
49
+ xml.sDtCalculo HELPER.convert_date_to_string(@reference_date)
50
+ end
51
+ end
52
+ end
53
+ end.doc.root.to_xml
54
+ end
55
+
56
+ def format_response(response)
57
+ response = response[:calc_prazo_restricao_response][:calc_prazo_restricao_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
+ delivery_at_home: HELPER.convert_string_to_bool(
77
+ service[:entrega_domiciliar]
78
+ ),
79
+ delivery_on_saturdays: HELPER.convert_string_to_bool(
80
+ service[:entrega_sabado]
81
+ ),
82
+ note: service[:obs_fim],
83
+ deadline: {
84
+ days: service[:prazo_entrega].to_i,
85
+ date: HELPER.convert_string_to_date(
86
+ service[:data_max_entrega]
87
+ ),
88
+ }
89
+ }
90
+ else
91
+ {
92
+ code: service[:codigo],
93
+ error: {
94
+ code: service[:erro],
95
+ description: service[:msg_erro]
96
+ }
97
+ }
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,123 @@
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 CalculatePrice < 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
+ super()
26
+ end
27
+
28
+ def request
29
+ puts xml if @show_request == true
30
+ begin
31
+ format_response(CLIENT.client.call(:calc_preco,
32
+ soap_action: 'http://tempuri.org/CalcPreco',
33
+ xml: xml).to_hash)
34
+ rescue Savon::SOAPFault => error
35
+ generate_exception(error)
36
+ rescue Savon::HTTPError => error
37
+ if error.http.code == 401
38
+ generate_exception("Unauthorized (#{error.http.code}).")
39
+ end
40
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def xml
47
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
48
+ xml['soap'].Envelope(HELPER.namespaces) do
49
+ xml['soap'].Body do
50
+ xml['ns1'].CalcPreco do
51
+ xml.nCdEmpresa @credentials.administrative_code
52
+ xml.sDsSenha @credentials.sigep_password
53
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
54
+ xml.sCepOrigem @source_zip_code
55
+ xml.sCepDestino @target_zip_code
56
+ xml.nCdFormato HELPER.object_type(@object[:type])
57
+ xml.nVlPeso @object[:weight].to_f/1000
58
+ xml.nVlComprimento @object[:length] || 0
59
+ xml.nVlAltura @object[:height] || 0
60
+ xml.nVlLargura @object[:width] || 0
61
+ xml.nVlDiametro @object[:diameter] || 0
62
+ xml.sCdMaoPropria HELPER.convert_bool_to_string(@own_hands)
63
+ xml.sCdAvisoRecebimento HELPER.convert_bool_to_string(
64
+ @receipt_notification
65
+ )
66
+ xml.nVlValorDeclarado @declared_value || 0
67
+ end
68
+ end
69
+ end
70
+ end.doc.root.to_xml
71
+ end
72
+
73
+ def format_response(response)
74
+ response = response[:calc_preco_response][:calc_preco_result]
75
+
76
+ services = response[:servicos][:c_servico]
77
+ services = [services] if services.is_a?(Hash)
78
+
79
+ formatted_services = []
80
+ services.each do |service|
81
+ formatted_services << format_service(service)
82
+ end
83
+
84
+ {
85
+ services: formatted_services
86
+ }
87
+ end
88
+
89
+ def format_service(service)
90
+ if [0, 10, 11].include?(service[:erro].to_i)
91
+ {
92
+ code: service[:codigo],
93
+ prices: {
94
+ additional_serivces: {
95
+ own_hands: HELPER.convert_string_to_float(
96
+ service[:valor_mao_propria]
97
+ ),
98
+ receipt_notification: HELPER.convert_string_to_float(
99
+ service[:valor_aviso_recebimento]
100
+ ),
101
+ declared_value: HELPER.convert_string_to_float(
102
+ service[:valor_valor_declarado]
103
+ ),
104
+ },
105
+ only_shipping: HELPER.convert_string_to_float(
106
+ service[:valor_sem_adicionais]
107
+ ),
108
+ total: HELPER.convert_string_to_float(service[:valor])
109
+ }
110
+ }
111
+ else
112
+ {
113
+ code: service[:codigo],
114
+ error: {
115
+ code: service[:erro],
116
+ description: service[:msg_erro]
117
+ }
118
+ }
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,138 @@
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 CalculatePriceDeadline < 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
+ super()
26
+ end
27
+
28
+ def request
29
+ puts xml if @show_request == true
30
+ begin
31
+ format_response(CLIENT.client.call(:calc_preco,
32
+ soap_action: 'http://tempuri.org/CalcPrecoPrazo',
33
+ xml: xml).to_hash)
34
+ rescue Savon::SOAPFault => error
35
+ generate_exception(error)
36
+ rescue Savon::HTTPError => error
37
+ if error.http.code == 401
38
+ generate_exception("Unauthorized (#{error.http.code}).")
39
+ end
40
+ generate_exception("Unknown HTTP error (#{error.http.code}).")
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def xml
47
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
48
+ xml['soap'].Envelope(HELPER.namespaces) do
49
+ xml['soap'].Body do
50
+ xml['ns1'].CalcPrecoPrazo do
51
+ xml.nCdEmpresa @credentials.administrative_code
52
+ xml.sDsSenha @credentials.sigep_password
53
+ xml.nCdServico HELPER.format_service_codes(@service_codes)
54
+ xml.sCepOrigem @source_zip_code
55
+ xml.sCepDestino @target_zip_code
56
+ xml.nCdFormato HELPER.object_type(@object[:type])
57
+ xml.nVlPeso @object[:weight].to_f/1000
58
+ xml.nVlComprimento @object[:length] || 0
59
+ xml.nVlAltura @object[:height] || 0
60
+ xml.nVlLargura @object[:width] || 0
61
+ xml.nVlDiametro @object[:diameter] || 0
62
+ xml.sCdMaoPropria HELPER.convert_bool_to_string(@own_hands)
63
+ xml.sCdAvisoRecebimento HELPER.convert_bool_to_string(
64
+ @receipt_notification
65
+ )
66
+ xml.nVlValorDeclarado @declared_value || 0
67
+ end
68
+ end
69
+ end
70
+ end.doc.root.to_xml
71
+ end
72
+
73
+ def format_response(response)
74
+ response = response[:calc_preco_prazo_response][:calc_preco_prazo_result]
75
+
76
+ services = response[:servicos][:c_servico]
77
+ services = [services] if services.is_a?(Hash)
78
+
79
+ formatted_services = []
80
+ services.each do |service|
81
+ formatted_services << format_service(service)
82
+ end
83
+
84
+ {
85
+ services: formatted_services
86
+ }
87
+ end
88
+
89
+ def format_service(service)
90
+ if [0, 10, 11].include?(service[:erro].to_i)
91
+ delivery_on_saturdays = HELPER.convert_string_to_bool(
92
+ service[:entrega_sabado]
93
+ )
94
+
95
+ {
96
+ code: service[:codigo],
97
+ prices: {
98
+ additional_serivces: {
99
+ own_hands: HELPER.convert_string_to_float(
100
+ service[:valor_mao_propria]
101
+ ),
102
+ receipt_notification: HELPER.convert_string_to_float(
103
+ service[:valor_aviso_recebimento]
104
+ ),
105
+ declared_value: HELPER.convert_string_to_float(
106
+ service[:valor_valor_declarado]
107
+ )
108
+ },
109
+ only_shipping: HELPER.convert_string_to_float(
110
+ service[:valor_sem_adicionais]
111
+ ),
112
+ total: HELPER.convert_string_to_float(service[:valor])
113
+ },
114
+ delivery_at_home: HELPER.convert_string_to_bool(
115
+ service[:entrega_domiciliar]
116
+ ),
117
+ delivery_on_saturdays: delivery_on_saturdays,
118
+ deadline: {
119
+ days: service[:prazo_entrega].to_i,
120
+ date: HELPER.calculate_deadline_date(
121
+ service[:prazo_entrega].to_i, delivery_on_saturdays
122
+ )
123
+ },
124
+ note: service[:obs_fim]
125
+ }
126
+ else
127
+ {
128
+ code: service[:codigo],
129
+ error: {
130
+ code: service[:erro],
131
+ description: service[:msg_erro]
132
+ }
133
+ }
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -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 CalculatePriceDeadlineWithDate < 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/CalcPrecoPrazoData',
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'].CalcPrecoPrazoData 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_data_response][:calc_preco_prazo_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
+ 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