Class: Siilar::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/siilar/client.rb,
lib/siilar/client/tags.rb,
lib/siilar/client/search.rb,
lib/siilar/client/tracks.rb,
lib/siilar/client/clients.rb
Defined Under Namespace
Modules: Search, Tags, Tracks
Classes: ClientService, SearchService, TagsService, TracksService
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#delete(path, options = {}) ⇒ Object
-
#execute(method, path, data, options = {}) ⇒ Object
-
#get(path, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#patch(path, options = {}) ⇒ Object
-
#post(path, options = {}) ⇒ Object
-
#request(method, path, data, options = {}) ⇒ Object
-
#search ⇒ Object
-
#tags ⇒ Object
-
#tracks ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
9
10
11
12
13
14
15
16
17
|
# File 'lib/siilar/client.rb', line 9
def initialize(options = {})
defaults = Siilar::Default.options
Siilar::Default.keys.each do |key|
instance_variable_set(:"@#{key}", options[key] || defaults[key])
end
@services = {}
end
|
Instance Attribute Details
#api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
7
8
9
|
# File 'lib/siilar/client.rb', line 7
def api_endpoint
@api_endpoint
end
|
#api_key ⇒ Object
Returns the value of attribute api_key.
7
8
9
|
# File 'lib/siilar/client.rb', line 7
def api_key
@api_key
end
|
#requests_timeout ⇒ Object
Returns the value of attribute requests_timeout.
7
8
9
|
# File 'lib/siilar/client.rb', line 7
def requests_timeout
@requests_timeout
end
|
#user_agent ⇒ Object
Returns the value of attribute user_agent.
7
8
9
|
# File 'lib/siilar/client.rb', line 7
def user_agent
@user_agent
end
|
Instance Method Details
#delete(path, options = {}) ⇒ Object
31
32
33
|
# File 'lib/siilar/client.rb', line 31
def delete(path, options = {})
execute :delete, path, options
end
|
#execute(method, path, data, options = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/siilar/client.rb', line 35
def execute(method, path, data, options = {})
response = request(method, path, data, options)
case response.code
when 200..299
response
when 401
raise AuthenticationFailed, response['message']
when 404
raise NotFoundError.new(response)
else
raise RequestError.new(response)
end
end
|
#get(path, options = {}) ⇒ Object
19
20
21
|
# File 'lib/siilar/client.rb', line 19
def get(path, options = {})
execute :get, path, options
end
|
#patch(path, options = {}) ⇒ Object
27
28
29
|
# File 'lib/siilar/client.rb', line 27
def patch(path, options = {})
execute :patch, path, options
end
|
#post(path, options = {}) ⇒ Object
23
24
25
|
# File 'lib/siilar/client.rb', line 23
def post(path, options = {})
execute :post, path, options
end
|
#request(method, path, data, options = {}) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/siilar/client.rb', line 50
def request(method, path, data, options = {})
if data.is_a?(Hash)
options[:query] = data.delete(:query) if data.key?(:query)
options[:headers] = data.delete(:headers) if data.key?(:headers)
end
if !data.empty?
options[:body] = data.to_json
end
HTTParty.send(method, api_endpoint + path, Extra.deep_merge!(base_options, options))
end
|
#search ⇒ Object
8
9
10
|
# File 'lib/siilar/client/clients.rb', line 8
def search
@services[:search] ||= Client::SearchService.new(self)
end
|
12
13
14
|
# File 'lib/siilar/client/clients.rb', line 12
def tags
@services[:tags] ||= Client::TagsService.new(self)
end
|
#tracks ⇒ Object
4
5
6
|
# File 'lib/siilar/client/clients.rb', line 4
def tracks
@services[:tracks] ||= Client::TracksService.new(self)
end
|