Module: Simpleflow::Modules

Defined in:
lib/simpleflow.rb,
lib/simpleflow/yql.rb,
lib/simpleflow/http.rb,
lib/simpleflow/json.rb,
lib/simpleflow/ping.rb,
lib/simpleflow/liquid.rb,
lib/simpleflow/jsonpath.rb,
lib/simpleflow/urlencode.rb,
lib/simpleflow/dns_resolve.rb,
lib/simpleflow/object_builder.rb,
lib/simpleflow/string_builder.rb

Class Method Summary collapse

Class Method Details

.comment(params) ⇒ Object

Comment do nothing and return nothing



27
28
29
# File 'lib/simpleflow.rb', line 27

def self.comment(params)
  {}
end

.getIpsFromDomain(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/simpleflow/dns_resolve.rb', line 4

def self.getIpsFromDomain(params)
 require 'resolv'
  
  hosts = []
  dns = Resolv::DNS.new
  dns.getresources(params["domain"], Resolv::DNS::Resource::IN::A).collect do |r| 
     hosts << r.address.to_s
  end
  { "hosts" => hosts }
end

.HTTP(params) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/simpleflow/http.rb', line 13

def self.HTTP(params)
	
  require 'net/http'
				require 'net/https'
  require 'uri'
  require 'cgi'


					require 'yajl'

  uri = URI.parse(params["url"])
				path = uri.request_uri
					
				# Parameters / Body
				body = nil
				
				# Raw request
				httpClass = case params["method"]
 when "GET" then Net::HTTP::Get
 when "PUT" then Net::HTTP::Put
when "POST" then Net::HTTP::Post
 when "DELETE" then  Net::HTTP::Delete
 when "OPTIONS" then Net::HTTP::Options
				end
				raw_request = httpClass.new(path)

				case params["method"]
 when "GET" , "DELETE"  
	path += "?"+ params["urlparams"].map { |p| p[0].to_s+"="+CGI::escape(p[1].to_s) }.join('&')
	raw_request = httpClass.new(path)
 when "PUT" , "POST" 
	body = case params["encoding"]
		when "application/json" then 
			raw_request["content-type"] = "application/json"
			params["urlparams"].to_json
		when "application/xml" then 
				raw_request["content-type"] = "application/xml"
				params["urlparams"].to_xml
		when "application/x-www-form-urlencoded" then 
				raw_request["content-type"] = "application/x-www-form-urlencoded"
				params["urlparams"].map { |p| p[0].to_s+"="+CGI::escape(p[1].to_s) }.join('&')
	end
				end
				
				raw_request.body = body if body
				
				# Basic Auth
				raw_request.basic_auth(uri.user, uri.password) if uri.user
					
				# HTTPS
			  http = Net::HTTP.new(uri.host, uri.port) 
				http.use_ssl = true if uri.scheme == "https"
				
				
				# Perform 
			  res = http.start{ |h| h.request(raw_request) }
			
				o = res.body
			
				case res
when Net::HTTPSuccess then
  
# Parsing
puts "HTTP res content-type : "+res.content_type

if (res.content_type == "application/xml") or (res.content_type == "text/xml")
	o = Hash.from_xml(res.body)
elsif res.content_type == "application/json"
	
	parser = Yajl::Parser.new
     o = parser.parse(res.body)
			
else 
	o = res.body
end

    { "out" => o }

					when Net::HTTPRedirection then

{ "out" => res.error! }

					else

{ "out" => res.error! }

				end
			
end

.input(params) ⇒ Object

For Composed modules



12
13
14
15
16
17
18
19
# File 'lib/simpleflow.rb', line 12

def self.input(params)
			inputName = params["input"]["name"]
if params.keys.include?(inputName)
{"out" => params[inputName] }
			else
{"out" => params["input"]["value"] }
			end
end

.JSONparse(params) ⇒ Object



4
5
6
7
8
# File 'lib/simpleflow/json.rb', line 4

def self.JSONparse(params)
				require 'yajl'
				parser = Yajl::Parser.new
  { "out" => parser.parse(params["in"]) }
end

.JSONPath(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simpleflow/jsonpath.rb', line 6

def self.JSONPath(params)
require 'jsonpath'

begin
	out = JSONPath.lookup( params["in"], params["path"])
rescue Exception => e
	out = {:error => e.message}
end

if params["firstonly"]
	out = out[0]
end

    { "out" =>  out }
end

.liquid(params) ⇒ Object

Liquid Templating



5
6
7
8
9
# File 'lib/simpleflow/liquid.rb', line 5

def self.liquid(params)
			require 'liquid'
			r = Liquid::Template.parse(params["template"]).render 'params' => params["in"]
			{ "out" => r }
end

.ObjectBuilder(params) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/simpleflow/object_builder.rb', line 5

def self.ObjectBuilder(params)
					o = {}
    params["items"].each { |v|
	o[v[0]] = v[1]
  	}
  { "out" => o }
end

.output(params) ⇒ Object

For Composed modules



22
23
24
# File 'lib/simpleflow.rb', line 22

def self.output(params)
  {"out" => params["in"] }
end

.ping(params) ⇒ Object

Ping



6
7
8
9
# File 'lib/simpleflow/ping.rb', line 6

def self.ping(params)
	require 'ping'
	{ "out" => Ping.pingecho(params["hostname"], params["timeout"], params["service"]) }
end

.StringBuilder(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/simpleflow/string_builder.rb', line 5

def self.StringBuilder(params)
  list = params["liste"].dup
  params.each { |k,v|
    if !k.index('[').nil?
      index = k.split('[')[1].split(']')[0].to_i
      list[index]=v
    end
  }
  { "out" => list.join() }
end

.urlencode(params) ⇒ Object



4
5
6
7
# File 'lib/simpleflow/urlencode.rb', line 4

def self.urlencode(params)
 require 'cgi'
  { "out" => CGI::escape(params["in"]) }
end

.YQL(params) ⇒ Object

def self.YQL(params)

			require 'yajl'
			require 'cgi'

			if !params["query"].is_a?(String)
				return { "out" => "Error: expecting query as a string"}
			elsif params["query"] == ""
				return { "out" => "Error: empty query"}
			end
			
    require 'net/http'
    require 'uri'
    url = URI.parse('http://query.yahooapis.com')
    res = Net::HTTP.start(url.host, url.port) { |http|
      http.get('/v1/public/yql?q='+CGI::escape(params["query"])+'&format=json')
    }

			 parser = Yajl::Parser.new
			 hash = parser.parse(res.body)

    { "out" => hash }
  end


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
# File 'lib/simpleflow/yql.rb', line 34

def self.YQL(params)
	Simpleflow.run({
		"modules" => [

			{
				"name" => "input",
				"value" => {
					"input" => { "value" => 'show tables' , "name" => "query" }
				}
			},

			{
				"name" => "HTTP",
				"value" => {
						"method"=>"GET",
						"url"=> 'http://query.yahooapis.com/v1/public/yql',
						"urlparams"=>[
							["format","json"],
							["q", 		""], # the [1,1] element is wired
							["env", "store://datatables.org/alltableswithkeys"]
						]
				}
			},

			{	"name" => "output","value" => {"in" => '', "name" => "out"}	}

		],
		"wires" => [
			{
				"src" => {"moduleId" => 0 , "terminal" => "out"},
				"tgt" => {"moduleId" => 1 , "terminal" => "urlparams[1][1]"}
			},
			{
				"src" => {"moduleId" => 1 , "terminal" => "out"},
				"tgt" => {"moduleId" => 2 , "terminal" => "in"}
			}
		]
	}, params)
end