Module: Futureshop

Defined in:
lib/futureshop.rb,
lib/futureshop/client.rb,
lib/futureshop/version.rb,
lib/futureshop/inventory.rb

Defined Under Namespace

Classes: Client, Error, Inventory

Constant Summary collapse

VERSION =
"0.1.7"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject



76
77
78
79
80
81
82
83
# File 'lib/futureshop.rb', line 76

def client
  @client ||= Futureshop::Client.new(
    shop_key: ENV["FUTURESHOP_SHOP_KEY"],
    client_id: ENV["FUTURESHOP_CLIENT_ID"],
    client_secret: ENV["FUTURESHOP_CLIENT_SECRET"],
    api_domain: ENV["FUTURESHOP_API_DOMAIN"]
  )
end

Class Method Details

.orders(format: "json", **options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/futureshop.rb', line 10

def orders(format: "json", **options)
  require "csv" if format == "csv"

  client.each_order(**options).with_index do |row, index|
    sleep 1 unless ENV["NO_THROTTLE"] # FIXME: Delegate to client
    order = client.order(row["orderNo"])
    case format
    when "json"
      puts order.to_json
    when "csv"
      headers = []
      CSV $stdout do |csv|
        if index == 0
          csv << aggregate_headers(order)
        end
        raise "Multiple shipmentList. orderNo: #{order["orderNo"]}" if order["shipmentList"].length > 1
        order["shipmentList"].each do |shipment|
          shipment["productList"].each do |product|
            csv << order.each_pair.with_object([]) {|(key, value), values|
              case value
              when Hash
                value.each_value do |v|
                  values << v
                end
              when Array
                case key
                when "shipmentList"
                  # shipmentList's length is 1
                  shipment.each_value do |v|
                    case v
                    when Hash
                      v.each_value do |ov|
                        values << ov
                      end
                    when Array
                      product.each_pair do |k, v|
                        case k
                        when "optionPriceList"
                          v = v.collect {|optionPrice| "#{optionPrice['name']}:#{optionPrice['selectionName']}(#{optionPrice['price']})"}.join("/")
                        when "optionList"
                          v = v.collect {|option| [option["name"], option["selectionItem"]].join(":")}.join(",")
                        end
                        values << v
                      end
                    else
                      values << v
                    end
                  end
                when "couponList"
                  values << value.collect {|coupon| [coupon["id"], coupon["name"]].join(":")}.join("/")
                else
                  raise "Unknown array field: #{key}"
                end
              else
                values << value
              end
            }
          end
        end
      end
    else
      raise RuntimeError("unsupported format: #{format}")
    end
  end
end