caseblocks_bucket_extractor 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/bucket_downloader +27 -26
- data/lib/caseblocks_bucket_extractor.rb +7 -12
- metadata +1 -1
data/bin/bucket_downloader
CHANGED
@@ -11,55 +11,56 @@ opts = Slop.parse do
|
|
11
11
|
|
12
12
|
on 'casetype=', 'Case Type Name'
|
13
13
|
on 'bucket=', 'Bucket Slug'
|
14
|
-
on '
|
15
|
-
on '
|
14
|
+
on 'url=', 'url to download from (default https://login.caseblocks.com)'
|
15
|
+
on 'token=', 'Authentication token for caseblocks api'
|
16
|
+
on 'relation_type=', 'Relation name to download, ie the name of the 1 to many (default domain)'
|
17
|
+
on 'path=', 'Path to safe file in'
|
18
|
+
on 'timeout=', "minutes to wait for download to complete (default 180)"
|
16
19
|
on 'f', 'forcequotes', 'Force quotation marks'
|
17
|
-
on 'l', 'local', "Download from localhost"
|
18
20
|
end
|
19
21
|
|
20
|
-
if opts[:casetype] && opts[:bucket]
|
22
|
+
if opts[:casetype] && opts[:bucket] && opts[:path] && opts[:token]
|
21
23
|
|
22
24
|
# Bucket Downloader
|
23
25
|
puts " == Bucket Downloader == "
|
24
26
|
|
25
27
|
bucket_name = opts[:bucket]
|
26
28
|
case_type = opts[:casetype]
|
29
|
+
retry_limit = (opts[:timeout] || 180).to_i
|
30
|
+
relation_type = opts[:relation_type] || "domain"
|
31
|
+
url = opts[:url] || "https://login.caseblocks.com"
|
27
32
|
|
28
|
-
puts "Downloading bucket: #{bucket_name}"
|
33
|
+
puts "Downloading bucket: #{bucket_name} from #{case_type}"
|
29
34
|
|
30
|
-
#Create instance of CaseblocksBucketExtractor passing URL, email and password with optional download_options i.e. {:retry_limit => 5,:time_between_retries => 5}
|
31
35
|
puts "Creating Bucket Extractor... "
|
32
|
-
|
33
|
-
puts "http://localhost:3001"
|
34
|
-
bucket_extractor = CaseblocksBucketExtractor.new("http://localhost:3000", opts[:username], opts[:password], {:retry_limit => 12,:time_between_retries => 10})
|
35
|
-
else
|
36
|
-
bucket_extractor = CaseblocksBucketExtractor.new("https://login.caseblocks.com", opts[:username], opts[:password], {:retry_limit => 5,:time_between_retries => 5})
|
37
|
-
end
|
38
|
-
|
39
|
-
#Setup file options array. :name -> will find extracted CSV file containing value, :path -> local file path to save file to
|
36
|
+
bucket_extractor = CaseblocksBucketExtractor.new(url, opts[:token], {:retry_limit => retry_limit,:time_between_retries => 60})
|
40
37
|
|
41
|
-
file_path =
|
38
|
+
file_path = opts[:path] + "/"+ DateTime.now.strftime("%y-%m-%d-%M-%H") + "_" + bucket_name + ".csv"
|
42
39
|
|
43
|
-
file_options = [{:path => file_path, :name => "
|
40
|
+
file_options = [{:path => file_path, :name => "#{relation_type}.csv"}]
|
44
41
|
|
45
42
|
puts "Downloading to #{file_path}"
|
46
43
|
|
47
44
|
#Begin download specifying Case Type Name, bucket slug and the file options defined above
|
48
|
-
|
45
|
+
begin
|
46
|
+
bucket_extractor.download(case_type, bucket_name, file_options)
|
49
47
|
|
50
|
-
|
51
|
-
|
48
|
+
if opts[:forcequotes]
|
49
|
+
puts "Forcing fields to have quotes"
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
CSV.open("#{file_path[0..-5]}.tmp", "w+", :force_quotes => true) do |tmp_csv|
|
52
|
+
puts "file_path: #{file_path}"
|
53
|
+
CSV.open(file_path, "r").each do |row|
|
54
|
+
tmp_csv << row
|
55
|
+
end
|
57
56
|
end
|
57
|
+
FileUtils.mv("#{file_path[0..-5]}.tmp", file_path)
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
|
+
puts "End Bucket Downloader"
|
61
|
+
rescue => e
|
62
|
+
puts "Download has not completed successully: #{e.message}"
|
60
63
|
end
|
61
|
-
|
62
|
-
puts "End Bucket Downloader"
|
63
64
|
else
|
64
65
|
puts opts
|
65
66
|
end
|
@@ -12,18 +12,12 @@ class CaseblocksBucketExtractor
|
|
12
12
|
headers "Accept" => "application/json"
|
13
13
|
headers "Content-Type" => "application/json"
|
14
14
|
|
15
|
-
def initialize(url,
|
15
|
+
def initialize(url, auth_token, download_options={:retry_limit => 60,:time_between_retries => 60})
|
16
16
|
self.class.base_uri url
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
puts "unable to generate token"
|
22
|
-
raise
|
23
|
-
else
|
24
|
-
@auth_token = result["token"] if result
|
25
|
-
self.class.default_params("auth_token" => @auth_token)
|
26
|
-
end
|
18
|
+
puts url
|
19
|
+
@auth_token = auth_token
|
20
|
+
self.class.default_params("auth_token" => @auth_token)
|
27
21
|
|
28
22
|
@retry_limit = download_options[:retry_limit]
|
29
23
|
@time_between_retries = download_options[:time_between_retries]
|
@@ -32,14 +26,15 @@ class CaseblocksBucketExtractor
|
|
32
26
|
def download(case_type_name, bucket_slug, file_options)
|
33
27
|
response = self.class.get("/case_blocks/#{URI::encode(case_type_name)}/buckets/#{bucket_slug}/schedule_download_job")
|
34
28
|
job_url = get_job_url(response.body)
|
35
|
-
|
29
|
+
|
30
|
+
raise response.headers["status"] unless response.headers["status"].start_with?("200")
|
36
31
|
|
37
32
|
result = start_polling(job_url)
|
38
33
|
|
39
34
|
if !result.nil? && result["job"]["status"] == "complete"
|
40
35
|
create_files(file_options,result)
|
41
36
|
else
|
42
|
-
|
37
|
+
raise "!!!!! Job - #{job_url} did not complete. !!!!!!"
|
43
38
|
end
|
44
39
|
end
|
45
40
|
|