Module: Reivt::RevAPI

Defined in:
lib/reivt/api.rb,
lib/reivt/api/mutations/rev_create.mutation.rb,
lib/reivt/api/mutations/rev_delete.mutation.rb,
lib/reivt/api/mutations/user_create.mutation.rb,
lib/reivt/api/mutations/user_signin.mutation.rb,
lib/reivt/api/mutations/document_create.mutation.rb,
lib/reivt/api/mutations/document_delete.mutation.rb

Overview

An extension of the RevAPI module

Author:

  • brwnrclse

Constant Summary collapse

SCHEMA_PATH =
"#{Dir.pwd}/lib/reivt/schema/schema.json".freeze
ENDPOINT_URL =
'https://api.graph.cool/simple/v1/revwver'.freeze
ENDPOINT =
GraphQL::Client::HTTP.new(ENDPOINT_URL) do
  def headers(_context)
    auth_token = Reivt::REIVT_STORE.transaction do |store|
      store.fetch(:auth0_id, nil)
    end

    { 'Authorization': "Bearer #{auth_token}" }
  end
end
ID_TOKEN =
Reivt::REIVT_STORE.transaction do |store|
  store.fetch(:user_id, nil)
end
SCHEMA =
GraphQL::Client.load_schema(SCHEMA_PATH)
CLIENT =
GraphQL::Client.new(schema: SCHEMA, execute: ENDPOINT)
CreateRevMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($description: String!, $title: String!, $user_id: ID!) {
    createRev(description: $description, title: $title, userId: $user_id) {
      id
    }
  }
GRAPHQL
DeleteRevMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($id: ID!) {
    deleteRev(id: $id) {
      id
    }
  }
GRAPHQL
CreateUserMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($auth0_id: String!) {
    createUser(authProvider:{auth0: {idToken: $auth0_id}}) {
      id
    }
  }
GRAPHQL
SigninMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($auth0_id: String!) {
    signinUser(auth0:{idToken: $auth0_id}) {
      user {
        id
      }
    }
  }
GRAPHQL
CreateDocumentMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($blob: String!, $content_type: String!, $doc_name: String!, $has_diff: Boolean!, $rev_id: ID!) {
    createDocument(blob: $blob, contentType: $content_type, name: $doc_name, hasDiff: $has_diff, revId: $rev_id) {
      id
    }
  }
GRAPHQL
DeleteDocumentMutation =
CLIENT.parse <<-'GRAPHQL'
  mutation($id: ID!) {
    deleteDocument(id: $id) {
      id
    }
  }
GRAPHQL

Class Method Summary collapse

Class Method Details

.create_doc(blob, content_type, doc_name, has_diff, rev_id) ⇒ String

API call to createDocument

Parameters:

  • blob (String)

    The contents of the Document

  • content_type (String)

    The kind of Document

  • doc_name (String)

    The name of the Document

  • rev_id (String)

    The id of the Rev this Document belongs to

Returns:

  • (String)

    The id of the Document



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reivt/api/mutations/document_create.mutation.rb', line 27

def self.create_doc(blob, content_type, doc_name, has_diff, rev_id)
  blob = "That's all folks?" if blob.nil? || blob.strip.empty?
  doc_name = 'Empty Doc' if doc_name.nil? || doc_name.strip.empty?
  has_diff = false if has_diff.nil?

  if content_type.nil? || content_type.strip.empty?
    content_type = 'plain text'
  end

  if rev_id.length < 25
    raise Reivt::GraphQLValidationException, 'Missing rev id'
  end

  result = CLIENT.query(CreateDocumentMutation, variables: {
                          blob: blob, content_type: content_type,
                          doc_name: doc_name, has_diff: has_diff,
                          rev_id: rev_id
                        })
  data = RevAPI.retrieve(result)

  data.createDocument.id
end

.create_rev(description, title) ⇒ String

API call to createRev

Parameters:

  • description (String)

    What the Rev is about

  • title (String)

    The name of the Rev

Returns:

  • (String)

    The id of the created Rev



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/reivt/api/mutations/rev_create.mutation.rb', line 25

def self.create_rev(description, title)
  description = 'Some rev' if description.nil? || description.strip.empty?
  title = 'A rev, right?' if title.nil? || title.strip.empty?

  result = CLIENT.query(RevAPI::CreateRevMutation, variables: {
                          description: description, title: title,
                          user_id: RevAPI::ID_TOKEN
                        })
  data = RevAPI.retrieve(result)

  data.createRev.id
end

.create_user(auth0_id) ⇒ String

Authenticate the user against the api to receive a user id

Parameters:

  • id (String)

    The users’s id from Auth0

Returns:

  • (String)

    The user’s id in the rev api



24
25
26
27
28
29
30
# File 'lib/reivt/api/mutations/user_create.mutation.rb', line 24

def self.create_user(auth0_id)
  result = CLIENT.query(RevAPI::CreateUserMutation,
                        variables: { auth0_id: auth0_id })
  data = RevAPI.retrieve(result)

  data.createUser.id
end

.delete_doc(id) ⇒ String, RevAPI::GraphQLValidationError

API call to deleteDocument

Parameters:

  • id (String)

    The unique identifier of the Document

Returns:

  • (String, RevAPI::GraphQLValidationError)

    The id of the deleted Document or an error



25
26
27
28
29
30
31
32
33
34
# File 'lib/reivt/api/mutations/document_delete.mutation.rb', line 25

def self.delete_doc(id)
  if id.length < 25
    raise raise Reivt::GraphQLValidationException, 'Missing id to delete'
  end

  result = CLIENT.query(DeleteDocumentMutation, variables: { id: id })
  data = RevAPI.retrieve(result)

  data.deleteDocument.id
end

.delete_rev(id) ⇒ String, RevAPI::GraphQLValidationError

API call to deleteRev

Parameters:

  • id (String)

    The unique identifier of the Rev

Returns:

  • (String, RevAPI::GraphQLValidationError)

    The id of the deleted Rev or an error



25
26
27
28
29
30
31
32
33
34
# File 'lib/reivt/api/mutations/rev_delete.mutation.rb', line 25

def self.delete_rev(id)
  if id.length < 25
    raise Reivt::GraphQLValidationException, 'Missing id to delete'
  end

  result = CLIENT.query(RevAPI::DeleteRevMutation, variables: { id: id })
  data = RevAPI.retrieve(result)

  data.deleteRev.id
end

.retrieve(api_response) ⇒ Array

Raise any errors in the response data or simply return the data

Parameters:

  • api_response (Obj)

    A nested object containing either data or errors

Returns:

  • (Array)

    A list containing the data and error object from the req



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/reivt/api.rb', line 43

def self.retrieve(api_response)
  errors = api_response.errors.messages
  data = api_response.data

  if errors.key?(:data)
    raise Reivt::GraphQLDataException, errors[:data].join(', ')
  end

  if data.errors.any?
    raise Reivt::GraphQLDataException, data.errors[:data].join(', ')
  end

  data
end

.signin_user(auth0_id) ⇒ String

Authenticate the user against the api to receive a user id

Parameters:

  • id (String)

    The users’s id from Auth0

Returns:

  • (String)

    The user’s id in the rev api



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/reivt/api/mutations/user_signin.mutation.rb', line 26

def self.(auth0_id)
  result = CLIENT.query(RevAPI::SigninMutation,
                        variables: { auth0_id: auth0_id })

  if result.data.nil?
    nil
  else
    data = RevAPI.retrieve(result)

    data.signinUser.user.id
  end
end