class NbaStats::Client

Public Class Methods

new(options={}) click to toggle source

Initialize a new Client object

@param options [Hash] @return [NbaStats::Client]

# File lib/nba_stats/client.rb, line 78
def initialize(options={})
  # Merge the config values from the module and those passed
  # to the client.
  merged_options = NbaStats.options.merge(options)

  # Copy the merged values to this client and ignore those
  # not part of our configuration
  Configuration::VALID_CONFIG_KEYS.each do |key|
    send("#{key}=", merged_options[key])
  end
end

Public Instance Methods

get(path='/', params={}) click to toggle source

Perform a HTTP GET request

@param path [String] @param params [Hash] @return [Hash]

# File lib/nba_stats/client.rb, line 103
def get(path='/', params={})
  uri = Addressable::URI.new
  uri.query_values = params
  # Build the path with + instead of %20 because nba.com is flaky
  full_path = "#{path}?#{uri.query.gsub(/%20/,'+')}"
  request(:get, full_path)
end
request_headers() click to toggle source

@return [Hash]

# File lib/nba_stats/client.rb, line 91
def request_headers
  @request_headers ||= {
      :accept => accept,
      :user_agent => user_agent
  }
end

Private Instance Methods

request(method, path) click to toggle source

Send the HTTP request

@param method [String] @param path [String] @return [RestClient::Response]

# File lib/nba_stats/client.rb, line 123
def request(method, path)
  resource[path].send(method.to_sym, request_headers) { |response, request, result, &block|
    case response.code
      when 200
        response
      when 400
        if response.include? ' is required'
          raise ArgumentError.new(response)
        else
          raise BadRequestError.new(response)
        end
      else
        response.return!(request, result, &block)
    end
  }
end
resource() click to toggle source

@return [RestClient::Resource]

# File lib/nba_stats/client.rb, line 114
def resource
  @resource ||= RestClient::Resource.new(endpoint)
end