Class: ThaiPostTracking::ThailandPost::Request
- Inherits:
-
Object
- Object
- ThaiPostTracking::ThailandPost::Request
- Includes:
- HTTParty
- Defined in:
- lib/thai_post_tracking/thailand_post/request.rb
Instance Method Summary collapse
- #get_token ⇒ Object
-
#initialize ⇒ Request
constructor
A new instance of Request.
- #response(trackings) ⇒ Object
- #token ⇒ Object
- #token_header ⇒ Object
- #token_string ⇒ Object
Constructor Details
#initialize ⇒ Request
Returns a new instance of Request.
8 9 10 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 8 def initialize @token_key = ThaiPostTracking.configuration.thailand_post_token end |
Instance Method Details
#get_token ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 49 def get_token = { headers: { Authorization: "Token #{ThaiPostTracking.configuration.thailand_post_token}", "Content-Type" => "application/json" } } self.class.post(@token_url, ) end |
#response(trackings) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 12 def response(trackings) body = { "status" => "all", "language" => "TH", "barcode" => trackings } = { headers: token_header, body: body.to_json, debug_output: STDOUT } response = self.class.post(@request_url, ) if response&.code == 200 response.parsed_response["response"]["items"] end end |
#token ⇒ Object
43 44 45 46 47 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 43 def token response = get_token return if response&.code != 200 JSON.parse(response.body)["token"] end |
#token_header ⇒ Object
32 33 34 35 36 37 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 32 def token_header { Authorization: token_string, "Content-Type" => "application/json" } end |
#token_string ⇒ Object
39 40 41 |
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 39 def token_string "Token #{token}" end |