janio_api 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d175fb1eb0e1a7e19fa09fcda305c1041a5d1872ec8836574907967032aa7b1
4
- data.tar.gz: 593aaf0c717b6ef19739f4d3309bdd9336d4c5fcb7047d21560e12e09dde5e4a
3
+ metadata.gz: 0721ffe6796b2c0d2b0c20a5bf2b6e54b0310e40135d02c72e3c696ff1c8836f
4
+ data.tar.gz: '00250458c6169e4d52cc7465bd31d0c8df7914c866476127a7d92b9435223137'
5
5
  SHA512:
6
- metadata.gz: 4523b95ae78228abaf6da00aa50e092846c611a9426cd28868869db75ac73b0fd60dcb1d70df43ab245de4209acd51e95a48db3b27bc41d6746c657a7c5fb045
7
- data.tar.gz: 3e2b7d937d7887957f06935936729874b7b101cd8a19c1edd88b3d19b965219ddc25c497c7b667b2c70e55908a6ba43eba851eebe461e59ec72420f20b66148f
6
+ metadata.gz: 28edeba9d49aa7707bc43b95ed3a29d761fd4ff25b8996c749f3f2f651dc58e43b22940028a722c61060deae7c68849866b26275606be78a0d516ab83030b50b
7
+ data.tar.gz: c52488a5053b715f4b57659a38ff85bce4ef5c1357fac31d49dc42532f9a8cb513d1404de8e3c9120f84895bbca6c15d19023d8f38da0a67c1fca2ab2f78fde8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- janio_api (0.1.2)
4
+ janio_api (0.1.3)
5
5
  activeresource
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # JanioAPI
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/janio_api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -22,6 +18,8 @@ Or install it yourself as:
22
18
 
23
19
  ## Usage
24
20
 
21
+ ### Setup
22
+
25
23
  1. Create a file under the `initializers` folder.
26
24
  2. Set the `api_host` and `api_token` as following sample:
27
25
  ```ruby
@@ -30,54 +28,91 @@ Or install it yourself as:
30
28
  config.api_token = ENV["API_TOKEN"]
31
29
  end
32
30
  ```
33
- 3. Then you can start creating Janio orders! Here's an example:
34
31
 
35
- ```ruby
36
- attributes={
37
- service_id: 1,
38
- tracking_no: "TRACKINGNUMBERHTH333ASDA1",
39
- shipper_order_id: nil,
40
- order_length: 12,
41
- order_width: 12,
42
- order_height: 12,
43
- order_weight: 12,
44
- payment_type: "cod",
45
- cod_amount_to_collect: 40.5,
46
- consignee_name: "Susan",
47
- consignee_number: "+6291234567891",
48
- consignee_country: "Indonesia",
49
- consignee_address: "Pos 5 security komp perumahan PT INALUM tanjung gading., Jln berangin.",
50
- consignee_postal: "12420",
51
- consignee_state: "Daerah Khusus Ibukota Jakarta",
52
- consignee_city: "Jakarta Selatan",
53
- consignee_province: "Cilandak",
54
- consignee_email: "susan123@email.com",
55
- pickup_contact_name: "Jackson",
56
- pickup_contact_number: "+6591234567",
57
- pickup_country: "Singapore",
58
- pickup_address: "Jurong West Ave 1",
59
- pickup_postal: "640534",
60
- pickup_state: "Singapore State",
61
- pickup_city: nil,
62
- pickup_province: nil,
63
- pickup_date: nil,
64
- pickup_notes: nil,
65
- items: [
66
- {
67
- item_desc: "Blue Male T-Shirt",
68
- item_quantity: 2,
69
- item_product_id: "PROD123",
70
- item_sku: "ITEMSKU123",
71
- item_category: "Fashion Apparel",
72
- item_price_value: 23.5,
73
- item_price_currency: "IDR"
74
- }
75
- ]
76
- }
77
-
78
- order = JanioAPI::Order.new(attributes)
79
- order.save
80
- ```
32
+ ### Creation
33
+
34
+ 1. Here's an example:
35
+
36
+ ```ruby
37
+ attributes={
38
+ service_id: 1,
39
+ tracking_no: "TRACKINGNUMBERHTH333ASDA1",
40
+ shipper_order_id: nil,
41
+ order_length: 12,
42
+ order_width: 12,
43
+ order_height: 12,
44
+ order_weight: 12,
45
+ payment_type: "cod",
46
+ cod_amount_to_collect: 40.5,
47
+ consignee_name: "Susan",
48
+ consignee_number: "+6291234567891",
49
+ consignee_country: "Indonesia",
50
+ consignee_address: "Pos 5 security komp perumahan PT INALUM tanjung gading., Jln berangin.",
51
+ consignee_postal: "12420",
52
+ consignee_state: "Daerah Khusus Ibukota Jakarta",
53
+ consignee_city: "Jakarta Selatan",
54
+ consignee_province: "Cilandak",
55
+ consignee_email: "susan123@email.com",
56
+ pickup_contact_name: "Jackson",
57
+ pickup_contact_number: "+6591234567",
58
+ pickup_country: "Singapore",
59
+ pickup_address: "Jurong West Ave 1",
60
+ pickup_postal: "640534",
61
+ pickup_state: "Singapore State",
62
+ pickup_city: nil,
63
+ pickup_province: nil,
64
+ pickup_date: nil,
65
+ pickup_notes: nil,
66
+ items: [
67
+ {
68
+ item_desc: "Blue Male T-Shirt",
69
+ item_quantity: 2,
70
+ item_product_id: "PROD123",
71
+ item_sku: "ITEMSKU123",
72
+ item_category: "Fashion Apparel",
73
+ item_price_value: 23.5,
74
+ item_price_currency: "IDR"
75
+ }
76
+ ]
77
+ }
78
+
79
+ order = JanioAPI::Order.new(attributes)
80
+ order.save
81
+ ```
82
+
83
+ 2. Make sure you use `#valid?` or `#save_with_validation` to catch errors before POST to the server.
84
+ ```ruby
85
+ unless order.valid?
86
+ # handle invalid order
87
+ end
88
+
89
+ #or
90
+
91
+ unless order.save_with_validation
92
+ # handle invalid order
93
+ end
94
+ ```
95
+
96
+ ### Tracking
97
+
98
+ 1. You are recommended to use the webhook provided by Janio to capture the latest parcel's status.
99
+ 2. You can also track the parcel using the `#track` or `.track([one or more tracking number])`. Examples:
100
+
101
+ ```ruby
102
+ JanioAPI::Order.track(["tracking_no1", "tracking_no2"]) # one or more tracking number
103
+
104
+ # or
105
+
106
+ JanioAPI::Order.new({tracking_no: "tracking_no1"}).track
107
+ ```
108
+
109
+ ### Update
110
+
111
+ - Update is not supported yet
112
+
113
+ ### Deletion
114
+
115
+ - Deletion is not supported yet
81
116
 
