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.



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

#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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/blockspring/cli/command/block.rb', line 125

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

  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

#openObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/blockspring/cli/command/block.rb', line 147

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

#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.



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

#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.



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
# File 'lib/blockspring/cli/command/block.rb', line 65

def push
  _user, key = Blockspring::CLI::Auth.get_credentials

  unless File.exists?('blockspring.json')
    return error('blockspring.json file not found')
  end

  config_text = File.read('blockspring.json')
  config_json = JSON.parse(config_text)

  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

  begin
    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.body)
    save_block_files(json_response, '.', 'Syncronizing')
  rescue RestClient::Exception => msg
    error(msg.inspect)
  end
end