class JanioAPI::Order
See apidocs.janio.asia/faq for parameters information
Constants
- DEFAULT_ATTRS
- PICKUP_DATE_ACCEPTED_COUNTRIES
- POSTAL_EXCLUDED_COUNTRIES
- SUPPORTED_CONSIGNEE_COUNTRIES
- SUPPORTED_PICKUP_COUNTRIES
- VALID_PAYMENT_TYPES
Public Class Methods
find(*arguments)
click to toggle source
override find to customize url, and only allow find_every use to find the parcel info, include the label pdf url check apidocs.janio.asia/view for more information params accept tracking numbers as well: “`tracking_no=,[tracking_no2],“`
# File lib/janio_api/resources/order.rb, line 97 def find(*arguments) scope = arguments.slice!(0) options = arguments.slice!(0) || {} options[:from] = "/api/order/order" unless options[:from] options[:params] = {} unless options[:params] options[:params][:with_items] = true unless options[:params][:with_items] options[:params][:secret_key] = JanioAPI.config.api_token case scope when :all find_every(options) when :first collection = find_every(options) collection&.first when :last collection = find_every(options) collection&.last end end
new(attributes = {}, persisted = false)
click to toggle source
Calls superclass method
JanioAPI::Base::new
# File lib/janio_api/resources/order.rb, line 142 def initialize(attributes = {}, persisted = false) if attributes[:pickup_date].is_a?(ActiveSupport::TimeWithZone) attributes[:pickup_date] = attributes[:pickup_date].strftime("%Y-%-m-%-d") end attributes = DEFAULT_ATTRS.merge(attributes) super set_service_id end
track(tracking_nos)
click to toggle source
Track one or more tracking nos
Check apidocs.janio.asia/track for more information
# File lib/janio_api/resources/order.rb, line 120 def track(tracking_nos) raise ArgumentError, "tracking_nos must be an array" unless tracking_nos.is_a?(Array) body = { get_related_updates: true, flatten_data: false, tracking_nos: tracking_nos } retries = 0 begin retries += 1 response = connection.post(tracking_path, body.to_json, headers) rescue ActiveResource::ConnectionError => e retry unless retries <= 5 raise e end self.format.decode(response.body) end
tracking_path()
click to toggle source
# File lib/janio_api/resources/order.rb, line 89 def tracking_path "/api/tracker/query-by-tracking-nos/" end
Public Instance Methods
get_service_id(service_category = "pickup")
click to toggle source
# File lib/janio_api/resources/order.rb, line 159 def get_service_id(service_category = "pickup") # only check with services offering pickup by default SERVICES.find do |s| s[:pickup_country] == pickup_country && s[:consignee_country] == consignee_country && s[:service_category] == service_category end&.dig(:id) end
pickup_date=(date)
click to toggle source
# File lib/janio_api/resources/order.rb, line 151 def pickup_date=(date) @attributes[:pickup_date] = if date.is_a?(ActiveSupport::TimeWithZone) || date.is_a?(Time) date.strftime("%Y-%-m-%-d") else date end end
save(blocking: true)
click to toggle source
# File lib/janio_api/resources/order.rb, line 186 def save(blocking: true) run_callbacks :save do new? ? create(blocking: blocking) : update(blocking: blocking) end end
set_service_id(service_category = "pickup")
click to toggle source
# File lib/janio_api/resources/order.rb, line 168 def set_service_id(service_category = "pickup") @attributes[:service_id] = get_service_id(service_category) end
track()
click to toggle source
Tracks the current order
Check apidocs.janio.asia/track for more information
# File lib/janio_api/resources/order.rb, line 175 def track body = { get_related_updates: true, flatten_data: true, tracking_nos: [@attributes[:tracking_no]] } response = connection.post(self.class.tracking_path, body.to_json, self.class.headers) self.class.format.decode(response.body)[0] end
Private Instance Methods
consignee_country_code()
click to toggle source
# File lib/janio_api/resources/order.rb, line 198 def consignee_country_code ISO3166::Country.find_country_by_name(consignee_country)&.alpha2 end
consignee_number_country_matched?()
click to toggle source
# File lib/janio_api/resources/order.rb, line 225 def consignee_number_country_matched? if Phonelib.invalid_for_country?(consignee_number, consignee_country_code) errors.add( :consignee_number, "is invalid, please make sure the phone is valid and phone's country code matches the consignee address's country" ) end end
consignee_postal_valid?()
click to toggle source
# File lib/janio_api/resources/order.rb, line 209 def consignee_postal_valid? if consignee_country_code && POSTAL_CODE_REGEX[consignee_country_code.to_sym] regex = Regexp.new(POSTAL_CODE_REGEX[consignee_country_code.to_sym]) errors.add(:consignee_postal, "is invalid, must match #{regex.inspect}") unless regex.match(consignee_postal) end end
create(blocking: true)
click to toggle source
Calls superclass method
# File lib/janio_api/resources/order.rb, line 246 def create(blocking: true) reformat_before_save(blocking) super() rescue => e reset_attributes_format raise e end
items_validation()
click to toggle source
# File lib/janio_api/resources/order.rb, line 240 def items_validation items&.each_with_index do |item, index| item.errors.full_messages.each { |msg| errors.add("item_#{index}".to_sym, msg) } unless item.valid? end end
load_attributes_from_response(response)
click to toggle source
# File lib/janio_api/resources/order.rb, line 290 def load_attributes_from_response(response) # reset the attributes structure before assign attributes that came back from server reset_attributes_format # save the response attributes if response_code_allows_body?(response.code.to_i) && (response["Content-Length"].nil? || response["Content-Length"] != "0") && !response.body.nil? && response.body.strip.size > 0 attributes = self.class.format.decode(response.body) attributes.merge!(attributes["orders"][0]).delete("orders") load(attributes, false, true) @persisted = true end end
pickup_contact_number_country_matched?()
click to toggle source
# File lib/janio_api/resources/order.rb, line 216 def pickup_contact_number_country_matched? if Phonelib.invalid_for_country?(pickup_contact_number, pickup_country_code) errors.add( :pickup_contact_number, "is invalid, please make sure the phone is valid and phone's country code matches the pickup address's country" ) end end
pickup_country_code()
click to toggle source
# File lib/janio_api/resources/order.rb, line 194 def pickup_country_code ISO3166::Country.find_country_by_name(pickup_country)&.alpha2 end
pickup_postal_valid?()
click to toggle source
# File lib/janio_api/resources/order.rb, line 202 def pickup_postal_valid? if pickup_country_code regex = Regexp.new(POSTAL_CODE_REGEX[pickup_country_code.to_sym]) errors.add(:pickup_postal, "is invalid, must match #{regex.inspect}") unless regex.match(pickup_postal) end end
reformat_before_save(blocking)
click to toggle source
Reformat the attributes before POST to server
# File lib/janio_api/resources/order.rb, line 263 def reformat_before_save(blocking) attributes = @attributes.dup @attributes.clear @attributes[:secret_key] = retrieve_api_token(attributes[:pickup_country]) # set blocking until label generated @attributes[:blocking] = blocking # reformat attributes @attributes[:orders] = [attributes] end
reset_attributes_format()
click to toggle source
# File lib/janio_api/resources/order.rb, line 284 def reset_attributes_format attributes = @attributes.dup @attributes.clear load(attributes.delete("orders")[0]) end
retrieve_api_token(country)
click to toggle source
# File lib/janio_api/resources/order.rb, line 273 def retrieve_api_token(country) if JanioAPI.config.api_tokens country_code_sym = ISO3166::Country.find_country_by_name(country)&.alpha2&.to_sym JanioAPI.config.api_tokens[country_code_sym] elsif JanioAPI.config.api_token JanioAPI.config.api_token else raise ArgumentError, "JanioAPI api_token/api_tokens is missing, please set it in the config." end end
route_supported?()
click to toggle source
# File lib/janio_api/resources/order.rb, line 234 def route_supported? unless set_service_id errors.add(:route, "not supported, if new route not available in service_id_map, please contact gem author.") end end
update(blocking: true)
click to toggle source
Calls superclass method
# File lib/janio_api/resources/order.rb, line 254 def update(blocking: true) reformat_before_save(blocking) super() rescue => e reset_attributes_format raise e end