Class: Blockspring::CLI::Command::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/blockspring/cli/command/block.rb

Overview

manipulate blocks (get, push, pull, new)

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, namespace

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

#getObject

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.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blockspring/cli/command/block.rb', line 18

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

#newObject

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/blockspring/cli/command/block.rb', line 101

def new
  user, key = Blockspring::CLI::Auth.get_credentials
  language = @args[0]
  name = @args[1]

  block = {}

  block['code'] = ""

  block['config'] = {
    "user" => user,
    "title" => name,
    "description" => '',
    "parameters" => {},
    "is_public" => false,
    "language" => language
  }

  dir_name = create_block_directory(block)
  if dir_name
    save_block_files(block, dir_name)
  end
end

#pullObject

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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/blockspring/cli/command/block.rb', line 42

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, '.')
  puts "Done."
end

#pushObject

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blockspring/cli/command/block.rb', line 64

def push
  _user, key = Blockspring::CLI::Auth.get_credentials
  config_text = File.read('blockspring.json')
  config_json = JSON.parse(config_text)
  # TODO: check for language
  # language could eventually be js:0.10.x or py:3 or ruby:MRI-2.0
  script_file = "block.#{config_json['language'].split(':')[0]}"
  script = File.read(script_file)

  payload = {
    code: script,
    config: config_json
  }

  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
  json_response = JSON.parse(response.to_str)
  save_block_files(json_response, '.')
end