Class: Bandsintown::Artist

Inherits:
Base
  • Object
show all
Defined in:
lib/bandsintown/artist.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#bandsintown_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

check_for_errors, connection, parse, request, request_and_parse, #to_hash

Constructor Details

#initialize(options = {}) ⇒ Artist

Returns a new instance of Artist.



6
7
8
9
10
# File 'lib/bandsintown/artist.rb', line 6

def initialize(options = {})
  @name = options[:name]
  @mbid = options[:mbid]
  @bandsintown_url = options[:url] || build_bandsintown_url
end

Instance Attribute Details

#eventsObject

Returns an array of Bandsintown::Event objects for each of the artist’s upcoming events available through bandsintown.com. See www.bandsintown.com/api/requests#artists-events for more information. Can be used with either artist name or mbid (music brainz id).

example:

# using artist name
artist = Bandsintown::Artist.new(:name => "Little Brother")
upcoming_little_brother_events = artist.events

# using mbid for Little Brother
artist = Bandsintown::Artist.new(:mbid => "b929c0c9-5de0-4d87-8eb9-365ad1725629")
upcoming_little_brother_events = artist.events


24
25
26
# File 'lib/bandsintown/artist.rb', line 24

def events
  @events
end

#mbidObject

Returns the value of attribute mbid.



4
5
6
# File 'lib/bandsintown/artist.rb', line 4

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/bandsintown/artist.rb', line 4

def name
  @name
end

#upcoming_events_countObject

Returns the value of attribute upcoming_events_count.



4
5
6
# File 'lib/bandsintown/artist.rb', line 4

def upcoming_events_count
  @upcoming_events_count
end

Class Method Details

.build_from_json(json_hash) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/bandsintown/artist.rb', line 76

def self.build_from_json(json_hash)
  returning Bandsintown::Artist.new({}) do |artist|
    artist.name = json_hash['name']
    artist.mbid = json_hash['mbid']
    artist.bandsintown_url = json_hash['url']
    artist.upcoming_events_count = json_hash['upcoming_events_count']
  end
end

.get(options = {}) ⇒ Object

Returns a Bandsintown::Artist object with basic information for a single artist, including the number of upcoming events. Useful in determining if an artist is on tour without requesting the event data. See www.bandsintown.com/api/requests#artists-get for more information. Can be used with either artist name or mbid (music brainz id).

example:

# using artist name
artist = Bandsintown::Artist.get(:name => "Little Brother")

# using mbid for Little Brother
artist = Bandsintown::Artist.get(:mbid => "b929c0c9-5de0-4d87-8eb9-365ad1725629")


71
72
73
74
# File 'lib/bandsintown/artist.rb', line 71

def self.get(options = {})
  request_url = Bandsintown::Artist.new(options).api_name
  build_from_json(request_and_parse(:get, request_url))
end

.resource_pathObject



85
86
87
# File 'lib/bandsintown/artist.rb', line 85

def self.resource_path
  "artists"
end

Instance Method Details

#api_nameObject

Used in api requests as the RESTful resource id for artists (api.bandsintown.com/artists/id/method). If @name is not nil, it will be URI escaped to generate the api_name. ‘/’ and ‘?’ must be double escaped. If @name is nil, @mbid is used with ‘mbid_’ prefixed.



33
34
35
36
37
38
39
40
41
42
# File 'lib/bandsintown/artist.rb', line 33

def api_name
  if @name
    name = @name.dup
    name.gsub!('/', CGI.escape('/'))
    name.gsub!('?', CGI.escape('?'))
    URI.escape(name)
  else
    "mbid_#{@mbid}"
  end
end

#on_tour?Boolean

Returns true if there is at least 1 upcoming event for the artist, or false if there are 0 upcoming events for the artist. Should only be used for artists requested using Bandsintown::Artist.get, or with events already loaded, otherwise an error will be raised.

example:

# using .get method
artist = Bandsintown::Artist.get(:name => "Little Brother")
artist_on_tour = artist.on_tour?

# using .initialize and .events methods
artist = Bandsintown::Artist.get(:name => "Little Brother")
events = artist.events
artist_on_tour = artist.on_tour?

Returns:

  • (Boolean)


56
57
58
# File 'lib/bandsintown/artist.rb', line 56

def on_tour?
  (@upcoming_events_count || @events.size) > 0
end