Class: Condensr

Inherits:
Object
  • Object
show all
Defined in:
lib/condensr.rb,
lib/condensr/helpers.rb,
lib/condensr/version.rb,
lib/condensr/exceptions.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_options) ⇒ Condensr



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
# File 'lib/condensr.rb', line 12

def initialize(client_options)
  # for a rails application, we should have this config in initializers/condensr.rb as follows
  # CONDENSR = Condensr.new(  {
  # aws: {
  #     access_key_id: 'akid',
  #     secret_access_key: 'secret',
  #     region: 'us-west-1',
  #     bucket: bucket_name
  # },
  # gcloud: {
  #     project_id:project_Id,
  #     key_file: path to key.json relative from the present working dir,
  #     bucket: bucket_name
  # }
  # })
  # so we can call the object CONDENSR from any part of the application
  fail ArgumentError.new("client options are missing") if client_options.empty?
  @client_options = client_options
  if (@client_options[:aws])
    Aws.config.update({
                          region: @client_options[:aws][:region],
                          credentials: Aws::Credentials.new(@client_options[:aws][:access_key_id], @client_options[:aws][:secret_access_key])
                      })
  end

  if (@client_options[:gcloud])
    key_file =  Pathname.pwd + @client_options[:gcloud][:key_file] if  @client_options[:gcloud][:key_file]
    @gcloud = Gcloud.new(@client_options[:gcloud][:project_id],  @client_options[:gcloud][:key] || key_file)
  end
end

Class Method Details

.expand_file_path(file_path) ⇒ Object



6
7
8
# File 'lib/condensr/helpers.rb', line 6

def self.expand_file_path(file_path)
  File.expand_path("../#{file_path}", File.dirname(__FILE__))
end

.extract_file_name(url) ⇒ Object



2
3
4
# File 'lib/condensr/helpers.rb', line 2

def self.extract_file_name(url)
  url.rpartition('/').last.tr('[|&;$%@"<>''()+,]', '')
end

Instance Method Details

#condense(options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/condensr.rb', line 43

def condense(options)
  # options should be
  # {
  #     upload_type: 'aws' || 'gcloud',
  #     file_url: file_url,
  #     destination_name: destination_name
  #     acl: optional, 'public-read' as default in the list of aws canned acl (http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html)
  # }
  fail ArgumentError.new("Required options not supplied") if (!options[:upload_type] || !options[:file_url])

  options[:file_name] = Condensr.extract_file_name(options[:file_url])
  options[:destination_name] = options[:destination_name].empty? ? options[:file_name] : options[:destination_name]
  file_path = download(options)
  output = upload(options, file_path)
  clear_file(file_path)
  output
end

#download(options) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/condensr.rb', line 63

def download(options)
  fetch_object = HTTParty.get(options[:file_url])
  file_path = Pathname.pwd + options[:file_name]
  File.open(file_path, "wb") do |f|
    f.write(fetch_object.parsed_response)
  end if fetch_object
  file_path
end