class HaystackRuby::Project

may consider making this a mixin instead

Attributes

haystack_version[RW]
name[RW]
url[RW]

Public Class Methods

new(name, config) click to toggle source
# File lib/haystack_ruby/project.rb, line 8
def initialize(name, config)
  @name = name
  @url = (config['secure']) ? 'https://' : 'http://'
  @url = "#{@url}#{config['base_url']}"
  @haystack_version = config['haystack_version']
  # expect to use basic auth
  if config['credentials'].present?
    @credentials = config['credentials']
   #for now at least, we fake the user object
  #expect to use scram
  else
    user = OpenStruct.new
    user.username = config['username']
    user.password = config['password']
    # @creds_path = config['credentials_path']
    # creds = YAML.load File.new(@creds_path).read
    # user.username = creds['username']
    # user.password = creds['password']
    authorize user
  end
end

Public Instance Methods

add_rec(params) click to toggle source

params is array of hashes: {name: xx, type: xx, value: xx}

# File lib/haystack_ruby/project.rb, line 128
def add_rec params
  grid = ["ver:\"#{@haystack_version}\" commit:\"add\""]
  grid << params.map{|p| p[:name]}.join(',')
  values = params.map do |p|
    p[:value] = "\"#{p[:value]}\"" if p[:type] == 'String'
    p[:value]
  end
  grid << values.join(',')
  res = commit grid 
  # return id of new rec
  res['rows'][0]['id']     
end
api_eval(expr_str) click to toggle source

this function will post expr_str exactly as encoded

# File lib/haystack_ruby/project.rb, line 64
def api_eval(expr_str)
  body = ["ver:\"#{@haystack_version}\""]
  body << "expr"
  body << '"'+expr_str+'"'
  res = self.connection.post('eval') do |req|
    req.headers['Content-Type'] = 'text/zinc'
    req.body = body.join("\n")
  end
  JSON.parse! res.body
end
authorize(user) click to toggle source
# File lib/haystack_ruby/project.rb, line 30
def authorize user
  auth_conv = HaystackRuby::Auth::Scram::Conversation.new(user, @url)
  auth_conv.authorize
  @auth_token = auth_conv.auth_token
  raise HaystackRuby::Error, "scram authorization failed" unless @auth_token.present?
end
commit(grid) click to toggle source

www.skyfoundry.com/doc/docSkySpark/Ops#commit grid is array of strings

# File lib/haystack_ruby/project.rb, line 117
def commit grid
  puts 'grid = '
  pp grid.join "\n"
  res = self.connection.post('commit') do |req|
    req.headers['Content-Type'] = 'text/zinc'
    req.body = grid.join "\n"
  end
  JSON.parse! res.body
end
connection() click to toggle source

for now, setting up to have a single connection per project

# File lib/haystack_ruby/project.rb, line 39
def connection
  # if @credentials.nil? && @auth_token.nil?
  #   authorize #will either set auth token or raise error
  # end
  @connection ||= Faraday.new(:url => @url) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    faraday.headers['Authorization'] = @auth_token.present? ? "BEARER authToken=#{@auth_token}" : "Basic #@credentials"
    faraday.headers['Accept'] = 'application/json' #TODO enable more formats
  end
end
equip_point_meta() click to toggle source

return meta data for all equip with related points

# File lib/haystack_ruby/project.rb, line 76
def equip_point_meta
  begin
    equips = read({filter: '"equip"'})['rows']
    puts equips
    equips.map! do |eq|
      eq.delete('disMacro')
      eq['description'] = eq['id'].match(/[(NWTC)|(\$siteRef)] (.*)/)[1]
      eq['id'] = eq['id'].match(/:([a-z0-9\-]*)/)[1]
      eq['points'] = []
      read({filter: "\"point and equipRef==#{eq['id']}\""})['rows'].each do |p|
        p.delete('analytics')
        p.delete('disMacro')
        p.delete('csvUnit')
        p.delete('csvColumn')
        p.delete('equipRef')
        p.delete('point')
        p.delete('siteRef')

        p['id'] = p['id'].match(/:([a-z0-9\-]*)/)[1]
        p['name'] = p['navName']
        p.delete('navName')
        eq['points'] << p
      end
      eq
    end
  rescue Exception => e
    puts "error: #{e}"
    nil
  end
end
ops() click to toggle source
# File lib/haystack_ruby/project.rb, line 107
def ops
  JSON.parse!(self.connection.get("ops").body)['rows']
end
read(params) click to toggle source
# File lib/haystack_ruby/project.rb, line 52
def read(params)
  body = ["ver:\"#{@haystack_version}\""]
  body << params.keys.join(',')
  body << params.values.join(',')
  res = self.connection.post('read') do |req|
    req.headers['Content-Type'] = 'text/zinc'
    req.body = body.join("\n")
  end
  JSON.parse! res.body
end
remove_rec(id) click to toggle source

TODO fix these. weird sensitivities around mod timestamp (format and time) params is array of hashes: {name: xx, type: xx, value: xx} def update_rec id,params grid = [“ver:"#{@haystack_version}" commit:"update"”] grid << 'id,mod,' + params.map{|p| p}.join(',') values = params.map do |p|

p[:value] = "\"#{p[:value]}\"" if p[:type] == 'String'
p[:value]

end grid << “#{id},2017-01-09T17:21:31.197Z UTC,#{values.join(',')}” puts “dumping grid #{grid}”

commit grid end

# File lib/haystack_ruby/project.rb, line 156
def remove_rec id
  grid = ["ver:\"#{@haystack_version}\" commit:\"remove\""]
  grid << 'id,mod' 
  grid << "#{id},#{DateTime.now}"
  commit grid      
end
valid?() click to toggle source
# File lib/haystack_ruby/project.rb, line 111
def valid?
  !(@name.nil? || @haystack_version.nil? || @url.nil?)
end