Class: FfApi::V1

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject

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_urlObject



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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ff_api.rb', line 43

def districts(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
            districts = JSON.parse(response.body)
            districts.map { |district_hash| FfApi::District.new(district_hash) }
        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
            FfApi::District.new(JSON.parse(response.body))
        else
            respond_to_error(response)
        end
    end
end

#respond_to_error(response) ⇒ Object



67
68
69
70
# File 'lib/ff_api.rb', line 67

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
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ff_api.rb', line 19

def schools(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
            schools = JSON.parse(response.body)
            schools.map { |school_hash| FfApi::School.new(school_hash) }
        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
            FfApi::School.new(JSON.parse(response.body))
        else
            respond_to_error(response)
        end
    end
end