Module: Blockspring

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

Defined Under Namespace

Classes: Request, Response

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.define(block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/blockspring.rb', line 139

def self.define(block)
  @response = Response.new

  #stdin parsing
  if(!STDIN.tty?)
    @request = self.parse($stdin.read, false)
  else
    @request = Request.new
  end

  #args parsing
  if (ARGV.length > 0)
    argv = {}
    for arg in ARGV
      found_match = /([^=]*)\=(.*)/.match(arg)
      if found_match
        found_match = found_match.captures
        if found_match[0][0..1] == "--"
          argv[ found_match[0][2..-1] ] = found_match[1]
        else
          argv[ found_match[0] ] = found_match[1]
        end
      end
    end
  else
    argv = {}
  end

  for key in argv.keys
    @request.params[key] = argv[key]
  end

  block.call(@request, @response)
end

.parse(input_params, json_parsed = true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blockspring.rb', line 8

def self.parse(input_params, json_parsed = true)
  @request = Request.new

  if json_parsed == true
    params = input_params
  else
    begin
      params = JSON.parse(input_params)
    rescue
      raise "You didn't pass valid json inputs."
    end
  end

  if !(params.is_a?(Hash))
    raise "Can't parse keys/values from your json inputs."
  end

  if !(params.has_key?("_blockspring_spec") && params["_blockspring_spec"])
    @request.instance_variable_set("@params", params)
  else
    for var_name in params.keys
      if (var_name == "_blockspring_spec")
        # pass
      elsif ((var_name == "_errors") && params[var_name].is_a?(Array))
        for error in params[var_name]
          if (error.is_a?(Hash)) && (error.has_key?("title"))
            @request.addError(error)
          end
        end
      elsif ((var_name == "_headers") && params[var_name].is_a?(Hash))
        headers = params[var_name]
        if(headers.is_a?(Hash))
          @request.addHeaders(stringify_keys(headers))
        else
          @request.addHeaders(headers)
        end
      elsif (
        params[var_name].is_a?(Hash) and
        params[var_name].has_key?("filename") and
        params[var_name]["filename"] and
        # either data or url must exist and not be empty
        (
          (params[var_name].has_key?("data") and params[var_name]["data"]) or
          (params[var_name].has_key?("url") and params[var_name]["url"]))
        )
          suffix = "-%s" % params[var_name]["filename"]
          tmp_file = Tempfile.new(["",suffix])
          if (params[var_name].has_key?("data"))
            begin
              tmp_file.write(Base64.decode64(params[var_name]["data"]))
              @request.params[var_name] = tmp_file.path
            rescue
              @request.params[var_name] = params[var_name]
            end
          else
            begin
              tmp_file.write(RestClient.get(params[var_name]["url"]))
              @request.params[var_name] = tmp_file.path
            rescue
              @request.params[var_name] = params[var_name]
            end
          end
          tmp_file.close
      else
        @request.params[var_name] = params[var_name]
      end
    end
  end

  return @request
end

.run(block, data = {}, api_key = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/blockspring.rb', line 80

def self.run(block, data = {}, api_key = nil )
  if !(data.is_a?(Hash))
    raise "your data needs to be a dictionary."
  end

  data = data.to_json
  api_key = api_key || ENV['BLOCKSPRING_API_KEY'] || ""
  blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com'
  block = block.split("/")[-1]

  begin
    response = RestClient.post "#{blockspring_url}/api_v2/blocks/#{block}?api_key=#{api_key}", data, :content_type => :json
  rescue => e
    response = e.response
  end

  results = response.body

  begin
    return JSON.parse(results)
  rescue
    return results
  end
end

.runParsed(block, data = {}, api_key = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/blockspring.rb', line 105

def self.runParsed(block, data = {}, api_key = nil )
  if !(data.is_a?(Hash))
    raise "your data needs to be a dictionary."
  end

  data = data.to_json
  api_key = api_key || ENV['BLOCKSPRING_API_KEY'] || ""
  blockspring_url = ENV['BLOCKSPRING_URL'] || 'https://sender.blockspring.com'
  block = block.split("/")[-1]

  begin
    response = RestClient.post "#{blockspring_url}/api_v2/blocks/#{block}?api_key=#{api_key}", data, :content_type => :json
  rescue => e
    response = e.response
  end

  results = response.body


  begin
    parsed_results = JSON.parse(results)

    if (!parsed_results.is_a?(Hash))
      return parsed_results
    else
      parsed_results["_headers"] = response.headers
    end
  rescue
    return results
  end

  return self.parse(parsed_results, true)
end