blockspring 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bc7d3b1074e923221774e13ffaefb946736ed1c
4
- data.tar.gz: 88c1eda5789095633670df1dd90b41dea1bfb203
3
+ metadata.gz: d8bdb9eb70f3e1efc2f5239f139e3cbf4e3d89b9
4
+ data.tar.gz: daa86a908c8b85229e07ffab85b5c93c74128722
5
5
  SHA512:
6
- metadata.gz: 53b71fec1279a5e9e1de4d5a9d3ba51c5d9283fce9e96053e4c9702b27857a2e7e49f1636fd19366718717727e3c8b639333e725c8131bea16ef2b75ca9b7755
7
- data.tar.gz: 5dc0be7d7a1e038f4be8adf085936c1cebfb4e7612689f56fb4b80c86d621db53d7a62d3694e9916d6e4c958e5d1a622dd56d8ade697351203af0b47e24890ef
6
+ metadata.gz: 56366394beff6b44ab5c014fb871b9d38448d2f6075362d5e44e72c5cd01aab6a89cbd013c3a55dcbb8b38d818c1d05073e1fcbb9e6a6bc962c8cabbc2810a1d
7
+ data.tar.gz: c2e8e983022b841250f5109f8c08056fa2b6c24c942a74babb83b2ff47f19e71f10408f3ccdacbd6eb7ea863ede8457dce1fb911b72fad2680e38e9e6796bf4f
data/README.md CHANGED
@@ -1,18 +1,47 @@
1
- blockspring-ruby
2
- ================
1
+ # blockspring.rb
3
2
 
4
- Gem for local dev with Blockspring.
3
+ Ruby gem to assist in creating and running blocks (cloud functions) with Blockspring.
5
4
 
6
5
  http://rubygems.org/gems/blockspring
7
6
 
8
- To use
9
- ======
10
- In a gemfile: `gem 'blockspring'`
7
+ ### Installation
11
8
 
12
- Not in a gemfile:
9
+ ```ruby
10
+ gem install blockspring
13
11
  ```
14
- $ gem install 'blockspring'
15
12
 
16
- # In a ruby script
13
+ ### Example Usage
14
+
15
+ Save the following script to an example.rb file:
16
+ ```ruby
17
17
  require 'blockspring'
18
+
19
+ myBlock <- function(request, response){
20
+ sum <- request.params["num1"].to_f + request.params["num2"].to_f
21
+
22
+ response.addOutput("sum", sum)
23
+
24
+ response.end()
25
+ }
26
+
27
+ Blockspring.define(myBlock)
18
28
  ```
29
+
30
+ Then in your command line write:
31
+ ```shell
32
+ ruby example.rb --num1=20 --num2=50
33
+ ```
34
+
35
+ or
36
+
37
+ ```shell
38
+ echo '{"num1":20, "num2": 50}' | ruby example.rb
39
+ ```
40
+
41
+ ### License
42
+
43
+ MIT
44
+
45
+ ### Contact
46
+
47
+ Email us: founders@blockspring.com
@@ -2,77 +2,178 @@ require 'rest_client'
2
2
  require 'json'
3
3
  require 'base64'
4
4
  require 'mime/types'
5
+ require "tempfile"
5
6
 
6
7
  module Blockspring
7
- def self.run(block, data)
8
- api_key = ENV['BLOCKSPRING_API_KEY']
9
-
10
- if(!api_key)
11
- raise "BLOCKSPRING_API_KEY environment variable not set"
8
+ def self.run(block, data = {}, api_key = nil )
9
+ if !(data.is_a?(Hash))
10
+ raise "your data needs to be a dictionary."
12
11
  end
13
12
 
14
- block_parts = block.split("/")
15
- block = block_parts[block_parts.length - 1]
13
+ data = data.to_json
14
+ api_key = api_key || ENV['BLOCKSPRING_API_KEY'] || ""
15
+ blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com'
16
+ block = block.split("/")[-1]
17
+
18
+ begin
19
+ response = RestClient.post "https://sender.blockspring.com/api_v2/blocks/#{block}?api_key=#{api_key}", data, :content_type => :json
20
+ rescue => e
21
+ response = e.response
22
+ end
16
23
 
17
- response = RestClient.post "https://sender.blockspring.com/api_v2/blocks/#{block}?api_key=#{api_key}", data.to_json, :content_type => :json, :accept => :json
24
+ begin
25
+ body = JSON.parse(response.body)
26
+ rescue
27
+ body = response.body
28
+ end
18
29
 
19
- return response.body
30
+ return body
20
31
  end
21
32
 
22
33
  def self.define(block)
23
- @request = {
24
- params: {}
25
- }
34
+ @request = Request.new
35
+ @response = Response.new
26
36
 
