Class: Futureshop::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/futureshop/client.rb

Constant Summary collapse

INTERVAL =
1

Instance Method Summary collapse

Constructor Details

#initialize(shop_key:, client_id:, client_secret:, api_domain:) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
# File 'lib/futureshop/client.rb', line 9

def initialize(shop_key:, client_id:, client_secret:, api_domain:)
  @shop_key = shop_key
  @client_id = client_id
  @client_secret = client_secret
  host, port = api_domain.split(":")
  @api_domain = host
  @port = port
end

Instance Method Details

#authorizationObject



18
19
20
21
# File 'lib/futureshop/client.rb', line 18

def authorization
  authorize
  @authorization
end

#authorizeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/futureshop/client.rb', line 23

def authorize
  authorized_at = Time.now
  return if @authorization && authorized_at < @authorization[:expires_at]
  uri = URI::HTTPS.build(host: @api_domain, port: @port, path: "/oauth/token")
  request = Net::HTTP::Post.new(uri)
  request.basic_auth(@client_id, @client_secret)
  request["X-SHOP-KEY"] = @shop_key
  request.form_data = {"grant_type" => "client_credentials"}
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) {|http|
    http.request(request)
  }
  response.value
  auth = JSON.parse(response.body)
  token_type = auth["token_type"]
  raise "Unknown token_type: #{token_type}" if token_type == "bearer"
  @authorization = {
    access_token: auth["access_token"],
    token_type: token_type,
    expires_at: authorized_at + auth["expires_in"]
  }
end

#each_order(**args) ⇒ Object

See Also:

  • +orders_batch+


96
97
98
99
100
101
102
103
104
# File 'lib/futureshop/client.rb', line 96

def each_order(**args)
  return self.enum_for(__method__, **args) unless block_given?

  orders_batch **args do |orders|
    orders.each do |order|
      yield order
    end
  end
end

#get(path = "/", params: {}) ⇒ Object



63
64
65
# File 'lib/futureshop/client.rb', line 63

def get(path = "/", params: {})
  request(:get, path, params: params)
end

#order(order_no) ⇒ Object



111
112
113
# File 'lib/futureshop/client.rb', line 111

def order(order_no)
  get("/admin-api/v1/orders/#{URI.encode_www_form_component(order_no)}")
end

#orders(**args) ⇒ Object

See Also:

  • +orders_batch+


107
108
109
# File 'lib/futureshop/client.rb', line 107

def orders(**args)
  return each_order.to_a
end

#orders_batch(order_date_start: nil, order_date_end: nil, order_no: [], shipping_status: nil, payment_status: nil) {|| ... } ⇒ Object

Yields:

  • ()


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/futureshop/client.rb', line 71

def orders_batch(order_date_start: nil, order_date_end: nil, order_no: [], shipping_status: nil, payment_status: nil)
  raise "shipping_status must be nil, \"notShipped\" or \"shipped\" but given #{shipping_status.dump}" unless [nil, "notShipped", "shipped"].include?(shipping_status)
  raise "payment_status must be nil, \"notReceived\" or \"received\" but given #{shipping_status.dump}" unless [nil, "notReceived", "received"].include?(payment_status)
  params = {
    shipping_status: shipping_status,
    payment_status: payment_status
  }
  params[:order_date_start] = order_date_start.strftime("%FT%T") if order_date_start
  params[:order_date_end] = order_date_end.strftime("%FT%T") if order_date_end
  params[:order_no] = order_no unless order_no.empty?

  res = get("/admin-api/v1/shipping", params: params)
  yield res["orderList"]

  next_url = res["nextUrl"]
  while next_url
    sleep INTERVAL
    url = URI.parse(next_url)
    res = request_by_uri(:get, url)
    yield res["orderList"]
    next_url = res["nextUrl"]
  end
end

#post(path = "/", data: {}) ⇒ Object



67
68
69
# File 'lib/futureshop/client.rb', line 67

def post(path = "/", data: {})
  request(:post, path, data: data)
end

#request(method, path = "/", params: {}, data: {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/futureshop/client.rb', line 56

def request(method, path = "/", params: {}, data: {})
  raise "Unsupported method: #{method}" unless [:get, :post].include?(method)
  query = params.empty? ? nil : build_query(params)
  uri = URI::HTTPS.build(host: @api_domain, port: @port, path: path, query: query)
  request_by_uri(method, uri, data: data)
end

#request_by_uri(method, uri, data: {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/futureshop/client.rb', line 45

def request_by_uri(method, uri, data: {})
  request = Net::HTTP.const_get(method.capitalize).new(uri)
  headers.each_pair do |field, value|
    request[field] = value
  end
  request.body = data.to_json unless data.empty?
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) {|http| http.request(request)}
  response.value
  JSON.parse(response.body)
end