class TubemogulApi::Service

Attributes

connection[R]

Public Class Methods

new(connection) click to toggle source
# File lib/tubemogul_api/service.rb, line 9
def initialize(connection)
  @connection = connection
end

Public Instance Methods

get(id, params = {}) click to toggle source
# File lib/tubemogul_api/service.rb, line 30
def get(id, params = {})
  response = connection.get("#{uri_suffix}/#{id}", params).body

  parse_response(response)
end
get_all(params = {}) click to toggle source
# File lib/tubemogul_api/service.rb, line 36
def get_all(params = {})
  response = connection.get(uri_suffix, params).body

  parse_response(response)
end
name() click to toggle source
# File lib/tubemogul_api/service.rb, line 13
def name
  @name ||= begin
    str = self.class.name.split('::').last
    str.gsub(/(.)([A-Z])/, '\1_\2').downcase
  end
end
parse_collection(response) click to toggle source
# File lib/tubemogul_api/service.rb, line 51
def parse_collection(response)
  Enumerator.new do |y|
    loop do
      response['items'].each do |json|
        y << parse_response(json)
      end
      paging = response['paging']
      break unless paging['has_more_items']
      response = connection.get(paging['next_page_uri']).body
    end
  end
end
parse_response(response) click to toggle source
# File lib/tubemogul_api/service.rb, line 42
def parse_response(response)
  case response['@type']&.downcase
  when 'collection'
    parse_collection(response)
  else
    OpenStruct.new(response)
  end
end
uri_suffix() click to toggle source
# File lib/tubemogul_api/service.rb, line 20
def uri_suffix
  @@endpoints ||= YAML.load_file( # rubocop:disable Style/ClassVars
    TubemogulApi.root.join('lib', 'config', 'endpoints_for_services.yaml')
  )

  endpoint = @@endpoints['service'][name]

  endpoint || raise(TubemogulApi::NotImplemented, "No endpoint for service #{name} available.")
end