Class: Ikbis::Advanced::Upload
- Defined in:
- lib/ikbis/advanced/upload.rb
Defined Under Namespace
Classes: CouldNotAuthenticate, CouldNotUpload, InvalidMediaType, InvalidUploadFile
Constant Summary collapse
- IMAGE =
Sends a multipart/form POST request to the server containing all the required fields for uploading media.
It made no sense for this method to follow its siblings behaviour by delegating a send request to Ikbis::Advanced::Base module with the req params, like the other modules are doing since this request is handled by Net::HTTP and NOT HTTParty.
The uploaded file is being parsed into a multi-part/form compatible request object by the gem ‘multipart-post’ by Nick Sieger, cheers to him for making this an easy task.
Params:
* string file_path => must either be a full path or a relative one to the upload file * hash options { * string :title => media title * string :caption => media caption, defaults to title if not specified * string enum :type => !"media"|"image"|"video" * string :tags => space delimited media tag(s) * string enum :permission => !"public"|"private"|"shared" } `!' implies default value note: ALL parameters are REQUIRED
Usage ex:
uploader = Ikbis::Advanced::Uploader.new # must create an object since OAuth is required uploader.oauth_authentication("ikbis.yml") uploader.upload( "http://www.mydomain.com/foo.png", { :title => "FooBAR", :caption => "FooBAR Cap'n", :tags => "hello world", :permission => "public" })
"image"
- VIDEO =
"video"
- MEDIA =
"media"
- @@image_types =
[ "jpg", "jpeg", "png", "svg", "gif", "bmp" ]
- @@video_types =
[ "flv", "wmv", "avi", "mpeg", "3gp", "3g2", "asf", "swf", "mpg", "rm", "3gpp", "mp4", "asp", "mov" ]
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize ⇒ Upload
constructor
Set the class extention string for this class.
- #uploadFile(file_path, options) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ Upload
Set the class extention string for this class.
22 23 24 25 |
# File 'lib/ikbis/advanced/upload.rb', line 22 def initialize @class_extention = "ikbis.upload" super ### call the media initialize function. end |
Instance Method Details
#uploadFile(file_path, options) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ikbis/advanced/upload.rb', line 76 def uploadFile(file_path, ) puts "|------------------------ Uploading ------------------------|" begin # open file for uploading begin file = File.new(file_path) rescue raise InvalidUploadFile end url = URI.parse('http://localhost:3000/api/medias') puts "| Initiating POST request to `#{url.request_uri}'" req = Net::HTTP::Post.new(url.request_uri) puts "| Constructing File object" req = Net::HTTP::Post::Multipart.new url.path, { "file" => UploadIO.new(file, mime_type(file), file.path), "title" => [:title], "caption" => [:caption], "type" => type(file), "tags" => [:tags], "permission" => [:permission] } puts "| Authenticating..." begin @consumer.sign!(req, @access_token) rescue raise CouldNotAuthenticate end puts "| Initiating connection with `#{url.host}:#{url.port}'" res = "" Net::HTTP.new(url.host, url.port).start do |http| print "| Processing request..." res = http.request(req) print " done!\n" end puts "| Upload request sent successfully:" puts "| Request URI -> #{url.request_uri}" puts "| Response -> #{res}" rescue InvalidMediaType print "| File format not supported. Supported types: " print "\n| Images => " print @@image_types.join(", ") print "\n| Videos => " print @@video_types.join(", ") print "\n" rescue CouldNotAuthenticate puts "| Error while authenticating, have you run oauth_authenticate() ?" rescue InvalidUploadFile puts "| An error occured processing the file you input, please make sure the file is valid." rescue puts "| Upload failed with `#{$!}'\n" raise CouldNotUpload end puts "|------------------------ --------- ------------------------|" end |