Class: DailymotionApi::Client

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

Constant Summary collapse

API_URL =
"https://api.dailymotion.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client



9
10
11
12
13
14
# File 'lib/dailymotion-api/client.rb', line 9

def initialize(params = {})
  @username = params[:username]
  @password = params[:password]
  @api_key = params[:api_key]
  @api_secret = params[:api_secret]
end

Instance Attribute Details

#video_idObject (readonly)

Returns the value of attribute video_id.



7
8
9
# File 'lib/dailymotion-api/client.rb', line 7

def video_id
  @video_id
end

Instance Method Details

#create_videoObject



36
37
38
39
# File 'lib/dailymotion-api/client.rb', line 36

def create_video
  response = HTTMultiParty.post("#{API_URL}/me/videos", body: { access_token: @access_token, url: @uploaded_video_url })
  @video_id = response.parsed_response["id"]
end

#delete_video(video_id) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/dailymotion-api/client.rb', line 79

def delete_video(video_id)
  # It's required to get an access token with manage_videos scope
  return nil unless video_id

  response = HTTMultiParty.delete("#{API_URL}/me/videos/#{video_id}", headers: { "Authorization" => "Bearer #{@access_token}" })
  response.parsed_response
end

#generate_upload_urlObject



26
27
28
29
# File 'lib/dailymotion-api/client.rb', line 26

def generate_upload_url
  response = HTTMultiParty.get("#{API_URL}/file/upload?access_token=#{@access_token}")
  @upload_url = response.parsed_response["upload_url"]
end

#get_authenticated_user_info(fields = "") ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/dailymotion-api/client.rb', line 69

def (fields = "")
  if fields.empty?
    response = HTTMultiParty.get("#{API_URL}/me/", headers: { "Authorization" => "Bearer #{@access_token}" })
  else
    response = HTTMultiParty.get("#{API_URL}/me?fields=#{fields}", headers: { "Authorization" => "Bearer #{@access_token}" })
  end

  response.parsed_response
end

#get_authenticated_user_videos(fields = "") ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/dailymotion-api/client.rb', line 59

def get_authenticated_user_videos(fields = "")
  if fields.empty?
    response = HTTMultiParty.get("#{API_URL}/me/videos", headers: { "Authorization" => "Bearer #{@access_token}" })
  else
    response = HTTMultiParty.get("#{API_URL}/me/videos?fields=#{fields}", headers: { "Authorization" => "Bearer #{@access_token}" })
  end

  response.parsed_response
end

#get_video(video_id, fields = "") ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/dailymotion-api/client.rb', line 45

def get_video(video_id, fields = "")
  return nil unless video_id

  url = "#{API_URL}/video/#{video_id}"
  url << "?fields=#{fields}" unless fields.empty?

  response = HTTMultiParty.get(url)
  response.parsed_response
end

#post_video(video) ⇒ Object



31
32
33
34
# File 'lib/dailymotion-api/client.rb', line 31

def post_video(video)
  response = HTTMultiParty.post(@upload_url, body: { file: video })
  @uploaded_video_url = JSON.parse(response.parsed_response)["url"]
end

#publish_video(metadata) ⇒ Object



41
42
43
# File 'lib/dailymotion-api/client.rb', line 41

def publish_video()
  HTTMultiParty.post("#{API_URL}/video/#{@video_id}", body: .merge(access_token: @access_token, published: true))
end

#request_access_tokenObject



16
17
18
19
# File 'lib/dailymotion-api/client.rb', line 16

def request_access_token
  response = HTTMultiParty.post("#{API_URL}/oauth/token", body: request_access_token_params)
  @access_token = response.parsed_response["access_token"]
end

#request_access_token_manage_videos_scopeObject



21
22
23
24
# File 'lib/dailymotion-api/client.rb', line 21

def request_access_token_manage_videos_scope
  response = HTTMultiParty.post("#{API_URL}/oauth/token", body: request_access_token_params.merge(scope: "manage_videos"))
  @access_token = response.parsed_response["access_token"]
end

#video_urlObject



55
56
57
# File 'lib/dailymotion-api/client.rb', line 55

def video_url
  @video_url ||= get_video(@video_id, "url")["url"] rescue nil
end