Class: SchoolDigger::Api
- Inherits:
-
Object
- Object
- SchoolDigger::Api
- Includes:
- HTTParty
- Defined in:
- lib/school_digger/api.rb
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_key ⇒ Object
readonly
Returns the value of attribute app_key.
Instance Method Summary collapse
-
#autocomplete(query, options = {}) ⇒ Object
# SchoolDigger::Api.new.autocomplete(‘San Die’, st: “CA”).
-
#district(district_id) ⇒ Object
# SchoolDigger::Api.new.district(“0600001”).
-
#district_rankings(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.district_rankings(‘CA’).
-
#districts(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.districts(‘CA’).
- #get(path, query = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Api
constructor
A new instance of Api.
-
#next_page(response) ⇒ Object
response = SchoolDigger::Api.new.districts(‘CA’) next_page_response = SchoolDigger::Api.new.next_page(response).
-
#school(school_id) ⇒ Object
# SchoolDigger::Api.new.school(“490003601072”).
-
#school_rankings(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.school_rankings(‘CA’).
-
#schools(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.schools(‘CA’) # SchoolDigger::Api.new.schools(‘CA’, q: “East High”).
Constructor Details
#initialize(options = {}) ⇒ Api
Returns a new instance of Api.
8 9 10 11 12 |
# File 'lib/school_digger/api.rb', line 8 def initialize( = {}) @app_id = .fetch(:app_id, default_app_id) @app_key = .fetch(:app_key, default_app_key) @api_version = .fetch(:api_version, default_api_version) end |
Instance Attribute Details
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
6 7 8 |
# File 'lib/school_digger/api.rb', line 6 def api_version @api_version end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
6 7 8 |
# File 'lib/school_digger/api.rb', line 6 def app_id @app_id end |
#app_key ⇒ Object (readonly)
Returns the value of attribute app_key.
6 7 8 |
# File 'lib/school_digger/api.rb', line 6 def app_key @app_key end |
Instance Method Details
#autocomplete(query, options = {}) ⇒ Object
# SchoolDigger::Api.new.autocomplete(‘San Die’, st: “CA”)
19 20 21 22 23 24 |
# File 'lib/school_digger/api.rb', line 19 def autocomplete(query, = {} ) = %w(q st level eSE boxLongitudeSE returnCount) = .select {|k,v| .include?(k.to_s)} [:q] = query get "/autocomplete/schools", end |
#district(district_id) ⇒ Object
# SchoolDigger::Api.new.district(“0600001”)
37 38 39 40 41 |
# File 'lib/school_digger/api.rb', line 37 def district(district_id) response = get "/districts/#{district_id}" return "Not Found" if response.code == 404 response end |
#district_rankings(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.district_rankings(‘CA’)
44 45 46 47 48 49 50 |
# File 'lib/school_digger/api.rb', line 44 def district_rankings(state, = {} ) = %w(st year page perPage) = .select {|k,v| .include?(k.to_s)} [:perPage] ||= 50 [:page] ||= 1 get "/rankings/districts/#{state}", end |
#districts(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.districts(‘CA’)
27 28 29 30 31 32 33 34 |
# File 'lib/school_digger/api.rb', line 27 def districts(state, = {} ) = %w(st q city zip nearLatitude nearLongitude boundaryAddress distanceMiles isInBoundaryOnly boxLatitudeNW boxLongitudeNW boxLatitudeSE boxLongitudeSE page perPage sortBy) = .select {|k,v| .include?(k.to_s)} [:st] = state [:perPage] ||= 50 [:page] ||= 1 get "/districts", end |
#get(path, query = {}) ⇒ Object
14 15 16 |
# File 'lib/school_digger/api.rb', line 14 def get(path, query = {}) response = self.class.get(school_digger_url_base + path, query: modify_query(query), timeout: 30) end |
#next_page(response) ⇒ Object
response = SchoolDigger::Api.new.districts(‘CA’) next_page_response = SchoolDigger::Api.new.next_page(response)
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/school_digger/api.rb', line 82 def next_page(response) max_pages = response["numberOfPages"] original_query = response.request.[:query] current_page = original_query[:page] next_page = current_page.to_i + 1 raise "Already at Last Page" if current_page >= max_pages query = original_query.merge({page: next_page}) SchoolDigger::Api.get( response.request.path, query: query, timeout: 30) end |
#school(school_id) ⇒ Object
# SchoolDigger::Api.new.school(“490003601072”)
65 66 67 68 69 |
# File 'lib/school_digger/api.rb', line 65 def school(school_id) response = get "/schools/#{school_id}" return "Not Found" if response.code == 404 response end |
#school_rankings(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.school_rankings(‘CA’)
72 73 74 75 76 77 78 |
# File 'lib/school_digger/api.rb', line 72 def school_rankings(state, = {} ) = %w(st year level page perPage) = .select {|k,v| .include?(k.to_s)} [:perPage] ||= 50 [:page] ||= 1 get "/rankings/schools/#{state}", end |
#schools(state, options = {}) ⇒ Object
# SchoolDigger::Api.new.schools(‘CA’) # SchoolDigger::Api.new.schools(‘CA’, q: “East High”)
55 56 57 58 59 60 61 62 |
# File 'lib/school_digger/api.rb', line 55 def schools(state, = {} ) = %w(st q districtID level city zip isMagnet isCharter isVirtual isTitleI isTitleISchoolwide nearLatitude nearLongitude boundaryAddress distanceMiles isInBoundaryOnly boxLatitudeNW boxLongitudeNW boxLatitudeSE boxLongitudeSE page perPage sortBy) = .select {|k,v| .include?(k.to_s)} [:st] = state [:perPage] ||= 50 [:page] ||= 1 get "/schools", end |