27
- # From STDIN
28
- stdin = STDIN.tty? ? "{}" : $stdin.read
29
- stdin_params = JSON.parse(stdin)
37
+ #stdin parsing
38
+ if(!STDIN.tty?)
39
+ begin
40
+ params = JSON.parse($stdin.read)
41
+ rescue
42
+ raise "You didn't pass valid json inputs."
43
+ end
44
+
45
+ if !(params.is_a?(Hash))
46
+ raise "Can't parse keys/values from your json inputs."
47
+ end
48
+
49
+ if !(params.has_key?("_blockspring_spec") && params["_blockspring_spec"])
50
+ @request.instance_variable_set("@params", params)
51
+ else
52
+ for var_name in params.keys
53
+ if (var_name == "_blockspring_spec")
54
+ # pass
55
+ elsif ((var_name == "_errors") && params[var_name].is_a?(Array))
56
+ for error in params[var_name]
57
+ if (error.is_a?(Hash)) && (error.has_key?("title"))
58
+ @request.addError(error)
59
+ end
60
+ end
61
+ elsif (
62
+ params[var_name].is_a?(Hash) and
63
+ params[var_name].has_key?("filename") and
64
+ params[var_name]["filename"] and
65
+ # either data or url must exist and not be empty
66
+ (
67
+ (params[var_name].has_key?("data") and params[var_name]["data"]) or
68
+ (params[var_name].has_key?("url") and params[var_name]["url"]))
69
+ )
70
+ suffix = "-%s" % params[var_name]["filename"]
71
+ tmp_file = Tempfile.new(["",suffix])
72
+ if (params[var_name].has_key?("data"))
73
+ begin
74
+ tmp_file.write(Base64.decode64(params[var_name]["data"]))
75
+ @request.params[var_name] = tmp_file.path
76
+ rescue
77
+ @request.params[var_name] = params[var_name]
78
+ end
79
+ else
80
+ begin
81
+ tmp_file.write(RestClient.get(params[var_name]["url"]))
82
+ @request.params[var_name] = tmp_file.path
83
+ rescue
84
+ @request.params[var_name] = params[var_name]
85
+ end
86
+ end
87
+ tmp_file.close
88
+ else
89
+ @request.params[var_name] = params[var_name]
90
+ end
91
+ end
92
+ end
93
+ end
30
94
 
31
- @request[:params] = stdin_params
95
+ #args parsing
96
+ if (ARGV.length > 0)
97
+ argv = {}
98
+ for arg in ARGV
99
+ found_match = /([^=]*)\=(.*)/.match(arg)
100
+ if found_match
101
+ found_match = found_match.captures
102
+ if found_match[0][0..1] == "--"
103
+ argv[ found_match[0][2..-1] ] = found_match[1]
104
+ else
105
+ argv[ found_match[0] ] = found_match[1]
106
+ end
107
+ end
108
+ end
109
+ else
110
+ argv = {}
111
+ end
112
+
113
+ for key in argv.keys
114
+ @request.params[key] = argv[key]
115
+ end
32
116
 
33
- # From sysargs
34
- sys_args = Hash[ ARGV.flat_map{|s| s.scan(/--?([^=\s]+)(?:=(\S+))?/) } ]
117
+ block.call(@request, @response)
118
+ end
35
119
 
36
- sys_args.each do |param_name, param_value|
37
- @request[:params][param_name] = param_value
120
+ class Request
121
+ def initialize
122
+ @params = {}
123
+ @_errors = []
38
124
  end
39
125
 
40
- # Set response
41
- @response = Response.new
126
+ attr_reader :params
127
+ attr_reader :_errors
42
128
 
43
- block.call(@request, @response)
129
+ def getErrors
130
+ return @_errors
131
+ end
132
+
133
+ def addError(error)
134
+ @_errors.push(error)
135
+ end
44
136
  end
45
137
 
46
138
  class Response
47
139
  def initialize
48
140
  @result = {
49
- data: {},
50
- files: {},
51
- errors: nil
141
+ :_blockspring_spec => true,
142
+ :_errors => []
52
143
  }
53
144
  end
54
145
 
55
- def addOutput(name, value)
56
- @result[:data][name] = value
146
+ def addOutput(name, value = nil)
147
+ @result[name] = value
57
148
  return self
58
149
  end
59
150
 
60
151
  def addFileOutput(name, filepath)
61
152
  filename = File.basename(filepath)
62
- b64_file_contents = Base64.encode64(File.read(filepath))
153
+ b64_file_contents = Base64.strict_encode64(File.read(filepath))
63
154
  mime_type_object = MIME::Types.of(filename).first
64
155
  mime_type = mime_type_object ? mime_type_object.content_type : nil
65
156
 
66
- @result[:files][name] = {
67
- filename: filename,
68
- mimeType: mime_type,
69
- data: b64_file_contents
157
+ @result[name] = {
158
+ :filename => filename,
159
+ :"content-type" => mime_type,
160
+ :data => b64_file_contents
70
161
  }
71
162
  return self
72
163
  end
73
164
 
165
+ def addErrorOutput(title, message = nil)
166
+ @result[:_errors].push({
167
+ title: title,
168
+ message: message
169
+ }
170
+ )
171
+
172
+ return self
173
+ end
174
+
74
175
  def end
75
176
  puts @result.to_json
76
177
  end
77
178
  end
78
- end
179
+ end
@@ -1,3 +1,3 @@
1
1
  module Blockspring
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blockspring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Pinkus