82
117
  ## Development
83
118
 
@@ -0,0 +1,38 @@
1
+ module JanioAPI
2
+ class Item < Base
3
+ validates :item_desc, :item_quantity, :item_price_value, :item_price_currency, presence: true
4
+ validates :item_category, inclusion: ["Fashion Accessories", "Fashion Footwear", "Fashion Apparels (Men)", "Fashion Apparels (Women)",
5
+ "Fashion Apparels (Babies, Toddlers and Children)", "Fashion Apparel", "Electronics",
6
+ "Electronics (Non-Telecommunications)", "Electronics (Telecommunications)", "Lifestyle Products",
7
+ "Lifestyle (Health Related)", "Lifestyle (Beauty Related)", "Lifestyle (Home & Living)",
8
+ "Lifestyle (Hobbies & Collection)", "Lifestyle (Pantry & Packaged Food & Beverages)", "Others", "Printed Matters"]
9
+
10
+ class << self
11
+ def find(*args)
12
+ raise "JanioAPI::Item cannot be fetched directly, plesase use JanioAPI::Order to fetch the items under the order."
13
+ end
14
+
15
+ def create
16
+ raise "JanioAPI::Item cannot be created directly, "\
17
+ "please include it in an JanioAPI::Order to create it under a new JanioAPI::Order."
18
+ end
19
+ end
20
+
21
+ def initialize(attributes = {}, persisted = false)
22
+ default_attrs = {
23
+ item_desc: nil, item_quantity: nil, item_price_value: nil, item_price_currency: nil, item_category: nil
24
+ }
25
+ attributes = default_attrs.merge(attributes)
26
+ super
27
+ end
28
+
29
+ def create
30
+ self.class.create
31
+ end
32
+
33
+ def update
34
+ raise "JanioAPI::Item cannot be updated, only creation is supported and "\
35
+ "please include it in an JanioAPI::Order to create it under a new JanioAPI::Order."
36
+ end
37
+ end
38
+ end
@@ -1,16 +1,7 @@
1
1
  module JanioAPI
2
- ##
3
- # See http://apidocs.janio.asia/faq for parameters information
2
+ ##
3
+ # See http://apidocs.janio.asia/faq for parameters information
4
4
  class Order < Base
5
- self.prefix = "/api/order/orders/"
6
- self.element_name = ""
7
-
8
- validate :check_api_token
9
-
10
- def check_api_token
11
- errors.add(:base, "Please set the api_token in the initializer file") if JanioAPI.config.api_token.blank?
12
- end
13
-
14
5
  class Collection < ActiveResource::Collection
15
6
  attr_accessor :count, :next, :previous
