Module: Plurall::Api

Defined in:
lib/plurall/api.rb,
lib/plurall/api/http.rb,
lib/plurall/api/client.rb,
lib/plurall/api/schema.rb,
lib/plurall/api/person_query.rb,
lib/plurall/api/classes_query.rb,
lib/plurall/api/schools_query.rb,
lib/plurall/api/managers_query.rb,
lib/plurall/api/students_query.rb,
lib/plurall/api/teachers_query.rb,
lib/plurall/api/directors_query.rb,
lib/plurall/api/attendants_query.rb,
lib/plurall/api/coordinators_query.rb,
lib/plurall/api/administrators_query.rb,
lib/plurall/api/student_classes_query.rb

Constant Summary collapse

HTTP =
::GraphQL::Client::HTTP.new Plurall.configuration.id_graphql_url do
  def headers _context
    {"Authorization" => "Bearer #{Plurall::Api.id_authorization}"}
  end
end
Client =
::GraphQL::Client.new schema: Schema, execute: HTTP
Schema =
::GraphQL::Client.load_schema HTTP
PersonQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($uuid: String!) {
    person(uuid: $uuid) {
      id
      uuid
      name
      email
      roles {
        id
        name
        schools {
          id
          legalName
          tradeName
          cnpj
          mecCode
        }
      }
      externalReference {
        applicationId
        identifier
        applicationName
      }
    }
  }
GRAPHQL
ClassesQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          classes(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                id
                name
                period {
                  id
                  year
                }
                levelGrades {
                  gradeId
                  gradeName
                  levelId
                  levelName
                }
                materials {
                  id
                  title
                  description
                  brand {
                    id
                    name
                  }
                  status {
                    id
                    name
                  }
                }
                disciplines {
                  id
                  name
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
SchoolsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(first: $first, after: $after, before: $before, page: $page, search: $search) {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
        hasPreviousPage
        startCursor
      }
      edges {
        node {
          id
          legalName
          tradeName
          cnpj
          mecCode
          address {
            address
            city
            neighborhood
            number
            state
            zipCode
          }
          brands {
            id
            name
          }
          externalReference {
            applicationId
            identifier
            applicationName
          }
          teachers(first: 1) {
            totalCount
          }
          students(first: 1) {
            totalCount
          }
          classes(first: 1) {
            totalCount
          }
          administrators(first: 1) {
            totalCount
          }
          attendants(first: 1) {
            totalCount
          }
          coordinators(first: 1) {
            totalCount
          }
          directors(first: 1) {
            totalCount
          }
          managers(first: 1) {
            totalCount
          }
        }
      }
    }
  }
