Class: FfApi::V1
- Inherits:
-
Object
- Object
- FfApi::V1
- Defined in:
- lib/ff_api.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #base_url ⇒ Object
- #districts(params = {}) ⇒ Object
- #districts_as_json(params = {}) ⇒ Object
-
#initialize(api_key:) ⇒ V1
constructor
A new instance of V1.
- #respond_to_error(response) ⇒ Object
- #schools(params = {}) ⇒ Object
- #schools_as_json(params = {}) ⇒ Object
Constructor Details
#initialize(api_key:) ⇒ V1
Returns a new instance of V1.
11 12 13 |
# File 'lib/ff_api.rb', line 11 def initialize(api_key:) self.api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/ff_api.rb', line 9 def api_key @api_key end |
Instance Method Details
#base_url ⇒ Object
15 16 17 |
# File 'lib/ff_api.rb', line 15 def base_url "http://dev-oh.finalforms.me:8080/api/v1" end |
#districts(params = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ff_api.rb', line 54 def districts(params = {}) connection = Faraday.new(url: base_url) if params.is_a?(Hash) json = districts_as_json(params) districts = json["results"] json["results"] = districts.map { |district_hash| FfApi::District.new(district_hash) } json else FfApi::District.new(districts_as_json) end end |
#districts_as_json(params = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ff_api.rb', line 66 def districts_as_json(params = {}) connection = Faraday.new(url: base_url) if params.is_a?(Hash) response = connection.get("districts.json", params) do |request| request.headers["Authorization"] = "Bearer #{api_key}" end if response.status == 200 JSON.parse(response.body) else respond_to_error(response) end else response = connection.get("district/#{params}.json") do |request| request.headers["Authorization"] = "Bearer #{api_key}" end if response.status == 200 JSON.parse(response.body) else respond_to_error(response) end end end |
#respond_to_error(response) ⇒ Object
89 90 91 92 |
# File 'lib/ff_api.rb', line 89 def respond_to_error(response) body = JSON.parse(response.body) body.merge(status: response.status) end |
#schools(params = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ff_api.rb', line 19 def schools(params = {}) connection = Faraday.new(url: base_url) if params.is_a?(Hash) json = schools_as_json(params) schools = json["results"] json["results"] = schools.map { |school_hash| FfApi::School.new(school_hash) } json else FfApi::School.new(schools_as_json) end end |
#schools_as_json(params = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ff_api.rb', line 31 def schools_as_json(params = {}) connection = Faraday.new(url: base_url) if params.is_a?(Hash) response = connection.get("schools.json", params) do |request| request.headers["Authorization"] = "Bearer #{api_key}" end if response.status == 200 JSON.parse(response.body) else respond_to_error(response) end else response = connection.get("school/#{params}.json") do |request| request.headers["Authorization"] = "Bearer #{api_key}" end if response.status == 200 JSON.parse(response.body) else respond_to_error(response) end end end |