Class: GreatSchools::API

Inherits:
Object
  • Object
show all
Defined in:
lib/great_schools.rb

Overview

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

  • Schools in a city

  • Schools near an address

  • Data about specific schools, including school directory information, grade levels, enrollment, test scores, ratings and reviews and more.

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) ++

Constant Summary collapse

DOMAIN =
'http://api.greatschools.org'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.keyObject

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



50
51
52
# File 'lib/great_schools.rb', line 50

def key
  @key
end

Class Method Details

.get(path, parameters = {}) ⇒ Object

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



63
64
65
66
67
68
69
70
71
72
73
# 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
  else
    raise GreatSchools::Error.new(response)
  end
end