Class: FlickRaw::Flickr
Overview
Root class of the flickr api hierarchy.
Instance Method Summary collapse
-
#call(req, args = {}) ⇒ Object
This is the central method.
-
#initialize ⇒ Flickr
constructor
:nodoc:.
-
#upload_photo(file, args = {}) ⇒ Object
Use this to upload the photo in file.
Methods inherited from Request
build_request, flickr_methods, flickr_objects, request_name
Constructor Details
#initialize ⇒ Flickr
:nodoc:
143 144 145 146 |
# File 'lib/flickraw.rb', line 143 def initialize # :nodoc: super self @token = nil end |
Instance Method Details
#call(req, args = {}) ⇒ Object
This is the central method. It does the actual request to the flickr server.
Raises RestClient::Exception if the response status is failed.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/flickraw.rb', line 151 def call(req, args={}) opts = { :user_agent => "flickraw/#{VERSION}", :accept_encoding => "gzip" } path = REST_PATH + build_args(args, req).collect { |a, v| "#{a}=#{v}" }.join('&') http_response = RestClient.get(FLICKR_HOST + path, opts) parse_response(http_response, req) end |
#upload_photo(file, args = {}) ⇒ Object
Use this to upload the photo in file.
flickr.upload_photo '/path/to/the/photo', :title => 'Title', :description => 'This is the description'
See www.flickr.com/services/api/upload.api.html for more information on the arguments.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/flickraw.rb', line 167 def upload_photo(file, args={}) photo = File.open(file, 'rb') { |f| f.read } boundary = MD5.md5(photo).to_s header = {'Content-type' => "multipart/form-data, boundary=#{boundary} ", 'User-Agent' => "Flickraw/#{VERSION}"} query = '' build_args(args).each { |a, v| query << "--#{boundary}\r\n" << "Content-Disposition: form-data; name=\"#{a}\"\r\n\r\n" << "#{v}\r\n" } query << "--#{boundary}\r\n" << "Content-Disposition: form-data; name=\"photo\"; filename=\"#{file}\"\r\n" << "Content-Transfer-Encoding: binary\r\n" << "Content-Type: image/jpeg\r\n\r\n" << photo << "\r\n" << "--#{boundary}--" http_response = Net::HTTP.start(FLICKR_HOST) { |http| http.post(UPLOAD_PATH, query, header) } xml = http_response.body if xml[/stat="(\w+)"/, 1] == 'fail' msg = xml[/msg="([^"]+)"/, 1] code = xml[/code="([^"]+)"/, 1] raise FailedResponse.new(msg, code, 'flickr.upload') end Response.structify( {:stat => 'ok', :photoid => xml[/<photoid>(\w+)<\/photoid>/, 1], :ticketid => xml[/<ticketid>([^<]+)<\/ticketid>/, 1]}) end |