Class: Siilar::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/siilar/client.rb,
lib/siilar/client/tags.rb,
lib/siilar/client/users.rb,
lib/siilar/client/radios.rb,
lib/siilar/client/search.rb,
lib/siilar/client/tracks.rb,
lib/siilar/client/clients.rb

Defined Under Namespace

Modules: Radios, Search, Tags, Tracks, Users Classes: ClientService, RadiosService, SearchService, TagsService, TracksService, UsersService

Instance Attribute Summary collapse

Instance Method Summary collapse

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_endpointObject

Returns the value of attribute api_endpoint.



7
8
9
# File 'lib/siilar/client.rb', line 7

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/siilar/client.rb', line 7

def api_key
  @api_key
end

#requests_timeoutObject

Returns the value of attribute requests_timeout.



7
8
9
# File 'lib/siilar/client.rb', line 7

def requests_timeout
  @requests_timeout
end

#user_agentObject

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

#radiosObject



4
5
6
# File 'lib/siilar/client/clients.rb', line 4

def radios
  @services[:radios] ||= Client::RadiosService.new(self)
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

#searchObject



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

def search
  @services[:search] ||= Client::SearchService.new(self)
end

#tagsObject



12
13
14
# File 'lib/siilar/client/clients.rb', line 12

def tags
  @services[:tags] ||= Client::TagsService.new(self)
end

#tracksObject



16
17
18
# File 'lib/siilar/client/clients.rb', line 16

def tracks
  @services[:tracks] ||= Client::TracksService.new(self)
end

#usersObject



20
21
22
# File 'lib/siilar/client/clients.rb', line 20

def users
  @services[:users] ||= Client::UsersService.new(self)
end