Class: LastFM::APIClass

Inherits:
Object
  • Object
show all
Defined in:
lib/lastfm-client/api_class.rb

Direct Known Subclasses

Album, Artist, Auth, Chart, Event, Geo, Group, Library, Playlist, Radio, Tag, Tasteometer, Track, User, Venue

Class Method Summary collapse

Class Method Details

.define_api_method(method, &block) ⇒ Object



51
52
53
54
55
# File 'lib/lastfm-client/api_class.rb', line 51

def self.define_api_method(method, &block)
  (class << self; self; end).instance_eval do
    define_method(method, &block)
  end
end

.read(*args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lastfm-client/api_class.rb', line 21

def self.read(*args)
  args.each do |method|
    define_api_method(method) do |params|
      restricted_method(method, params)
    end
  end
end

.restricted_method(method, params, request_method = :get) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
# File 'lib/lastfm-client/api_class.rb', line 37

def self.restricted_method(method, params, request_method = :get)
  raise ArgumentError, "Params should be a hash" unless params.is_a?(Hash)

  params[:api_sig] = true
  send_request(method, params, request_method)
end

.restricted_methods(*args, &block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/lastfm-client/api_class.rb', line 13

def self.restricted_methods(*args, &block)
  if block_given?
    yield
  else
    read(*args)
  end
end

.send_request(method, params, request_method = :get) ⇒ Object



44
45
46
47
48
49
# File 'lib/lastfm-client/api_class.rb', line 44

def self.send_request(method, params, request_method = :get)
  api_method  = self.to_s.split("::").last + "." # LastFM::Album => Album
  api_method += method.to_s.tr('_', '')          # get_info => getinfo

  LastFM.send_api_request(api_method.downcase, params, request_method)
end

.unrestricted_methods(*args) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/lastfm-client/api_class.rb', line 4

def self.unrestricted_methods(*args)
  args.each do |method|
    define_api_method(method) do |params|
      raise ArgumentError, "Params should be a hash" unless params.is_a?(Hash)
      send_request(method, params)
    end
  end
end

.write(*args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/lastfm-client/api_class.rb', line 29

def self.write(*args)
  args.each do |method|
    define_api_method(method) do |params|
      restricted_method(method, params, :post)
    end
  end
end