Class: Freesound::Client

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

Defined Under Namespace

Modules: Response

Instance Method Summary collapse

Constructor Details

#initialize(api_key: Freesound.api_key, api_url: Freesound.api_url) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
# 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

Instance Method Details

#combined_search(**params) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/freesound/client.rb', line 39

def combined_search(**params)
  path = "#{@base_path}/search/combined/"

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

#content_search(q, **params) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/freesound/client.rb', line 23

def content_search(q, **params)
  path = "#{@base_path}/search/content/"

  request(:get, path, params: {query: q, **params}) do |res|
    Response::Search.new(res, @http)
  end
end

#pack(id, **params) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



47
48
49
50
51
52
53
# 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

#text_search(q, **params) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/freesound/client.rb', line 15

def text_search(q, **params)
  path = "#{@base_path}/search/text/"

  request(:get, path, params: {query: q, **params}) do |res|
    Response::Search.new(res, @http)
  end
end

#user(username, **params) ⇒ Object



55
56
57
58
59
60
61
# 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