Class: Avocado::Uploader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/avocado/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUploader

Returns a new instance of Uploader.



7
8
9
# File 'lib/avocado/uploader.rb', line 7

def initialize
  @payload = []
end

Instance Attribute Details

#payloadObject

Returns the value of attribute payload.



5
6
7
# File 'lib/avocado/uploader.rb', line 5

def payload
  @payload
end

Instance Method Details

#upload!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/avocado/uploader.rb', line 11

def upload!
  return unless should_upload?
  WebMock.allow_net_connect! if defined? WebMock
  write_payload_to_json_file do |file|
    request = Net::HTTP::Post::Multipart.new uri.path, 'file' => UploadIO.new(file, 'text/json', 'avocado.json')
    net_request = Net::HTTP.new(uri.host, uri.port)
    net_request.use_ssl = (uri.scheme == 'https')
    response = net_request.start { |http| http.request(request) }

    if response.is_a?(Net::HTTPRedirection)
      raise "Avocado was redirected to '#{response['location']}', update your initializer!"
    else
      response
    end
  end
ensure
  WebMock.disable_net_connect! if defined? WebMock
end