Class: CdekClient::Client
- Inherits:
-
AbstractClient
- Object
- AbstractClient
- CdekClient::Client
- Defined in:
- lib/cdek_client/client.rb
Instance Method Summary collapse
- #calculate_price(params) ⇒ Object
- #call_courier(params) ⇒ Object
- #delete_orders(params) ⇒ Object
-
#initialize(account = nil, password = nil) ⇒ Client
constructor
A new instance of Client.
- #new_orders(params) ⇒ Object
- #new_schedule(params) ⇒ Object
- #order_infos(params) ⇒ Object
- #order_statuses(params = {}) ⇒ Object
- #orders_print(params) ⇒ Object
- #pickup_points(params = {}) ⇒ Object
Constructor Details
#initialize(account = nil, password = nil) ⇒ Client
Returns a new instance of Client.
11 12 13 14 |
# File 'lib/cdek_client/client.rb', line 11 def initialize(account = nil, password = nil) @account = account @password = password end |
Instance Method Details
#calculate_price(params) ⇒ Object
218 219 220 |
# File 'lib/cdek_client/client.rb', line 218 def calculate_price(params) calculator.calculate params end |
#call_courier(params) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/cdek_client/client.rb', line 141 def call_courier(params) params = normalize_request_data params params[:CallCount] = params[:Call].is_a?(Array) ? params[:Call].length : 1 params = { CallCourier: params } result = url_for(:primary, :call_courier), url_for(:secondary, :call_courier), params if result.errors.any? result.set_data [] return result end normalized_data = [] if result.data[:response].has_key? :CallCourier Util.array_wrap(result.data[:response][:CallCourier]).each do |error_data| next unless error_data.has_key?(:ErrorCode) error = CdekClient.get_api_error error_data[:ErrorCode], error_data[:Msg] result.add_error error end end if result.data[:response].has_key? :Call Util.array_wrap(result.data[:response][:Call]).each do |call_data| if call_data.has_key? :ErrorCode error = CdekClient.get_api_error call_data[:ErrorCode], call_data[:Msg] result.add_error error elsif call_data.has_key? :Number normalized_call_data = normalize_response_data call_data, response_normalization_rules_for(:call_courier) normalized_data.push normalized_call_data end end end result.set_data normalized_data result end |
#delete_orders(params) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cdek_client/client.rb', line 173 def delete_orders(params) params = normalize_request_data params params[:OrderCount] = params[:Order].is_a?(Array) ? params[:Order].length : 1 params = { DeleteRequest: params } result = url_for(:primary, :delete_orders), url_for(:secondary, :delete_orders), params # Currently API returns '500 - internal server error' for some invalid requests here. # Continue processing data if it is such error but response data is present if result.errors.any? && (result.errors.length > 1 || result.errors[0].code != 500 || !result.data.is_a?(Hash) || !result.data.has_key?(:response)) result.set_data [] return result end normalized_data = [] Util.array_wrap(result.data[:response][:DeleteRequest]).each do |delete_data| if delete_data.has_key? :ErrorCode error = CdekClient.get_api_error delete_data[:ErrorCode], delete_data[:Msg] result.add_error error elsif delete_data.has_key? :Number normalized_delete_data = normalize_response_data delete_data, response_normalization_rules_for(:delete_orders) normalized_data.push normalized_delete_data end end result.set_data normalized_data result end |
#new_orders(params) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cdek_client/client.rb', line 79 def new_orders(params) params = normalize_request_data params params[:OrderCount] = params[:Order].is_a?(Array) ? params[:Order].length : 1 params = { DeliveryRequest: params } result = url_for(:primary, :new_orders), url_for(:secondary, :new_orders), params if result.errors.any? result.set_data [] return result end normalized_data = [] if result.data[:response].has_key? :DeliveryRequest Util.array_wrap(result.data[:response][:DeliveryRequest]).each do |error_data| error = CdekClient.get_api_error error_data[:ErrorCode], error_data[:Msg] result.add_error error end end if result.data[:response].has_key? :Order Util.array_wrap(result.data[:response][:Order]).each do |order_data| if order_data.has_key? :ErrorCode error = CdekClient.get_api_error order_data[:ErrorCode], order_data[:Msg] result.add_error error elsif order_data.has_key? :Number normalized_order_data = normalize_response_data order_data, response_normalization_rules_for(:new_orders) normalized_data.push normalized_order_data end end end result.set_data normalized_data result end |
#new_schedule(params) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cdek_client/client.rb', line 110 def new_schedule(params) params = normalize_request_data params params[:OrderCount] = params[:Order].is_a?(Array) ? params[:Order].length : 1 params = { ScheduleRequest: params } result = url_for(:primary, :new_schedule), url_for(:secondary, :new_schedule), params if result.errors.any? result.set_data [] return result end normalized_data = [] if result.data[:response].has_key? :Order Util.array_wrap(result.data[:response][:Order]).each do |error_data| error = CdekClient.get_api_error error_data[:ErrorCode], error_data[:Msg] result.add_error error end end if result.data[:response].has_key? :ScheduleRequest Util.array_wrap(result.data[:response][:ScheduleRequest]).each do |schedule_data| if schedule_data.has_key? :ErrorCode error = CdekClient.get_api_error schedule_data[:ErrorCode], schedule_data[:Msg] result.add_error error elsif schedule_data.has_key? :Msg normalized_schedule_data = normalize_response_data schedule_data, response_normalization_rules_for(:new_schedule) normalized_data.push normalized_schedule_data end end end result.set_data normalized_data result end |
#order_infos(params) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cdek_client/client.rb', line 55 def order_infos(params) params = normalize_request_data params params = { InfoRequest: params } result = url_for(:primary, :order_infos), url_for(:secondary, :order_infos), params if result.errors.any? result.set_data [] return result end if result.data.has_key? :response result.data[:response].values.each do |error_data| error = CdekClient.get_api_error error_data[:ErrorCode], error_data[:Msg] result.add_error error end result.set_data [] elsif result.data.has_key?(:InfoReport) && result.data[:InfoReport].is_a?(Hash) normalized_data = Util.array_wrap result.data[:InfoReport][:Order] normalized_data = normalize_response_data normalized_data, response_normalization_rules_for(:order_infos) result.set_data normalized_data else result.set_data [] end result end |
#order_statuses(params = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cdek_client/client.rb', line 35 def order_statuses(params = {}) params = normalize_request_data params params = { StatusReport: params } result = url_for(:primary, :order_statuses), url_for(:secondary, :order_statuses), params if result.errors.any? result.set_data [] return result end if result.data[:StatusReport].has_key? :ErrorCode error = CdekClient.get_api_error result.data[:StatusReport][:ErrorCode], result.data[:StatusReport][:Msg] result.add_error error result.set_data [] else normalized_data = Util.array_wrap result.data[:StatusReport][:Order] normalized_data = normalize_response_data normalized_data, response_normalization_rules_for(:order_statuses) result.set_data normalized_data end result end |
#orders_print(params) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cdek_client/client.rb', line 199 def orders_print(params) params = normalize_request_data params params[:OrderCount] = params[:Order].is_a?(Array) ? params[:Order].length : 1 params = OrdersPrint: params result = raw_request url_for(:primary, :orders_print), url_for(:secondary, :orders_print), :post, {}, params errors_hash = Util.xml_to_hash(result.data) rescue nil if !errors_hash.nil? && errors_hash.has_key?(:response) [:OrdersPrint, :Order].each do |key| Util.array_wrap(errors_hash[:response][key]).each do |error_data| next unless error_data.has_key?(:ErrorCode) error = CdekClient.get_api_error error_data[:ErrorCode], error_data[:Msg] result.add_error error end end result.set_data nil end result end |
#pickup_points(params = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cdek_client/client.rb', line 16 def pickup_points(params = {}) params = normalize_request_data params result = request url_for(:primary, :pickup_points), url_for(:secondary, :pickup_points), :get, params if result.errors.any? result.set_data [] return result end if result.data[:PvzList].has_key? :ErrorCode error = CdekClient.get_api_error result.data[:PvzList][:ErrorCode], result.data[:PvzList][:Msg] result.add_error error result.set_data [] else normalized_data = Util.array_wrap result.data[:PvzList][:Pvz] normalized_data = normalize_response_data normalized_data, response_normalization_rules_for(:pickup_points) result.set_data normalized_data end result end |