Module: PhishDotNetClient

Extended by:
PhishDotNetClient
Included in:
PhishDotNetClient
Defined in:
lib/phish_dot_net_client.rb,
lib/phish_dot_net_client/set.rb,
lib/phish_dot_net_client/song.rb,
lib/phish_dot_net_client/setlist.rb,
lib/phish_dot_net_client/version.rb,
lib/phish_dot_net_client/song_transition.rb

Overview

This module encapsulates interaction with the Phish.net API. It allows you to call any API method and will parse “setlistdata” fields in the JSON responses.

Defined Under Namespace

Classes: Set, Setlist, Song, SongTransition

Constant Summary collapse

API_METHODS =

The possible API methods. Generated from rake parse_method_docs.

{
  "pnet.api.authkey.get"        => { :scope => "protected" },
  "pnet.api.authorize"          => { :scope => "protected" },
  "pnet.api.authorized.check"   => { :scope => "protected" },
  "pnet.api.isAuthorized"       => { :scope => "protected" },
  "pnet.artists.get"            => { :scope => "public" },
  "pnet.blog.get"               => { :scope => "public" },
  "pnet.blog.item.get"          => { :scope => "public" },
  "pnet.collections.get"        => { :scope => "protected" },
  "pnet.collections.query"      => { :scope => "protected" },
  "pnet.forum.canpost"          => { :scope => "protected" },
  "pnet.forum.get"              => { :scope => "public" },
  "pnet.forum.thread.get"       => { :scope => "protected" },
  "pnet.forum.thread.new"       => { :scope => "protected" },
  "pnet.forum.thread.respond"   => { :scope => "protected" },
  "pnet.news.comments.get"      => { :scope => "public" },
  "pnet.news.get"               => { :scope => "public" },
  "pnet.reviews.query"          => { :scope => "protected" },
  "pnet.reviews.recent"         => { :scope => "public" },
  "pnet.shows.links.get"        => { :scope => "protected" },
  "pnet.shows.query"            => { :scope => "protected" },
  "pnet.shows.setlists.get"     => { :scope => "protected" },
  "pnet.shows.setlists.latest"  => { :scope => "public" },
  "pnet.shows.setlists.random"  => { :scope => "public" },
  "pnet.shows.setlists.recent"  => { :scope => "public" },
  "pnet.shows.setlists.tiph"    => { :scope => "public" },
  "pnet.shows.upcoming"         => { :scope => "public" },
  "pnet.user.get"               => { :scope => "protected" },
  "pnet.user.myshows.add"       => { :scope => "protected" },
  "pnet.user.myshows.get"       => { :scope => "protected" },
  "pnet.user.myshows.remove"    => { :scope => "protected" },
  "pnet.user.register"          => { :scope => "protected" },
  "pnet.user.shows.rate"        => { :scope => "protected" },
  "pnet.user.uid.get"           => { :scope => "protected" },
  "pnet.user.username.check"    => { :scope => "protected" }
}
BASE_URL =

The base URL for API calls

"https://api.phish.net/api.js"
DEFAULT_PARAMS =

Default API parameters

{ api: "2.0",
format: "json" }
VERSION =

Gem version

'0.1.2'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override method_missing to provide mapping of Ruby methods to API method names.



135
136
137
138
139
140
141
142
143
# File 'lib/phish_dot_net_client.rb', line 135

def method_missing(name, *args)
  api_method = get_api_method(name)

  if api_method
    call_api_method(api_method, *args)
  else
    super(name, *args)
  end
end

Instance Method Details

#apikey=(private_api_key) ⇒ void

This method returns an undefined value.

Set the apikey. The “private api key” from your Phish.net account should be used.

Parameters:

  • private_api_key (String)

    the apikey



71
72
73
# File 'lib/phish_dot_net_client.rb', line 71

def apikey=(private_api_key)
  DEFAULT_PARAMS.merge!(:apikey => private_api_key)
end

#authorize(username, password) ⇒ void

This method returns an undefined value.

Calls pnet.api.authorize with the specified username and password, then stores the username and returned authkey if the call was successful. The password is not stored.

Parameters:

  • username (String)

    the username

  • password (String)

    the password



83
84
85
86
87
88
89
# File 'lib/phish_dot_net_client.rb', line 83

def authorize(username, password)
  resp = call_api_method("pnet.api.authorize", :username => username, :passwd => password)

  if resp['success'] == 1 && resp.has_key?('authkey')
    DEFAULT_PARAMS.merge!(:username => username, :authkey => resp['authkey'])
  end
end

#call_api_method(api_method, params = {}) ⇒ Hash, Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the specified Phish.net api method.

Parameters:

  • api_method (String)

    the method to call

  • params (Hash) (defaults to: {})

    the url parameters for the api call

Returns:

  • (Hash, Array)

    the parsed JSON of API response

Raises:

  • (RuntimeError)

    if the http response status is a 2xx



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/phish_dot_net_client.rb', line 108

def call_api_method(api_method, params={})
  # method_data = API_METHODS[api_method]
  # ensure_api_key if method_data[:scope] == "protected"

  params.merge!(:method => api_method)
  response = RestClient.get BASE_URL, { :params => DEFAULT_PARAMS.merge(params) }

  if response.code < 200 || response.code > 299
    raise "non 2xx reponse: status=#{response.code}"
  end

  parsed = Oj.load(response)

  if parsed.is_a?(Array)
    parsed.each do |obj|
      obj["setlistdata"] = Setlist.new(obj["setlistdata"]) if obj.has_key?("setlistdata")
    end
  elsif parsed.is_a?(Hash)
    parsed["setlistdata"] = Setlist.new(parsed["setlistdata"]) if parsed.has_key?("setlistdata")
  end

  return parsed
end

#clear_authvoid

This method returns an undefined value.

Clears the stored API authentication parameters (apikey, username, authkey)



94
95
96
# File 'lib/phish_dot_net_client.rb', line 94

def clear_auth
  [:apikey, :username, :authkey].each { |key| DEFAULT_PARAMS.delete(key) }
end

#get_api_method(rb_method_name) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the api method name.

Parameters:

  • rb_method_name (Symbol)

    the Ruby method name

Returns:

  • (String)

    the api method name



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/phish_dot_net_client.rb', line 148

def get_api_method(rb_method_name)
  api_method_name = rb_method_name.to_s.gsub("_", ".")

  unless api_method_name.match(/\Apnet\./)
    api_method_name = 'pnet.' + api_method_name
  end

  return api_method_name

  # if API_METHODS.has_key?(api_method_name)
  #   return api_method_name
  # else
  #   return nil
  # end
end