Class: SOULs::SOULsMutation
- Inherits:
-
GraphQL::Schema::RelayClassicMutation
- Object
- GraphQL::Schema::RelayClassicMutation
- SOULs::SOULsMutation
- Defined in:
- lib/souls/app/graphql/souls_mutation.rb
Class Method Summary collapse
- .get_instance_id ⇒ Object
- .post(url:, payload: {}, content_type: "application/json") ⇒ Object
- .production? ⇒ Boolean
- .souls_auth_check(context) ⇒ Object
- .souls_check_user_permissions(user, obj, method) ⇒ Object
- .souls_fb_auth(token:) ⇒ Object
- .souls_get_worker(worker_name: "") ⇒ Object
- .souls_make_graphql_query(query: "newCommentMailer", args: {}) ⇒ Object
- .souls_post_to_dev(worker_name: "", query_string: "") ⇒ Object
- .souls_publish_pubsub_queue(topic_name: "send-mail-job", message: "text!") ⇒ Object
Instance Method Summary collapse
Class Method Details
.get_instance_id ⇒ Object
83 84 85 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 83 def self.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 self.post(url:, payload: {}, content_type: "application/json") response = Faraday.post(url, payload.to_json, "Content-Type": content_type) response.body end |
.production? ⇒ Boolean
79 80 81 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 79 def self.production? ENV["RACK_ENV"] == "production" end |
.souls_auth_check(context) ⇒ Object
75 76 77 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 75 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
21 22 23 24 25 26 27 28 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 21 def self.(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) = policy_clazz.public_send(method) raise(Pundit::NotAuthorizedError, "permission error!") unless end |
.souls_fb_auth(token:) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 30 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
70 71 72 73 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 70 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
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 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
63 64 65 66 67 68 |
# File 'lib/souls/app/graphql/souls_mutation.rb', line 63 def self.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 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() end |
Instance Method Details
#souls_worker_trigger(worker_name:, query_file_name:) ⇒ 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:) 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) 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 |