Class: JanioAPI::Order

Inherits:
Base
  • Object
show all
Defined in:
lib/janio_api/resources/order.rb

Overview

See apidocs.janio.asia/faq for parameters information

Defined Under Namespace

Classes: Collection

Constant Summary collapse

SUPPORTED_PICKUP_COUNTRIES =
SERVICES.map { |s| s[:pickup_country] }.uniq.freeze
SUPPORTED_CONSIGNEE_COUNTRIES =
SERVICES.map { |s| s[:consignee_country] }.uniq.freeze
PICKUP_DATE_ACCEPTED_COUNTRIES =
["Singapore"]
POSTAL_EXCLUDED_COUNTRIES =
["Hong Kong", "Vietnam", "Brunei"].freeze
VALID_PAYMENT_TYPES =
["cod", "prepaid"].freeze
DEFAULT_ATTRS =
{
  service_id: 1,
  tracking_no: nil,
  shipper_order_id: nil,
  order_length: 12,
  order_width: 12,
  order_height: 12,
  order_weight: 1,
  payment_type: nil,
  cod_amount_to_collect: 0,
  consignee_name: nil,
  consignee_number: nil,
  consignee_country: nil,
  consignee_address: nil,
  consignee_postal: nil,
  consignee_state: nil,
  consignee_city: nil,
  consignee_province: nil,
  consignee_email: nil,
  pickup_contact_name: nil,
  pickup_contact_number: nil,
  pickup_country: nil,
  pickup_address: nil,
  pickup_postal: nil,
  pickup_state: nil,
  pickup_city: nil,
  pickup_province: nil,
  pickup_date: nil,
  pickup_notes: nil,
  items: nil
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Order

Returns a new instance of Order.



156
157
158
159
160
161
162
163
# File 'lib/janio_api/resources/order.rb', line 156

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

Class Method Details

.find(*arguments) ⇒ Object

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],“`



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/janio_api/resources/order.rb', line 111

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

.track(tracking_nos) ⇒ Object

Track one or more tracking nos

Check apidocs.janio.asia/track for more information

Raises:

  • (ArgumentError)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/janio_api/resources/order.rb', line 134

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_pathObject



103
104
105
# File 'lib/janio_api/resources/order.rb', line 103

def tracking_path
  "/api/tracker/query-by-tracking-nos/"
end

.with_custom_url(host, path, &action) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/janio_api/resources/order.rb', line 88

def with_custom_url(host, path, &action)
  old_host = self.site
  old_path = self.prefix

  self.site = URI.parse(host)
  self.prefix = path

  result = action.call

  self.site = old_host
  self.prefix = old_path

  result
end

Instance Method Details

#get_service_id(service_category = "pickup") ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/janio_api/resources/order.rb', line 173

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) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/janio_api/resources/order.rb', line 165

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) ⇒ Object



202
203
204
205
206
# File 'lib/janio_api/resources/order.rb', line 202

def save(blocking: true)
  run_callbacks :save do
    new? ? create(blocking: blocking) : update(blocking: blocking)
  end
end

#set_service_id(service_category = "pickup") ⇒ Object



182
183
184
185
186
# File 'lib/janio_api/resources/order.rb', line 182

def set_service_id(service_category = "pickup")
  return true unless @attributes[:service_id].nil?

  @attributes[:service_id] = get_service_id(service_category)
end

#trackObject

Tracks the current order

Check apidocs.janio.asia/track for more information



191
192
193
194
195
196
197
198
199
200
# File 'lib/janio_api/resources/order.rb', line 191

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