class GreatSchools::API

The GreatSchools API allows you to add valuable information to your application, including:

Before you can start using the GreatSchool API, you must register and request an access key at: www.greatschools.org/api/registration.page – TODO: load API access key from a config file (great_schools.yml or something) ++

Constants

DOMAIN

Attributes

key[RW]

The API access key, must be set before making any requests.

Public Class Methods

get(path, parameters = {}) click to toggle source

Makes an API request to path with the supplied query parameters (merges in the API access key).

Returns a Hash or an Array with the encompassing XML container stripped.

Raises a GreatSchools::Error if the server response code is not 200.

Attributes

  • path - component path of the URL

  • parameters - Hash of query string elements

# File lib/great_schools.rb, line 63
def get(path, parameters = {})
  parameters.merge!(key: key).keep_if { |_,v| v.present? }

  response = HTTParty.get("#{DOMAIN}/#{path}", query: parameters, format: :xml)

  if response.code.eql?(200)
    parse(response.values.first) # strip the container element before parsing
  elsif response.body.blank?
    nil # no error to parse, return nothing
  else
    raise GreatSchools::Error.new(response)
  end
end

Private Class Methods

parse(hash) click to toggle source

Returns a Hash of a single element, or an Array of elements without the container hash.

# File lib/great_schools.rb, line 81
def parse(hash)
  if hash && hash.keys.size.eql?(1) # we have an array of elements
    hash.values.first # strip the container and return the array
  else # we have one element, return the hash
    hash
  end
end