Class: FileTurn::Upload

Inherits:
Resource show all
Defined in:
lib/fileturn/resources/upload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

conn

Constructor Details

#initialize(params) ⇒ Upload

Returns a new instance of Upload.



23
24
25
# File 'lib/fileturn/resources/upload.rb', line 23

def initialize(params)
  @params = RecursiveOpenStruct.new(params, recurse_over_arrays: true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



27
28
29
# File 'lib/fileturn/resources/upload.rb', line 27

def method_missing(m, *args, &block)
  @params.send(m)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



21
22
23
# File 'lib/fileturn/resources/upload.rb', line 21

def params
  @params
end

Class Method Details

.find(id, &block) ⇒ Object



6
7
8
9
10
# File 'lib/fileturn/resources/upload.rb', line 6

def find(id, &block)
  conn.get("/uploads/#{id}.json", {}, 200) do |params|
    FileTurn::Upload.new(params)
  end
end

.process!(file) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/fileturn/resources/upload.rb', line 12

def process!(file)
  conn.post('/uploads.json', { file_name: File.basename(file.path) }, 200) do |params|
    upload = FileTurn::Upload.new(params)
    upload.upload_file!(file)
    upload.reload!
  end
end

Instance Method Details

#reload!Object



31
32
33
34
# File 'lib/fileturn/resources/upload.rb', line 31

def reload!
  @params = FileTurn::Upload.find(id).params
  self
end

#upload_file!(file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fileturn/resources/upload.rb', line 36

def upload_file!(file)
  faraday = Faraday.new(:url => url) do |conn|
    conn.request :multipart
    conn.adapter :net_http
  end

  response = faraday.post '/', fields.to_h.merge(file: Faraday::UploadIO.new(file.path, ''))

  if response.status != 204
    raise FileTurn::BadRequestError.new(response.body)
  end
end