Module: Blockspring

Defined in:
lib/blockspring.rb,
lib/blockspring/version.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.define(block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blockspring.rb', line 29

def self.define(block)
  @request = {
    params: {}
  }

  # From STDIN
  stdin = STDIN.tty? ? "{}" : $stdin.read
  stdin_params = JSON.parse(stdin)

  @request[:params] = stdin_params

  # From sysargs
  sys_args = Hash[ ARGV.flat_map{|s| s.scan(/--?([^=\s]+)(?:=(\S+))?/) } ]

  sys_args.each do |param_name, param_value|
    @request[:params][param_name] = param_value
  end

  # Set response
  @response = Response.new

  block.call(@request, @response)
end

.run(block, data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blockspring.rb', line 7

def self.run(block, data)
  api_key = ENV['BLOCKSPRING_API_KEY']

  if(!api_key)
    raise "BLOCKSPRING_API_KEY environment variable not set"
  end

  block_parts = block.split("/")
  block = block_parts[block_parts.length - 1]

  response = RestClient.post "https://sender.blockspring.com/api_v1/blocks/#{block}?api_key=#{api_key}", data.to_json, :content_type => :json, :accept => :json

  body = JSON.parse(response.body)

  begin
    body["results"] = JSON.parse(body["results"])
    return body
  rescue
    return body
  end
end