GRAPHQL
ManagersQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          managers(first: $first, after: $after, before: $before, page: $page) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                id
                uuid
                email
                name
                activationCode
                activationDate
                birthDate
                cpf
                createdAt
                gender
                inactivationDate
                modifiedAt
                optionInEmail
                optionInSms
                optionOfUse
                personStatusId
                rg
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
StudentsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          students(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                id
                uuid
                email
                enrollment
                name
                classes(first: 1) {
                  totalCount
                  pageInfo {
                    endCursor
                    hasNextPage
                    hasPreviousPage
                    startCursor
                  }
                  edges {
                    node {
                      id
                      name
                      period {
                        id
                        year
                      }
                      levelGrades {
                        gradeId
                        gradeName
                        levelId
                        levelName
                      }
                      materials {
                        id
                        title
                        description
                        brand {
                          id
                          name
                        }
                        status {
                          id
                          name
                        }
                      }
                      disciplines {
                        id
                        name
                      }
                    }
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
TeachersQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          teachers(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                id
                uuid
                email
                name
                classes {
                  id
                  name
                  period {
                    id
                    year
                  }
                  levelGrades {
                    gradeId
                    gradeName
                    levelId
                    levelName
                  }
                  materials {
                    id
                    title
                    description
                    brand {
                      id
                      name
                    }
                    status {
                      id
                      name
                    }
                  }
                  disciplines {
                    id
                    name
                  }
                  teacherDisciplines {
                    id
                    name
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
DirectorsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          directors(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                uuid
                email
                name
                roles {
                  id
                  name
                  schools {
                    id
                    legalName
                    cnpj
                    tradeName
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
AttendantsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          attendants(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                uuid
                email
                name
                roles {
                  id
                  name
                  schools {
                    id
                    legalName
                    cnpj
                    tradeName
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
CoordinatorsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          coordinators(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                uuid
                email
                name
                levels {
                  id
                  name
                }
                roles {
                  id
                  name
                  schools {
                    id
                    legalName
                    cnpj
                    tradeName
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
AdministratorsQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          administrators(first: $first, after: $after, before: $before, page: $page, search: $search) {
            totalCount
            pageInfo {
              endCursor
              hasNextPage
              hasPreviousPage
              startCursor
            }
            edges {
              node {
                uuid
                email
                name
                roles {
                  id
                  name
                  schools {
                    id
                    legalName
                    cnpj
                    tradeName
                  }
                }
                externalReference {
                  applicationId
                  identifier
                  applicationName
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL
StudentClassesQuery =
Plurall::Api::Client.parse <<-GRAPHQL
  query ($school_id: ID!, $uuid: String!, $first: IntRangeFrom1To30!, $after: String!, $before: String!, $page: Int!, $search: String!) {
    schools(id: $school_id, first: 1) {
      edges {
        node {
          id
          legalName
          cnpj
          tradeName
          students(uuid: $uuid, first: 1) {
            edges {
              node {
                id
                uuid
                email
                enrollment
                name
                classes(first: $first, after: $after, before: $before, page: $page, search: $search) {
                  totalCount
                  pageInfo {
                    endCursor
                    hasNextPage
                    hasPreviousPage
                    startCursor
                  }
                  edges {
                    node {
                      id
                      name
                      period {
                        id
                        year
                      }
                      levelGrades {
                        gradeId
                        gradeName
                        levelId
                        levelName
                      }
                      materials {
                        id
                        title
                        description
                        brand {
                          id
                          name
                        }
                        status {
                          id
                          name
                        }
                      }
                      disciplines {
                        id
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
GRAPHQL

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.id_authorizationObject



35
36
37
38
39
40
# File 'lib/plurall/api.rb', line 35

def id_authorization
  reset_id_authorization! if @id_authorization.nil?

  # This value will expire after a few hours, so holding in memory is fine.
  @id_authorization
end

Class Method Details

.administrators(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



92
93
94
# File 'lib/plurall/api.rb', line 92

def self.administrators school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::AdministratorsQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.attendants(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



96
97
98
# File 'lib/plurall/api.rb', line 96

def self.attendants school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::AttendantsQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.classes(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



88
89
90
# File 'lib/plurall/api.rb', line 88

def self.classes school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::ClassesQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.coordinators(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



100
101
102
# File 'lib/plurall/api.rb', line 100

def self.coordinators school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::CoordinatorsQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.directors(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



104
105
106
# File 'lib/plurall/api.rb', line 104

def self.directors school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::DirectorsQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.managers(school_id:, first: 30, after: "", before: "", page: 0) ⇒ Object



108
109
110
# File 'lib/plurall/api.rb', line 108

def self.managers school_id:, first: 30, after: "", before: "", page: 0
  Plurall::Api.query Plurall::Api::ManagersQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page}
end

.me(access_token:, me_url: Plurall.configuration.me_url) ⇒ Object



28
29
30
# File 'lib/plurall/api.rb', line 28

def self.me access_token:, me_url: Plurall.configuration.me_url
  Faraday.post me_url, {}, {"Authorization" => "Bearer #{access_token}"}
end

.new_id_authorization!(id_application: Plurall.configuration.id_application, id_auth_url: Plurall.configuration.id_auth_url) ⇒ Object



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

def new_id_authorization! id_application: Plurall.configuration.id_application,
  id_auth_url: Plurall.configuration.id_auth_url

  auth_request = Faraday.post id_auth_url,
    {idApplication: id_application}.to_json,
    {"Content-Type" => "application/json"}
  JSON.parse(auth_request.body).dig("authorization")
end

.oauth_authorize_url(redirect_uri:, authorize_url: Plurall.configuration.oauth_authorize_url, client_id: Plurall.configuration.client_id) ⇒ Object



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

def self.oauth_authorize_url redirect_uri:, authorize_url: Plurall.configuration.oauth_authorize_url,
  client_id: Plurall.configuration.client_id

  uri = URI authorize_url
  params = {response_type: :code, client_id: client_id, redirect_uri: redirect_uri}
  uri.query = URI.encode_www_form params
  uri.to_s
end

.oauth_token(code:, redirect_uri:, oauth_token_url: Plurall.configuration.oauth_token_url, client_id: Plurall.configuration.client_id, client_secret: Plurall.configuration.client_secret) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/plurall/api.rb', line 7

def self.oauth_token code:, redirect_uri:, oauth_token_url: Plurall.configuration.oauth_token_url,
  client_id: Plurall.configuration.client_id, client_secret: Plurall.configuration.client_secret

  Faraday.post oauth_token_url, {
    grant_type: :authorization_code,
    code: code,
    redirect_uri: redirect_uri,
    client_id: client_id,
    client_secret: client_secret
  }
end

.person(uuid:) ⇒ Object



112
113
114
# File 'lib/plurall/api.rb', line 112

def self.person uuid:
  Plurall::Api.query Plurall::Api::PersonQuery, variables: {uuid: uuid}
end

.query(query, **kwargs) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/plurall/api.rb', line 116

def self.query query, **kwargs
  retries ||= 0

  response = Plurall::Api::Client.query query, **kwargs

  raise Plurall::TokenExpiredError if response.errors.all[:data].any? { |error| error.include? "TokenExpiredError" }

  # TODO: Improve partial error handling here...
  raise "NIL DATA: #{query} #{kwargs} | #{response.to_h}" if response.data.nil?

  if response.errors.any? || response.errors.all.any?
    raise (response.errors.values + response.errors.all.values).uniq.join("|")
  end

  response
rescue Plurall::TokenExpiredError => e
  Plurall::Api.reset_id_authorization!

  retry if (retries += 1) < 2 # retry twice

  raise e
end

.reset_id_authorization!Object



42
43
44
# File 'lib/plurall/api.rb', line 42

def reset_id_authorization!
  self.id_authorization = new_id_authorization!
end

.schools(first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



72
73
74
# File 'lib/plurall/api.rb', line 72

def self.schools first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::SchoolsQuery, variables: {first: first, after: after, before: before, page: page, search: search}
end

.student_classes(school_id:, uuid:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



84
85
86
# File 'lib/plurall/api.rb', line 84

def self.student_classes school_id:, uuid:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::StudentClassesQuery, variables: {school_id: school_id, uuid: uuid, first: first, after: after, before: before, page: page, search: search}
end

.students(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



80
81
82
# File 'lib/plurall/api.rb', line 80

def self.students school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::StudentsQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end

.teachers(school_id:, first: 30, after: "", before: "", page: 0, search: "") ⇒ Object



76
77
78
# File 'lib/plurall/api.rb', line 76

def self.teachers school_id:, first: 30, after: "", before: "", page: 0, search: ""
  Plurall::Api.query Plurall::Api::TeachersQuery, variables: {school_id: school_id, first: first, after: after, before: before, page: page, search: search}
end