Class: Parse::Stack::Tasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/parse/stack/tasks.rb

Overview

Defines all the related Rails tasks for Parse.

Instance Method Summary collapse

Instance Method Details

#install_tasksObject

Installs the rake tasks.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/parse/stack/tasks.rb', line 23

def install_tasks
  if defined?(::Rails)
    unless Rake::Task.task_defined?("db:seed") || Rails.root.blank?
      namespace :db do
        desc "Seeds your database with by loading db/seeds.rb"
        task :seed => "parse:env" do
          load Rails.root.join("db", "seeds.rb")
        end
      end
    end
  end

  namespace :parse do
    task :env do
      if Rake::Task.task_defined?("environment")
        Rake::Task["environment"].invoke
        if defined?(::Rails)
          Rails.application.eager_load! if Rails.application.present?
        end
      end
    end

    task :verify_env => :env do
      unless Parse::Client.client?
        raise "Please make sure you have setup the Parse.setup configuration before invoking task. Usually done in the :environment task."
      end

      endpoint = ENV["HOOKS_URL"] || ""
      unless endpoint.starts_with?("http://") || endpoint.starts_with?("https://")
        raise "The ENV variable HOOKS_URL must be a <http/s> url : '#{endpoint}'. Ex. https://12345678.ngrok.io/webhooks"
      end
    end

    desc "Run auto_upgrade on all of your Parse models."
    task :upgrade => :env do
      puts "Auto Upgrading Parse schemas..."
      Parse.auto_upgrade! do |k|
        puts "[+] #{k}"
      end
    end

    namespace :webhooks do
      desc "Register local webhooks with Parse server"
      task :register => :verify_env do
        endpoint = ENV["HOOKS_URL"]
        puts "Registering Parse Webhooks @ #{endpoint}"
        Rake::Task["parse:webhooks:register:functions"].invoke
        Rake::Task["parse:webhooks:register:triggers"].invoke
      end

      desc "List all webhooks and triggers registered with the Parse Server"
      task :list => :verify_env do
        Rake::Task["parse:webhooks:list:functions"].invoke
        Rake::Task["parse:webhooks:list:triggers"].invoke
      end

      desc "Remove all locally registered webhooks from the Parse Application."
      task :remove => :verify_env do
        Rake::Task["parse:webhooks:remove:functions"].invoke
        Rake::Task["parse:webhooks:remove:triggers"].invoke
      end

      namespace :list do
        task :functions => :verify_env do
          endpoint = ENV["HOOKS_URL"] || "-"
          Parse.client.functions.each do |r|
            name = r["functionName"]
            url = r["url"]
            star = url.starts_with?(endpoint) ? "*" : " "
            puts "[#{star}] #{name} -> #{url}"
          end
        end

        task :triggers => :verify_env do
          endpoint = ENV["HOOKS_URL"] || "-"
          triggers = Parse.client.triggers.results
          triggers.sort! { |x, y| [x["className"], x["triggerName"]] <=> [y["className"], y["triggerName"]] }
          triggers.each do |r|
            name = r["className"]
            trigger = r["triggerName"]
            url = r["url"]
            star = url.starts_with?(endpoint) ? "*" : " "
            puts "[#{star}] #{name}.#{trigger} -> #{url}"
          end
        end
      end

      namespace :register do
        task :functions => :verify_env do
          endpoint = ENV["HOOKS_URL"]
          Parse::Webhooks.register_functions!(endpoint) do |name|
            puts "[+] function - #{name}"
          end
        end

        task :triggers => :verify_env do
          endpoint = ENV["HOOKS_URL"]
          Parse::Webhooks.register_triggers!(endpoint, { include_wildcard: true }) do |trigger, name|
            puts "[+] #{trigger.to_s.ljust(12, " ")} - #{name}"
          end
        end
      end

      namespace :remove do
        task :functions => :verify_env do
          Parse::Webhooks.remove_all_functions! do |name|
            puts "[-] function - #{name}"
          end
        end

        task :triggers => :verify_env do
          Parse::Webhooks.remove_all_triggers! do |trigger, name|
            puts "[-] #{trigger.to_s.ljust(12, " ")} - #{name}"
          end
        end
      end
    end # webhooks
  end # webhooks namespace
end