Class: Yourub::Client

Inherits:
Object
  • Object
show all
Includes:
MetaSearch
Defined in:
lib/yourub/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MetaSearch

#get, #search

Methods included from REST::Videos

list, single_video

Methods included from REST::Categories

for_country

Methods included from REST::Search

list

Constructor Details

#initialize(options = {}) ⇒ Client

The Yourub::Client is a subclass of the Google::APIClient.

In order to initialize the client, you have either to provide a configuration file 'config/yourub.yml' in your main application folder or to pass an hash in the initialization. If you don't provide all the values, default values will be used.The only mandatory value is the developer_key

Examples:

#passing an hash
options = { developer_key: "a_secret_key",
            application_name: "my_app",
            application_version: 2.0,
            log_level: "INFO"}
client = Yourub::Client.new(options)
#assuming that you have a file in config/yourub.yml (see the example in
#the source repository)
client = Yourub::Client.new()

See Also:



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

def initialize(options = {})
  unless options.empty?
    Yourub::Config.override_config_file(options)
  end

  @service = Google::Apis::YoutubeV3::YouTubeService.new
  @service.key = config.developer_key
  @service.client_options.application_name = config.application_name
  @service.client_options.application_version = config.application_version
  @video_categories_cache = {}
end

Instance Attribute Details

#videos ⇒ Object (readonly)

Returns the value of attribute videos.



8
9
10
# File 'lib/yourub/client.rb', line 8

def videos
  @videos
end

Instance Method Details

#config ⇒ Object



89
90
91
# File 'lib/yourub/client.rb', line 89

def config
  Yourub::Config
end

#countries ⇒ Object



44
45
46
# File 'lib/yourub/client.rb', line 44

def countries
  Yourub::Validator.available_countries
end

#execute!(api_method:, parameters:) ⇒ Object

Compatibility shim used by Yourub::REST::Request



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yourub/client.rb', line 67

def execute!(api_method:, parameters:)
  case api_method
  when :search_list
    part = parameters.delete(:part) || parameters.delete('part') || 'snippet'
    opts = normalize_search_params(parameters)
    data = @service.list_searches(part, **opts)
    OpenStruct.new(data: data, status: 200)
  when :videos_list
    part = parameters.delete(:part) || parameters.delete('part') || 'snippet'
    opts = normalize_videos_params(parameters)
    data = @service.list_videos(part, **opts)
    OpenStruct.new(data: data, status: 200)
  when :video_categories_list
    part = parameters.delete(:part) || parameters.delete('part') || 'snippet'
    opts = normalize_categories_params(parameters)
    data = @service.list_video_categories(part, **opts)
    OpenStruct.new(data: data, status: 200)
  else
    raise ArgumentError, "Unsupported api_method #{api_method.inspect}"
  end
end

#flush_video_categories_cache! ⇒ Object



62
63
64
# File 'lib/yourub/client.rb', line 62

def flush_video_categories_cache!
  @video_categories_cache.clear
end

#list_video_categories(region_code: nil, hl: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yourub/client.rb', line 48

def list_video_categories(region_code: nil, hl: nil)
  key = video_categories_cache_key(region_code, hl)
  return @video_categories_cache[key] if @video_categories_cache.key?(key)

  part = "snippet"
  kwargs = {}
  unless region_code.nil? || region_code.to_s.strip.empty?
    kwargs[:region_code] = region_code.to_s.strip.upcase
  end
  kwargs[:hl] = hl.to_s.strip if hl && !hl.to_s.strip.empty?

  @video_categories_cache[key] = @service.list_video_categories(part, **kwargs)
end