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



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

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

#post(url:, payload: {}, content_type: "application/json") ⇒ Object



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

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

#production?Boolean

Returns:

  • (Boolean)


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

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

#souls_auth_check(context) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


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

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
# File 'lib/souls/app/graphql/souls_mutation.rb', line 30

def 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



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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/souls/app/graphql/souls_mutation.rb', line 45

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_to_dev(worker_name: "", query_string: "") ⇒ Object



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

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



39
40
41
42
43
# File 'lib/souls/app/graphql/souls_mutation.rb', line 39

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: query, args: args)
  case ENV["RACK_ENV"]
  when "production"
    souls_publish_pubsub_queue(topic_name: topic_name, message: query_string)
  when "development"
    puts(souls_post_to_dev(worker_name: worker_name, query_string: query_string))
  end
end