Class: Blockspring::CLI::Command::Block
- Defined in:
- lib/blockspring/cli/command/block.rb
Overview
manipulate blocks (get, push, pull, new)
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#get ⇒ Object
block:get BLOCKID.
-
#new ⇒ Object
block:new LANGUAGE “Block Name”.
- #open ⇒ Object
-
#pull ⇒ Object
block:pull.
-
#push ⇒ Object
block:push.
Methods inherited from Base
Methods included from Helpers
#display, #error, error_with_failure, error_with_failure=, #format_with_bang, #home_directory, #longest, #output_with_bang, #running_on_a_mac?, #running_on_windows?
Constructor Details
This class inherits a constructor from Blockspring::CLI::Command::Base
Instance Method Details
#get ⇒ Object
block:get BLOCKID
pull down an existing block from blockspring
Example:
$ blockspring get testuser/f19512619b94678ea0b4bf383f3a9cf5 Creating directory cool-block-f1951261 Syncing script file cool-block-f1951261/block.py Syncing config file cool-block-f1951261/blockspring.json Done.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/blockspring/cli/command/block.rb', line 19 def get block_parts = @args[0].split("/") block = get_block(block_parts[block_parts.length - 1]) dir_name = create_block_directory(block) if dir_name save_block_files(block, dir_name) puts "Done." end end |
#new ⇒ Object
block:new LANGUAGE “Block Name”
generate a new block
LANGUAGE: js|php|py|R|rb
Example:
$ blockspring new js “My Cool Block” Creating directory my-cool-block Syncing script file my-cool-block/block.js Syncing config file my-cool-block/blockspring.json
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/blockspring/cli/command/block.rb', line 139 def new user, key = Blockspring::CLI::Auth.get_credentials language = @args[0] name = @args[1] return error('You must specify a language') unless language return error('You must specify a name for your block') unless name begin block = get_template(language) block['config']['title'] = name dir_name = create_block_directory(block) if dir_name save_block_files(block, dir_name, 'Creating') end rescue RestClient::ResourceNotFound => msg error("The language '#{language}' is not supported by Blockspring.") rescue RestClient::Exception => msg error(msg.inspect) end end |
#open ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/blockspring/cli/command/block.rb', line 164 def open config_text = File.read('blockspring.json') config_json = JSON.parse(config_text) user = config_json['user'] block_id = config_json['id'] uri = "#{Blockspring::CLI::Auth.base_url}/#{user}/#{block_id}" Launchy.open( uri ) do |exception| puts "Attempted to open #{uri} and failed because #{exception}" end end |
#pull ⇒ Object
block:pull
pull block changes from the server to current directory
Example:
$ blockspring pull Syncing script file block.py Syncing config file blockspring.json Done.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/blockspring/cli/command/block.rb', line 43 def pull # load config file config_text = File.read('blockspring.json') config_json = JSON.parse(config_text) # TODO: ensure valid config puts "Pulling #{config_json['user']}/#{config_json['id']}" block = get_block(config_json['id']) save_block_files(block, '.', 'Pulling') puts "Done." end |
#push ⇒ Object
block:push
push local block changes or new block to the server
Example:
$ blockspring push Syncing script file block.py Syncing config file blockspring.json Done.
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 |
# File 'lib/blockspring/cli/command/block.rb', line 65 def push _user, key = Blockspring::CLI::Auth.get_credentials if not(File.exists?('blockspring.json')) config_json = { 'language' => nil } # hardcode block.* name. find first one and set that to language. Dir["block.*"].each do |block_file| language_match = block_file.match(/\Ablock\.(.*)/) config_json['language'] = language_match ? language_match.captures[0] : nil break end else config_text = File.read('blockspring.json') config_json = JSON.parse(config_text) end if config_json['language'].nil? return error('You must declare a language in your blockspring.json file.') end # language could eventually be js:0.10.x or py:3 or ruby:MRI-2.0 script_file = "block.#{config_json['language'].split(':')[0]}" unless File.exists?(script_file) return error("#{script_file} file not found") end script = File.read(script_file) payload = { code: script, config: config_json } if @args.include? '--force' payload['force'] = true end if config_json['id'] uri = "#{Blockspring::CLI::Auth.base_url}/cli/blocks/#{config_json['id']}" else uri = "#{Blockspring::CLI::Auth.base_url}/cli/blocks" end response = RestClient.post(uri, payload.to_json, :content_type => :json, :accept => :json, params: { api_key: key }, user_agent: Blockspring::CLI.user_agent) do |response, request, result, &block| case response.code when 200 json_response = JSON.parse(response.body) save_block_files(json_response, '.', 'Syncronizing') when 401 error('You must be logged in to push a block') when 404 error('That block does not exist or you don\'t have permission to push to it') end end end |