Class: SOULs::SOULsMutation

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

Instance Method Summary collapse

Instance Method Details

#get_instance_idObject



88
89
90
# File 'lib/souls/app/graphql/souls_mutation.rb', line 88

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

#production?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/souls/app/graphql/souls_mutation.rb', line 84

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

#souls_auth_check(context) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


80
81
82
# File 'lib/souls/app/graphql/souls_mutation.rb', line 80

def 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)


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

def 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)


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

def souls_fb_auth(token: "")
  raise(ArgumentError, "Invalid or Missing Token") if token.blank?

  FirebaseIdToken::Certificates.request!
  user = FirebaseIdToken::Signature.verify(token)
  if user.blank?
    Retryable.retryable(tries: 20, on: ArgumentError) do
      user = FirebaseIdToken::Signature.verify(token)
      raise(ArgumentError, "Invalid or Missing Token") if user.blank?
    end
  end
  user
end

#souls_get_worker(worker_name: "") ⇒ Object



75
76
77
78
# File 'lib/souls/app/graphql/souls_mutation.rb', line 75

def 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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/souls/app/graphql/souls_mutation.rb', line 50

def 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(url:, payload: {}, content_type: "application/json") ⇒ Object



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

def souls_post(url:, payload: {}, content_type: "application/json")
  response = Faraday.post(url, payload.to_json, "Content-Type": content_type)
  response.body
end

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



68
69
70
71
72
73
# File 'lib/souls/app/graphql/souls_mutation.rb', line 68

def souls_post_to_dev(worker_name: "", query_string: "")
  port = souls_get_worker(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



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

def 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

#souls_worker_trigger(worker_name:, query_file_name:, args: {}) ⇒ Object



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

def souls_worker_trigger(worker_name:, query_file_name:, args: {})
  query_file_name = query_file_name.gsub("_", "-")
  topic_name = "souls-#{worker_name}-#{query_file_name}"
  query = query_file_name.underscore.camelize(:lower)
  query_string = souls_make_graphql_query(query:, args:)
  case ENV["RACK_ENV"]
  when "production"
    souls_publish_pubsub_queue(topic_name:, message: query_string)
  when "development"
    puts(souls_post_to_dev(worker_name:, query_string:))
  end
end