Class: XboxVideo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xbox_video/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client



13
14
15
# File 'lib/xbox_video/client.rb', line 13

def initialize(key)
  @key = key
end

Instance Method Details

#download(video:, path:) ⇒ Object

download a clip. Pass in a video object and a pathname. Returns pathname



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xbox_video/client.rb', line 33

def download(video:,path:)
  path = "#{path}/#{video.game} #{video.title} #{video.clip_id}.mp4"
  File.open(path,'wb') do |save_file|
    open(video.clips.first.uri, 'rb') do |read_file|
      save_file.write(read_file.read)
    end
  end

  path

end

#download_all(videos:, path:) ⇒ Object

pass in an array of video objects to be downloaded



52
53
54
55
56
# File 'lib/xbox_video/client.rb', line 52

def download_all(videos:,path:)
  videos.each do |video|
    download(video: video, path: path)
  end
end

#download_random(path:) ⇒ Object



45
46
47
48
49
# File 'lib/xbox_video/client.rb', line 45

def download_random(path: )
  videos = get_videos
  video = videos.sample
  download(video: video, path: path)
end

#get_videos(game: nil, game_id: nil) ⇒ Object

users can search by specifying the game title or game id



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xbox_video/client.rb', line 18

def get_videos(game: nil, game_id: nil)

  # check if user specified a game_id
  if game_id.nil?
    # get random game if none listed
    game.nil? ? game_id = GAME_LIST[GAME_LIST.keys.sample] : game_id = GAME_LIST[game.downcase.gsub(":",' ')]
  end

  uri = "#{BASE_URL}/public/titles/#{game_id}/clips?qualifier=created&type=userGenerated"
  response = RestClient.get(uri, :'Authorization' => @key, :'contract_version' => 2)
  parsed = JSON.parse(response)
  videos = create_videos(parsed)
end