lib/scope.rb in expressive-0.0.26 vs lib/scope.rb in expressive-0.0.27

- old
+ new

@@ -1,7 +1,8 @@ module Expressive require 'json' + require "cgi" class Scope attr_reader :lookups def initialize(parent = {}) @parent = parent @@ -87,28 +88,44 @@ scope[cells.first.text_value] << addition end end syntax('$hash') do |scope, cells| - puts "$hash..." hash = {} cells.each do |cell| key = cell.elements[1].elements[0].elements[1].text_value value = cell.elements[1].elements[1].elements[1].instance_eval{eval(scope)} - puts value.class hash[key] = value end hash end + syntax('$hval') do |scope, cells| + key = cells[0].eval(scope) + hash = cells[1].eval(scope) + hash[key] + end + syntax('$lookup') {|scope, cells| perform_lookup(scope, cells)} syntax('lookup') {|scope, cells| perform_lookup(scope, cells)} syntax('post') do |scope, cells| perform_webhook(:post, scope, cells) end + syntax('$concat') do |scope, cells| + perform_concat(scope, cells) + end + + syntax('$sms') do |scope, cells| + perform_sms(scope, cells) + end + + syntax('$split') do |scope, cells| + perform_split(scope, cells) + end + syntax('put') do |scope, cells| perform_webhook(:put, scope, cells) end syntax('get') do |scope, cells| @@ -165,10 +182,43 @@ h.delete_if{|k, v| v.kind_of?(Expressive::Function) || v.kind_of?(Expressive::ExtendedValue)} end private + def perform_concat(scope, cells) + return cells[0].eval(scope) + cells[1].eval(scope) + end + + def perform_sms(scope, cells) + uri = URI.parse("http://api.clickatell.com/http/sendmsg") + + query = {:user => ENV["clickatell_username"], :password => ENV["clickatell_password"], :api_id => ENV['clickatell_api_id']} + + to = cells[0].eval(scope) + message = CGI.escape(cells[1].eval(scope)) + if to[0] == "0" + to = "44" + to[1..-1] + elsif to[0] == "+" + to = to[1..-1] + end + query[:to] = to + query[:text] = message + uri.query = query.map{|k,v| "#{k}=#{v}"}.join("&") + begin + response = Webhook.new(:get, uri.to_s, {}, {}).execute + scope = scope.merge(response) if response.is_a?(Hash) + rescue RestClient::Exception => e + scope['_errors'] = e.message + end + scope + + end + + def perform_split(scope, cells) + return cells[0].elements[1].text_value.split(cells[1].elements[1].text_value) + end + #(post "http://example.com" "*" (headers "AUTH-TOKEN=13415") ) #(post "http://example.com" name email john smith (headers "AUTH-TOKEN=13415") ) def perform_round(*args) tuple = args.first tuple[0].to_f.round(tuple[1]) @@ -188,18 +238,27 @@ end end def perform_webhook(verb, scope, cells) #convert cells to array so it can manipulated easily - cells_array = cells.map{|c| c.text_value.gsub(/\A"|"\Z/, '')} + cells_array = cells.map{|c| c.eval(scope).kind_of?(Hash) || !!c.eval(scope) == c.eval(scope) ? c.eval(scope) : c.text_value.gsub(/\A"|"\Z/, '')} + if !cells_array[2].nil? and !!cells_array[2] == cells_array[2] + update_case = cells_array[2] + else + update_case = true + end url = cells_array.shift url = Expressive.run(url, scope) if url =~ /\A\(.*\)\Z/ headers = create_webhook_headers(cells_array) params = create_webhook_parameters(scope, cells_array) begin response = Webhook.new(verb, url, params, headers).execute - scope = scope.merge(response) if response.is_a?(Hash) + if update_case + scope = scope.merge(response) if response.is_a?(Hash) + else + return response + end rescue RestClient::Exception => e scope['_errors'] = e.message end scope end @@ -218,9 +277,11 @@ end def create_webhook_parameters(scope, cells) if cells.first && cells.first == '*' scope.to_hash + elsif cells.first.kind_of? Hash + cells.first else cells.inject({}) do |params, key| params[key] = scope[key] params end