caseblocks_bucket_extractor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/caseblocks_bucket_extractor.rb +100 -0
  2. metadata +61 -0
@@ -0,0 +1,100 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'open-uri'
4
+
5
+ class CaseblocksBucketExtractor
6
+ include HTTParty
7
+
8
+ attr_reader :auth_token
9
+ attr_reader :retry_limit
10
+ attr_reader :time_between_retries
11
+
12
+ headers "Accept" => "application/json"
13
+ headers "Content-Type" => "application/json"
14
+
15
+ def initialize(url, email, password, download_options={:retry_limit => 60,:time_between_retries => 60})
16
+ self.class.base_uri url
17
+
18
+ result = self.class.post("/tokens", :body => {email: email, password: password}.to_json)
19
+
20
+ if result["message"]
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
27
+
28
+ @retry_limit = download_options[:retry_limit]
29
+ @time_between_retries = download_options[:time_between_retries]
30
+ end
31
+
32
+ def download(case_type_name, bucket_slug, file_options)
33
+ response = self.class.get("/case_blocks/#{URI::encode(case_type_name)}/buckets/#{bucket_slug}/schedule_download_job")
34
+ job_url = get_job_url(response.body)
35
+ return if job_url.nil?
36
+
37
+ result = start_polling(job_url)
38
+
39
+ if !result.nil? && result["job"]["status"] == "complete"
40
+ create_files(file_options,result)
41
+ else
42
+ puts "!!!!! Job - #{job_url} did not complete. !!!!!!"
43
+ end
44
+ end
45
+
46
+ private
47
+ def start_polling(job_url)
48
+ download_complete = false
49
+ result = nil
50
+ count_of_retries = @retry_limit
51
+
52
+ while(!download_complete && count_of_retries >= 0)
53
+ hol_up
54
+ count_of_retries -= 1
55
+
56
+ response = HTTParty.get("#{job_url}?auth_token=#{self.auth_token}")
57
+
58
+ begin
59
+ result = JSON.parse(response.body)
60
+ rescue
61
+ puts "STATUS UPDATE: cannot parse JSON received response code with body of #{response.body}"
62
+ next
63
+ end
64
+
65
+ download_complete = result["job"]["status"] == "complete"
66
+ end
67
+
68
+ result
69
+ end
70
+
71
+ def hol_up
72
+ sleep @time_between_retries
73
+ end
74
+
75
+ def create_files(file_options,json)
76
+ file_options.each do |file_option|
77
+ json_file = json["job"]["job_log"]["files"].find{|f| f["file"].include?(file_option[:name])}
78
+ download_file(file_option[:path], json_file["link"])
79
+ end
80
+ end
81
+
82
+ def download_file(file_location,s3_link)
83
+ File.open(file_location, "wb") do |saved_file|
84
+ open(s3_link, 'rb') do |read_file|
85
+ saved_file.write(read_file.read)
86
+ end
87
+ end
88
+ end
89
+
90
+ def get_job_url(response_body)
91
+ begin
92
+ job_url = JSON.parse(response_body)["job_url"]
93
+ rescue
94
+ puts "SCHEDULE_DOWNLOAD: cannot get job_url with body of #{response.body}"
95
+ return nil
96
+ end
97
+
98
+ job_url
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caseblocks_bucket_extractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charlie Quinn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.11.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.11.0
30
+ description: A simple hello world gem
31
+ email: charlie@emergeadapt.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/caseblocks_bucket_extractor.rb
37
+ homepage: http://rubygems.org/gems/caseblocks_bucket_extractor
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.25
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Gem to kick off, poll and save bucket downloads from CaseBlocks
61
+ test_files: []