Class: Sendl::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/sendl/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Uploader

Returns a new instance of Uploader.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sendl/uploader.rb', line 6

def initialize(options)
  argument_errors = []
  argument_errors << "You must pass an API key!" unless options[:api_key]
  argument_errors << "You must specify recipients!" unless options[:recipients]
  raise(ArgumentError, argument_errors.join(", ")) if argument_errors.size > 0
  @endpoint = ENV['ENDPOINT']  || 'https://sendcat.com'
  @api_key = options[:api_key]
  @recipients = de_dupe_recipients(options[:recipients])
  @message = options[:message] || ""
  @debug = options[:debug] || false
  @success = false
  @errors = []
  @share_id = nil
  @share_id = get_share_id
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def endpoint
  @endpoint
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def errors
  @errors
end

#json_parserObject

Returns the value of attribute json_parser.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def json_parser
  @json_parser
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def message
  @message
end

#recipientsObject

Returns the value of attribute recipients.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def recipients
  @recipients
end

#share_urlObject

Returns the value of attribute share_url.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def share_url
  @share_url
end

#successObject

Returns the value of attribute success.



4
5
6
# File 'lib/sendl/uploader.rb', line 4

def success
  @success
end

Instance Method Details

#debug(msg) ⇒ Object



22
23
24
# File 'lib/sendl/uploader.rb', line 22

def debug(msg)
  @debug && STDERR.puts(msg)
end

#success?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sendl/uploader.rb', line 41

def success?
  errors.size == 0
end

#upload(files, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sendl/uploader.rb', line 26

def upload(files, options = {})
  files.each do |f|
    upload_file(f)
  end
  
  if success?
    debug("success? was true, finalising the share")
    r = Curl::Easy.new(url_for("shares/#{@share_id}/finalise.json")) do |curl|
      curl.http_post Curl::PostField.content("auth_token", api_key)
    end
  else
    STDERR.puts "Could not complete share as there were errors"
  end
end