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 +4 -4
- data/README.md +38 -9
- data/lib/blockspring.rb +135 -34
- data/lib/blockspring/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8bdb9eb70f3e1efc2f5239f139e3cbf4e3d89b9
|
4
|
+
data.tar.gz: daa86a908c8b85229e07ffab85b5c93c74128722
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56366394beff6b44ab5c014fb871b9d38448d2f6075362d5e44e72c5cd01aab6a89cbd013c3a55dcbb8b38d818c1d05073e1fcbb9e6a6bc962c8cabbc2810a1d
|
7
|
+
data.tar.gz: c2e8e983022b841250f5109f8c08056fa2b6c24c942a74babb83b2ff47f19e71f10408f3ccdacbd6eb7ea863ede8457dce1fb911b72fad2680e38e9e6796bf4f
|
data/README.md
CHANGED
@@ -1,18 +1,47 @@
|
|
1
|
-
blockspring
|
2
|
-
================
|
1
|
+
# blockspring.rb
|
3
2
|
|
4
|
-
|
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
|
-
|
9
|
-
======
|
10
|
-
In a gemfile: `gem 'blockspring'`
|
7
|
+
### Installation
|
11
8
|
|
12
|
-
|
9
|
+
```ruby
|
10
|
+
gem install blockspring
|
13
11
|
```
|
14
|
-
$ gem install 'blockspring'
|
15
12
|
|
16
|
-
|
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
|
data/lib/blockspring.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
15
|
-
|
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
|
-
|
24
|
+
begin
|
25
|
+
body = JSON.parse(response.body)
|
26
|
+
rescue
|
27
|
+
body = response.body
|
28
|
+
end
|
18
29
|
|
19
|
-
return
|
30
|
+
return body
|
20
31
|
end
|
21
32
|
|
22
33
|
def self.define(block)
|
23
|
-
@request =
|
24
|
-
|
25
|
-
}
|
34
|
+
@request = Request.new
|
35
|
+
@response = Response.new
|
26
36
|
|
27
|
-
#
|
28
|
-
|
29
|
-
|
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
|
-
|
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
|
-
|
34
|
-
|
117
|
+
block.call(@request, @response)
|
118
|
+
end
|
35
119
|
|
36
|
-
|
37
|
-
|
120
|
+
class Request
|
121
|
+
def initialize
|
122
|
+
@params = {}
|
123
|
+
@_errors = []
|
38
124
|
end
|
39
125
|
|
40
|
-
|
41
|
-
|
126
|
+
attr_reader :params
|
127
|
+
attr_reader :_errors
|
42
128
|
|
43
|
-
|
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
|
-
|
50
|
-
|
51
|
-
errors: nil
|
141
|
+
:_blockspring_spec => true,
|
142
|
+
:_errors => []
|
52
143
|
}
|
53
144
|
end
|
54
145
|
|
55
|
-
def addOutput(name, value)
|
56
|
-
@result[
|
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.
|
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[
|
67
|
-
filename
|
68
|
-
|
69
|
-
data
|
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
|
data/lib/blockspring/version.rb
CHANGED