Class: ThaiPostTracking::ThailandPost::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/thai_post_tracking/thailand_post/request.rb

Direct Known Subclasses

GetItems, HookItems

Instance Method Summary collapse

Constructor Details

#initializeRequest

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_tokenObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 49

def get_token
  options = {
    headers: {
      Authorization: "Token #{ThaiPostTracking.configuration.thailand_post_token}",
      "Content-Type" => "application/json"
    }
  }

  self.class.post(@token_url, options)
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
  }

  options = {
    headers: token_header,
    body: body.to_json,
    debug_output: STDOUT
  }

  response = self.class.post(@request_url, options)

  if response&.code == 200
    response.parsed_response["response"]["items"]
  end
end

#tokenObject



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_headerObject



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_stringObject



39
40
41
# File 'lib/thai_post_tracking/thailand_post/request.rb', line 39

def token_string
  "Token #{token}"
end