class Freesound::Client

Public Class Methods

new(api_key: Freesound.api_key, api_url: Freesound.api_url) { |http| ... } click to toggle source
# File lib/freesound/client.rb, line 5
def initialize(api_key: Freesound.api_key, api_url: Freesound.api_url)
  uri = URI.parse(api_url.to_s)
  url, @base_path = uri.merge("/"), uri.request_uri

  @http = HTTP
    .persistent(url)
    .auth("Token #{api_key}")
    .tap { |http| yield(http) if block_given? }
end

Public Instance Methods

pack(id, **params) click to toggle source
# File lib/freesound/client.rb, line 63
def pack(id, **params)
  path = "#{@base_path}/packs/#{id}/"

  request(:get, path, params: params) do |res|
    Response::Pack.new(res, @http)
  end
end
request(http_method, *args, **options) { |response| ... } click to toggle source
# File lib/freesound/client.rb, line 71
def request(http_method, *args, **options)
  response = @http.public_send(http_method.to_s.downcase, *args, **options)

  block_given? ? yield(response) : response
end
sound(id, **params) click to toggle source
# File lib/freesound/client.rb, line 47
def sound(id, **params)
  path = "#{@base_path}/sounds/#{id}/"

  request(:get, path, params: params) do |res|
    Response::Sound.new(res, @http)
  end
end
user(username, **params) click to toggle source
# File lib/freesound/client.rb, line 55
def user(username, **params)
  path = "#{@base_path}/users/#{username}/"

  request(:get, path, params: params) do |res|
    Response::User.new(res, @http)
  end
end