Class: Dailymotion::API
- Inherits:
-
Object
- Object
- Dailymotion::API
- Defined in:
- lib/dailymotion/api.rb
Constant Summary collapse
- API_BASE_URL =
'https://api.dailymotion.com'
Instance Attribute Summary collapse
-
#faraday ⇒ Object
readonly
Returns the value of attribute faraday.
-
#faraday_post ⇒ Object
readonly
Returns the value of attribute faraday_post.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #get_connection(object, id, connection, params = {}) ⇒ Object (also: #get_connections)
- #get_object(object, id, params = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ API
constructor
A new instance of API.
- #post_object(object, id, params = {}) ⇒ Object
- #post_video(url) ⇒ Object
- #refresh_token! ⇒ Object
- #set_faradays ⇒ Object
- #upload_file(filepath, url) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ API
Returns a new instance of API.
7 8 9 10 11 |
# File 'lib/dailymotion/api.rb', line 7 def initialize(opts = {}) @options = opts set_faradays end |
Instance Attribute Details
#faraday ⇒ Object (readonly)
Returns the value of attribute faraday.
5 6 7 |
# File 'lib/dailymotion/api.rb', line 5 def faraday @faraday end |
#faraday_post ⇒ Object (readonly)
Returns the value of attribute faraday_post.
5 6 7 |
# File 'lib/dailymotion/api.rb', line 5 def faraday_post @faraday_post end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/dailymotion/api.rb', line 4 def @options end |
Instance Method Details
#get_connection(object, id, connection, params = {}) ⇒ Object Also known as: get_connections
41 42 43 44 45 46 |
# File 'lib/dailymotion/api.rb', line 41 def get_connection(object, id, connection, params = {}) @faraday.get do |req| req.url "/#{object}/#{id}/#{connection}" req.params = params end end |
#get_object(object, id, params = {}) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/dailymotion/api.rb', line 34 def get_object(object, id, params = {}) @faraday.get do |req| req.url "/#{object}/#{id}" req.params = params end end |
#post_object(object, id, params = {}) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/dailymotion/api.rb', line 49 def post_object(object, id, params = {}) @faraday.post do |req| req.url "/#{object}/#{id}" req.params = params end end |
#post_video(url) ⇒ Object
56 57 58 |
# File 'lib/dailymotion/api.rb', line 56 def post_video(url) post_object("me", "videos", :url => url) end |
#refresh_token! ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/dailymotion/api.rb', line 66 def refresh_token! raise StandardError, "client_id, client_secret and refresh_token in options hash are mandatory" unless [:client_id] && [:client_secret] && [:refresh_token] refresh_request = @faraday_post.post "#{API_BASE_URL}/oauth/token", { :grant_type => "refresh_token", :client_id => @options[:client_id], :client_secret => @options[:client_secret], :refresh_token => @options[:refresh_token] } @options[:token] = refresh_request.body.access_token set_faradays end |
#set_faradays ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dailymotion/api.rb', line 13 def set_faradays @faraday = Faraday.new(:url => API_BASE_URL) do |builder| builder.use Dailymotion::FaradayMiddleware::OAuth2, @options[:token] builder.use Faraday::Response::Logger builder.adapter Faraday.default_adapter builder.use ::FaradayMiddleware::Mashify builder.use ::FaradayMiddleware::ParseJson end @faraday_post = Faraday.new do |builder| builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded builder.adapter Faraday.default_adapter builder.use ::FaradayMiddleware::Mashify builder.use ::FaradayMiddleware::ParseJson end end |
#upload_file(filepath, url) ⇒ Object
60 61 62 63 64 |
# File 'lib/dailymotion/api.rb', line 60 def upload_file(filepath, url) payload = { :file => Faraday::UploadIO.new(filepath, "application/octet-stream") } @faraday_post.post url, payload end |