Class: Cnvrg::Flows
- Inherits:
-
Object
- Object
- Cnvrg::Flows
- Defined in:
- lib/cnvrg/flow.rb
Class Method Summary collapse
Instance Method Summary collapse
- #edit_href ⇒ Object
- #edit_version_href(version) ⇒ Object
- #export(version, file: nil) ⇒ Object
- #get_flow ⇒ Object
- #get_version(version) ⇒ Object
-
#initialize(flow_slug, project: nil) ⇒ Flows
constructor
A new instance of Flows.
- #reload_flow ⇒ Object
- #run ⇒ Object
- #set_flow(new_flow) ⇒ Object
- #set_flow_slug(slug) ⇒ Object
- #version_href(version = nil) ⇒ Object
Constructor Details
#initialize(flow_slug, project: nil) ⇒ Flows
Returns a new instance of Flows.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/cnvrg/flow.rb', line 3 def initialize(flow_slug, project: nil) @project = project || Cnvrg::Project.new(Cnvrg::CLI.get_project_home) @flow_info= Flows.resolve_flow_title(flow_slug, project) @slug = @flow_info["slug"] @tasks = {} @relations = {} @title = nil @base_resource = @project.base_resource + "flows/#{@slug}" @public_url = "#{@project.url}/flows/#{@slug}" # self.reload_flow end |
Class Method Details
.create_flow(project, recipe, run: false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cnvrg/flow.rb', line 57 def self.create_flow(project, recipe, run: false) url = "#{project.base_resource}flows" if run url += "/run" end resp = Cnvrg::API.request(url, 'POST', {flow_version: recipe.to_json}) || {} if resp["status"] == 200 return [Flows.new(resp["flow_version"]["flow_id"], project: project), resp["flow_version"]["id"]] elsif resp["status"].between?(400,499) raise StandardError.new(resp["message"]) end raise StandardError.new("Can't create new flow") end |
.resolve_flow_title(title, project) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cnvrg/flow.rb', line 15 def self.resolve_flow_title(title, project) resp = Cnvrg::API.request("#{project.base_resource}/flows", 'GET') if resp.blank? raise StandardError.new("Can't resolve flow") end res = resp["result"].find{|flow| flow["slug"].downcase == title.downcase} res ||= resp["result"].find{|flow| flow["title"].downcase == title.downcase} if res.blank? raise StandardError.new("Can't find flow with title #{title}") end res end |
Instance Method Details
#edit_href ⇒ Object
28 29 30 |
# File 'lib/cnvrg/flow.rb', line 28 def edit_href "#{@public_url}/flow_versions/new" end |
#edit_version_href(version) ⇒ Object
32 33 34 |
# File 'lib/cnvrg/flow.rb', line 32 def edit_version_href(version) return "#{edit_href}?flow_version_slug=#{version}" end |
#export(version, file: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cnvrg/flow.rb', line 40 def export(version, file: nil) resp = Cnvrg::API.request(version_href(version), 'GET') if resp["status"] != 200 raise StandardError.new("Cant find flow version: #{version} for flow: #{@slug}") end flow_version = resp["flow_version"] api_recipe = flow_version["api_recipe"] file = file.presence || "flow-#{@slug.downcase.gsub("\s", "_")}.yml" File.open(file, "w"){|f| f.write api_recipe.to_yaml} file end |
#get_flow ⇒ Object
71 72 73 74 75 76 |
# File 'lib/cnvrg/flow.rb', line 71 def get_flow unless File.exists? @fullpath raise StandardError.new("Cant find flow in #{@fullpath}") end YAML.load_file(@fullpath) end |
#get_version(version) ⇒ Object
52 53 54 55 |
# File 'lib/cnvrg/flow.rb', line 52 def get_version(version) end |
#reload_flow ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cnvrg/flow.rb', line 88 def reload_flow flow = self.get_flow @title = flow[:title] @slug = flow[:slug] @relations = flow[:relations] local_tasks = flow[:tasks] || {} @relations.each do |relation| relation.values.each do |task| if local_tasks[task].present? @tasks[task] = Cnvrg::Task.new(@project.local_path, content: local_tasks[task]) else @tasks[task] = Cnvrg::Task.new(@project.local_path, path: task) end end end end |
#run ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/cnvrg/flow.rb', line 106 def run resp = Cnvrg::API.request("#{@base_resource}/#{@slug}/run", 'POST') if Cnvrg::CLI.is_response_success(resp) return resp end Cnvrg::CLI.("Cant run flow #{@slug}") end |
#set_flow(new_flow) ⇒ Object
78 79 80 |
# File 'lib/cnvrg/flow.rb', line 78 def set_flow(new_flow) File.open(@fullpath, "w"){|file| file.write new_flow.to_yaml} end |
#set_flow_slug(slug) ⇒ Object
82 83 84 85 86 |
# File 'lib/cnvrg/flow.rb', line 82 def set_flow_slug(slug) flow = self.get_flow flow[:slug] = slug self.set_flow(flow) end |
#version_href(version = nil) ⇒ Object
36 37 38 |
# File 'lib/cnvrg/flow.rb', line 36 def version_href(version=nil) "#{@base_resource}/flow_versions/#{version || 'latest'}" end |