module PhishDotNetClient

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.

Constants

API_METHODS

The possible API methods. Generated from +rake parse_method_docs+.

BASE_URL

The base URL for API calls

DEFAULT_PARAMS

Default API parameters

VERSION

Gem version

Public Instance Methods

apikey=(private_api_key) click to toggle source

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

@param private_api_key [String] the apikey @return [void]

# 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) click to toggle source

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.

@param username [String] the username @param password [String] the password

@return [void]

# 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={}) click to toggle source

@api private

Calls the specified Phish.net api method.

@param api_method [String] the method to call @param params [Hash] the url parameters for the api call

@raise [RuntimeError] if the http response status is a 2xx

@return [Hash, Array] the parsed JSON of API response

# 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_auth() click to toggle source

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

@return [void]

# 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) click to toggle source

@api private @param rb_method_name [Symbol] the Ruby method name @return [String] the api method name

# 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
method_missing(name, *args) click to toggle source

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

@api private

Calls superclass method
# 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