-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.rb
64 lines (53 loc) · 1.55 KB
/
import.rb
1
2
3
4
5
6
7
8
9
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
require 'dotenv/load'
require 'shopify_api'
require 'optparse'
require 'csv'
require 'fileutils'
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby import.rb [options]"
opts.on("-v", "--verbose", "Show extra information") do
@options[:verbose] = true
end
opts.on("-f=FILE", "--file=FILE", "Input file") do |file|
@options[:input_file] = file
end
end.parse!
if @options[:input_file]
input = @options[:input_file]
else
abort "Please select an input file."
end
puts "Contecting to store: #{ENV['SHOPIFY_DOMAIN']}"
begin
ShopifyAPI::Context.setup(
# These values are required but not actually used for private apps
api_key: "DUMMY_VALUE",
host_name: "DUMMY_VALUE",
scope: "DUMMY_VALUE",
private_shop: ENV.fetch("SHOPIFY_DOMAIN"),
api_secret_key: ENV.fetch("TOKEN"),
session_storage: ShopifyAPI::Auth::FileSessionStorage.new,
is_embedded: false,
is_private: true,
api_version: "2022-07"
)
session = ShopifyAPI::Utils::SessionUtils.load_current_session
rescue => error
puts "Unable to connect to Shopify API: #{error.message}"
end
if session and input
csv_in = CSV.parse(File.read(input), :headers => true)
csv_in.each do |row|
begin
gift_card = ShopifyAPI::GiftCard.new(session: session)
gift_card.note = row["Note"]
gift_card.initial_value = row["Balance"]
gift_card.code = row["Code"]
gift_card.expires_on = row["Expires"]
gift_card.save()
rescue => error
puts "Error with gift card `#{row["Code"]}`: #{error.message}"
end
end
end