Class: SOULs::SOULsMutation

Inherits:
GraphQL::Schema::RelayClassicMutation
  • Object
show all
Defined in:
lib/souls/app/graphql/souls_mutation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.souls_auth_check(context) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


58
59
60
# File 'lib/souls/app/graphql/souls_mutation.rb', line 58

def self.souls_auth_check(context)
  raise(GraphQL::ExecutionError, "You need to sign in!!") if context[:user].nil?
end

.souls_check_user_permissions(user, obj, method) ⇒ Object

Raises:

  • (StandardError)


3
4
5
6
7
8
9
10
# File 'lib/souls/app/graphql/souls_mutation.rb', line 3

def self.souls_check_user_permissions(user, obj, method)
  raise(StandardError, "Invalid or Missing Token") unless user

  policy_class = obj.class.name + "Policy"
  policy_clazz = policy_class.constantize.new(user, obj)
  permission = policy_clazz.public_send(method)
  raise(Pundit::NotAuthorizedError, "permission error!") unless permission
end

.souls_fb_auth(token:) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
# File 'lib/souls/app/graphql/souls_mutation.rb', line 12

def self.souls_fb_auth(token:)
  FirebaseIdToken::Certificates.request!
  sleep(3) if ENV["RACK_ENV"] == "development"
  user = FirebaseIdToken::Signature.verify(token)
  raise(ArgumentError, "Invalid or Missing Token") if user.blank?

  user
end

.souls_get_worker(worker_name: "") ⇒ Object



53
54
55
56
# File 'lib/souls/app/graphql/souls_mutation.rb', line 53

def self.souls_get_worker(worker_name: "")
  workers = SOULs.configuration.workers
  workers.filter { |n| n[:name] == worker_name }
end

.souls_make_graphql_query(query: "newCommentMailer", args: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/souls/app/graphql/souls_mutation.rb', line 27

def self.souls_make_graphql_query(query: "newCommentMailer", args: {})
  if args.blank?
    query_string = %(query { #{query.to_s.underscore.camelize(:lower)} { response } })
  else
    inputs = ""
    args.each do |key, value|
      inputs +=
        if value.instance_of?(String)
          "#{key.to_s.underscore.camelize(:lower)}: \"#{value}\" "
        else
          "#{key.to_s.underscore.camelize(:lower)}: #{value} "
        end
    end
    query_string = %(query { #{query.to_s.underscore.camelize(:lower)}(#{inputs}) { response } })
  end
  query_string
end

.souls_post_to_dev(worker_name: "", query_string: "") ⇒ Object



45
46
47
48
49
50
51
# File 'lib/souls/app/graphql/souls_mutation.rb', line 45

def self.souls_post_to_dev(worker_name: "", query_string: "")
  app = SOULs.configuration.app
  port = souls_get_worker(worker_name: "souls-#{app}-#{worker_name}")[0][:port]
  endpoint = SOULs.configuration.endpoint
  res = Net::HTTP.post_form(URI.parse("http://localhost:#{port}#{endpoint}"), { query: query_string })
  res.body
end

.souls_publish_pubsub_queue(topic_name: "send-mail-job", message: "text!") ⇒ Object



21
22
23
24
25
# File 'lib/souls/app/graphql/souls_mutation.rb', line 21

def self.souls_publish_pubsub_queue(topic_name: "send-mail-job", message: "text!")
  pubsub = Google::Cloud::Pubsub.new(project: ENV["SOULS_GCP_PROJECT_ID"])
  topic = pubsub.topic(topic_name)
  topic.publish(message)
end

Instance Method Details

#get_instance_idObject



66
67
68
# File 'lib/souls/app/graphql/souls_mutation.rb', line 66

def get_instance_id
  `curl http://metadata.google.internal/computeMetadata/v1/instance/id -H Metadata-Flavor:Google`
end

#production?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/souls/app/graphql/souls_mutation.rb', line 62

def production?
  ENV["RACK_ENV"] == "production"
end