Class: SchoolDigger::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/school_digger/api.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete(query, options = {}) ⇒ Object

# SchoolDigger::Api.new.autocomplete(‘San Die’, st: “CA”)



11
12
13
14
15
16
# File 'lib/school_digger/api.rb', line 11

def autocomplete(query, options = {} )
  available_options = %w(q st level boxLatitudeNW boxLongitudeNW boxLatitudeSE boxLongitudeSE returnCount)
  options = options.select {|k,v| available_options.include?(k.to_s)}
  options[:q] = query
  get "/autocomplete/schools", options
end

#district(district_id) ⇒ Object

# SchoolDigger::Api.new.district(“0600001”)



29
30
31
32
33
# File 'lib/school_digger/api.rb', line 29

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



36
37
38
39
40
41
42
# File 'lib/school_digger/api.rb', line 36

def district_rankings(state, options = {} )
  available_options = %w(st year page perPage)
  options = options.select {|k,v| available_options.include?(k.to_s)}
  options[:perPage] ||= 50
  options[:page] ||= 1
  get "/rankings/districts/#{state}", options
end

#districts(state, options = {}) ⇒ Object

# SchoolDigger::Api.new.districts(‘CA’)



19
20
21
22
23
24
25
26
# File 'lib/school_digger/api.rb', line 19

def districts(state, options = {} )
  available_options = %w(st q city zip nearLatitude nearLongitude boundaryAddress distanceMiles isInBoundaryOnly boxLatitudeNW boxLongitudeNW boxLatitudeSE boxLongitudeSE page perPage sortBy)
  options = options.select {|k,v| available_options.include?(k.to_s)}
  options[:st] = state
  options[:perPage] ||= 50
  options[:page] ||= 1
  get "/districts", options
end

#get(path, query = {}) ⇒ Object



6
7
8
# File 'lib/school_digger/api.rb', line 6

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)



74
75
76
77
78
79
80
81
82
83
# File 'lib/school_digger/api.rb', line 74

def next_page(response)
  max_pages = response["numberOfPages"]
  original_query  = response.request.options[: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”)



57
58
59
60
61
# File 'lib/school_digger/api.rb', line 57

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



64
65
66
67
68
69
70
# File 'lib/school_digger/api.rb', line 64

def school_rankings(state, options = {} )
  available_options = %w(st year level page perPage)
  options = options.select {|k,v| available_options.include?(k.to_s)}
  options[:perPage] ||= 50
  options[:page] ||= 1
  get "/rankings/schools/#{state}", options
end

#schools(state, options = {}) ⇒ Object

# SchoolDigger::Api.new.schools(‘CA’) # SchoolDigger::Api.new.schools(‘CA’, q: “East High”)



47
48
49
50
51
52
53
54
# File 'lib/school_digger/api.rb', line 47

def schools(state, options = {} )
  available_options = %w(st q districtID level city zip isMagnet isCharter isVirtual isTitleI isTitleISchoolwide nearLatitude nearLongitude boundaryAddress distanceMiles isInBoundaryOnly boxLatitudeNW boxLongitudeNW boxLatitudeSE boxLongitudeSE page perPage sortBy)
  options = options.select {|k,v| available_options.include?(k.to_s)}
  options[:st] = state
  options[:perPage] ||= 50
  options[:page] ||= 1
  get "/schools", options
end