16
7
 
@@ -24,6 +15,31 @@ module JanioAPI
24
15
 
25
16
  self.collection_parser = Collection
26
17
 
18
+ SUPPORTED_COUNTRIES = ["Australia", "Brunei", "China", "Hong Kong", "Indonesia", "Italy", "Japan", "Malaysia", "Philippines", "Saudi Arabia",
19
+ "Singapore", "South Korea", "Spain", "Taiwan", "Thailand", "UAE", "UK", "US", "Vietnam"].freeze
20
+
21
+ POSTAL_EXCLUDED_COUNTRIES = ["Hong Kong", "Vietnam"].freeze
22
+
23
+ self.prefix = "/api/order/orders/"
24
+ self.element_name = ""
25
+
26
+ has_many :items, class_name: "JanioAPI::Item"
27
+
28
+ validates :service_id, :order_length, :order_width, :order_height, :order_weight,
29
+ :cod_amount_to_collect, :consignee_name, :consignee_number, :consignee_country, :consignee_address, :consignee_state,
30
+ :consignee_city, :consignee_province, :consignee_email, :pickup_contact_name, :pickup_contact_number, :pickup_country, :pickup_address,
31
+ :pickup_state, :pickup_city, :pickup_province, :pickup_date, :pickup_notes, presence: true
32
+
33
+ validates :pickup_country, :consignee_country, inclusion: SUPPORTED_COUNTRIES
34
+ validates :pickup_postal, :consignee_postal, presence: true, unless: -> { POSTAL_EXCLUDED_COUNTRIES.include?(consignee_country) }
35
+ validates :pickup_postal, presence: true, unless: -> { POSTAL_EXCLUDED_COUNTRIES.include?(pickup_country) }
36
+ validates :payment_type, inclusion: ["cod", "prepaid"]
37
+ validates :cod_amount_to_collect, presence: true, if: -> { payment_type == "cod" }
38
+ validates :items, length: {minimum: 1, message: "are required. Please add at least one."}
39
+
40
+ validate :check_api_token
41
+ validate :items_validation
42
+
27
43
  class << self
28
44
  def tracking_path
29
45
  "/api/tracker/query-by-tracking-nos/"
@@ -70,6 +86,42 @@ module JanioAPI
70
86
  end
71
87
  end
72
88
 
89
+ def initialize(attributes = {}, persisted = false)
90
+ default_attrs = {
91
+ service_id: 1,
92
+ tracking_no: nil,
93
+ shipper_order_id: nil,
94
+ order_length: 12,
95
+ order_width: 12,
96
+ order_height: 12,
97
+ order_weight: 1,
98
+ payment_type: nil,
99
+ cod_amount_to_collect: 0,
100
+ consignee_name: nil,
101
+ consignee_number: nil,
102
+ consignee_country: nil,
103
+ consignee_address: nil,
104
+ consignee_postal: nil,
105
+ consignee_state: nil,
106
+ consignee_city: nil,
107
+ consignee_province: nil,
108
+ consignee_email: nil,
109
+ pickup_contact_name: nil,
110
+ pickup_contact_number: nil,
111
+ pickup_country: nil,
112
+ pickup_address: nil,
113
+ pickup_postal: nil,
114
+ pickup_state: nil,
115
+ pickup_city: nil,
116
+ pickup_province: nil,
117
+ pickup_date: nil,
118
+ pickup_notes: nil,
119
+ items: nil
120
+ }
121
+ attributes = default_attrs.merge(attributes)
122
+ super
123
+ end
124
+
73
125
  # Tracks the current order
74
126
  #
75
127
  # Check http://apidocs.janio.asia/track for more information
@@ -89,6 +141,18 @@ module JanioAPI
89
141
  end
90
142
  end
91
143
 
144
+ private
145
+
146
+ def check_api_token
147
+ errors.add(:base, "Please set the api_token in the initializer file") if JanioAPI.config.api_token.blank?
148
+ end
149
+
150
+ def items_validation
151
+ items&.each_with_index do |item, index|
152
+ item.errors.full_messages.each { |msg| errors.add("item_#{index}".to_sym, msg) } unless item.valid?
153
+ end
154
+ end
155
+
92
156
  def create(blocking: true)
93
157
  reformat_before_save(blocking)
94
158
  super()
@@ -1,3 +1,3 @@
1
1
  module JanioAPI
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: janio_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Chong
@@ -104,6 +104,7 @@ files:
104
104
  - lib/janio_api/connection.rb
105
105
  - lib/janio_api/redirect_fetcher.rb
106
106
  - lib/janio_api/resources/base.rb
107
+ - lib/janio_api/resources/item.rb
107
108
  - lib/janio_api/resources/order.rb
108
109
  - lib/janio_api/version.rb
109
110
  - lib/zeitwerk_loader.rb