Class: Dailymotion::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dailymotion/api.rb

Constant Summary collapse

API_BASE_URL =
'https://api.dailymotion.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#faradayObject (readonly)

Returns the value of attribute faraday.



5
6
7
# File 'lib/dailymotion/api.rb', line 5

def faraday
  @faraday
end

#faraday_postObject (readonly)

Returns the value of attribute faraday_post.



5
6
7
# File 'lib/dailymotion/api.rb', line 5

def faraday_post
  @faraday_post
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/dailymotion/api.rb', line 4

def options
  @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

Raises:

  • (StandardError)


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 options[:client_id] && options[:client_secret] && options[: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_faradaysObject



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