Class: StockShaker::Client::ShopeeOP

Inherits:
Object
  • Object
show all
Includes:
StockShaker::Concern::Utility
Defined in:
lib/stock_shaker/client/shopee_op.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StockShaker::Concern::Utility

#to_query_params

Constructor Details

#initialize(server_url, shop_id) ⇒ ShopeeOP

Returns a new instance of ShopeeOP.



16
17
18
19
20
21
22
23
24
25
# File 'lib/stock_shaker/client/shopee_op.rb', line 16

def initialize(server_url, shop_id)
  @common_params = {
    shopid: shop_id.to_i,
    partner_id: StockShaker.config.shopee_config.partner_id.to_i,
    timestamp: DateTime.now.to_time.to_i
  }
  @server_url = server_url
  @rest_url = nil
  validates!
end

Instance Attribute Details

#common_paramsObject (readonly)

Returns the value of attribute common_params.



14
15
16
# File 'lib/stock_shaker/client/shopee_op.rb', line 14

def common_params
  @common_params
end

#rest_urlObject (readonly)

Returns the value of attribute rest_url.



14
15
16
# File 'lib/stock_shaker/client/shopee_op.rb', line 14

def rest_url
  @rest_url
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



14
15
16
# File 'lib/stock_shaker/client/shopee_op.rb', line 14

def server_url
  @server_url
end

Class Method Details



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stock_shaker/client/shopee_op.rb', line 59

def self.do_authorization_link
  token_base_string = "#{StockShaker.config.shopee_config.secret_key}#{StockShaker.config.shopee_config.redirect_url}"
  token = Digest::SHA256.hexdigest(token_base_string)

  params = {
    id: StockShaker.config.shopee_config.partner_id,
    token: token,
    redirect: StockShaker.config.shopee_config.redirect_url
  }

  "#{StockShaker::Client::SHOPEE_API_AUTH_URL}?#{to_query_params(params)}"
end

Instance Method Details

#do_signature(rest_url, request_params) ⇒ Object



39
40
41
42
# File 'lib/stock_shaker/client/shopee_op.rb', line 39

def do_signature(rest_url, request_params)
  signature_base_string = "#{rest_url}|#{request_params}"
  OpenSSL::HMAC.hexdigest('SHA256', StockShaker.config.shopee_config.secret_key, signature_base_string)
end

#execute(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stock_shaker/client/shopee_op.rb', line 27

def execute(request)
  request_params = @common_params.merge(request.api_params).to_json
  @rest_url = "#{@server_url}#{request.api_name}"

  signature = do_signature(@rest_url, request_params)

  request.add_header_params('content-type', 'json')
  request.add_header_params('Authorization', signature)

  perform(@rest_url, request, request_params)
end

#perform(url, request, request_params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/stock_shaker/client/shopee_op.rb', line 44

def perform(url, request, request_params)
  RestClient::Request.execute(
    method: request.http_method,
    payload: request_params,
    url: url,
    headers: request.header_params
  )
rescue StandardError => err
  err
end

#validates!Object



55
56
57
# File 'lib/stock_shaker/client/shopee_op.rb', line 55

def validates!
  raise 'server_url is required' unless @server_url
end