<commit_before>
# Quick look at replacing Net::HTTP::Post calls with bash curl calls

#require 'net/http'
#require 'uri'
require 'json'

write_token = '<KEY>'

def json_header
  { 'Content-Type' =>'application/json', 'Authorization' => write_token }
end

data = {
      'objectTags' => [ 'cyber-dojo' ],
      'actionTags' => [ 'create' ],
      'dateTime' => '2015-06-25T09:11:15-00:00',
      'location' => { 
        'lat'  => 'LAT', # ??
        'long' => 'LONG' # ??
      },
      'properties' => {
        'dojo-id' => '96DA91B46B',
        'exercise-name' => 'Fizz_Buzz',
        'language-name' => 'C#',
        'test-name'     => 'NUnit'
      }
}

streams_url = 'https://api.1self.co/v1/streams'
stream_id = 'GSYZNQSYANLMWEEH'

#url = URI.parse("#{streams_url}/#{stream_id}/events")    
#request = Net::HTTP::Post.new(url.path, json_header)
#request.body = data.to_json
#result = Net::HTTP.new(url.host).request(request)
#p result


curl = 'curl' +
  ' --silent' +
  ' --header content-type:application/json' + 
  " --header authorization:#{write_token}" +
  ' -X POST' +
  " -d '#{data.to_json}'" +
  " #{streams_url}/#{stream_id}/events"

p curl
#output = `#{curl} &`
#es = $?.exitstatus    
#p es if es != 0
#p output if output != ""

#http://ujihisa.blogspot.co.uk/2010/03/how-to-run-external-command.html
pid = Process::spawn(curl) 
Process::wait(pid)
p $?


  
<commit_msg>scratch code to look into calling curl using Open4 gem<commit_after>
# Quick look at replacing Net::HTTP::Post calls with bash curl calls

#require 'net/http'
#require 'uri'
require 'json'
require 'open4'

write_token = '<KEY>'

def json_header
  { 'Content-Type' =>'application/json', 'Authorization' => write_token }
end

data = {
      'objectTags' => [ 'cyber-dojo' ],
      'actionTags' => [ 'create' ],
      'dateTime' => '2015-06-25T09:11:15-00:00',
      'location' => { 
        'lat'  => 'LAT', # ??
        'long' => 'LONG' # ??
      },
      'properties' => {
        'dojo-id' => '96DA91B46B',
        'exercise-name' => 'Fizz_Buzz',
        'language-name' => 'C#',
        'test-name'     => 'NUnit'
      }
}

streams_url = 'https://api.1self.co/v1/streams'
stream_id = 'GSYZNQSYANLMWEEH'

# Using Net::HTTP::Post
#
#url = URI.parse("#{streams_url}/#{stream_id}/events")    
#request = Net::HTTP::Post.new(url.path, json_header)
#request.body = data.to_json
#result = Net::HTTP.new(url.host).request(request)
#p result


curl = 'curl' +
  ' --silent' +
  ' --header content-type:application/json' + 
  " --header authorization:#{write_token}" +
  ' -X POST' +
  " -d '#{data.to_json}'" +
  " #{streams_url}/#{stream_id}/events"

p curl

# Using backtick
#
#output = `#{curl} &`
#es = $?.exitstatus    
#p es if es != 0
#p output if output != ""

# Using Process::spawn
#
#http://ujihisa.blogspot.co.uk/2010/03/how-to-run-external-command.html
#pid = Process::spawn(curl)
#Process::wait(pid)
#p $?


# Using Open4
#https://github.com/ahoward/open4

#pid,stdin,stdout,stderr = Open4::popen4 curl
#_,status = Process::waitpid2 pid

status = Open4::popen4(curl) do |_pid,stdin,stdout,stderr|
  p '<STDOUT>'
  p stdout.read.strip
  p '<STDERR>'
  p stderr.read.strip
end
p '<STATUS>'
p status.exitstatus

# This was giving 502 Bad Gateway

  

{'fraction_non_alphanumeric': 0.19747381392483057, 'fraction_numerical': 0.024029574861367836, 'mean_word_length': 3.2444444444444445, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 34, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3992369', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1255, 'n_words': 237}