Class: HaystackRuby::Project
- Inherits:
-
Object
- Object
- HaystackRuby::Project
- Defined in:
- lib/haystack_ruby/project.rb
Overview
may consider making this a mixin instead
Instance Attribute Summary collapse
-
#haystack_version ⇒ Object
required.
-
#name ⇒ Object
required.
-
#url ⇒ Object
required.
Instance Method Summary collapse
-
#add_rec(params) ⇒ Object
params is array of hashes: xx, type: xx, value: xx.
-
#api_eval(expr_str) ⇒ Object
this function will post expr_str exactly as encoded.
- #authorize(user) ⇒ Object
-
#commit(grid) ⇒ Object
www.skyfoundry.com/doc/docSkySpark/Ops#commit grid is array of strings.
-
#connection ⇒ Object
for now, setting up to have a single connection per project.
-
#equip_point_meta ⇒ Object
return meta data for all equip with related points.
-
#initialize(name, config) ⇒ Project
constructor
A new instance of Project.
- #ops ⇒ Object
- #read(params) ⇒ Object
-
#remove_rec(id) ⇒ Object
end.
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, config) ⇒ Project
Returns a new instance of Project.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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'] user end end |
Instance Attribute Details
#haystack_version ⇒ Object
required
7 8 9 |
# File 'lib/haystack_ruby/project.rb', line 7 def haystack_version @haystack_version end |
Instance Method Details
#add_rec(params) ⇒ Object
params is array of hashes: xx, type: xx, value: xx
128 129 130 131 132 133 134 135 136 137 138 139 |
# 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) ⇒ Object
this function will post expr_str exactly as encoded
64 65 66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/haystack_ruby/project.rb', line 30 def user auth_conv = HaystackRuby::Auth::Scram::Conversation.new(user, @url) auth_conv. @auth_token = auth_conv.auth_token raise HaystackRuby::Error, "scram authorization failed" unless @auth_token.present? end |
#commit(grid) ⇒ Object
www.skyfoundry.com/doc/docSkySpark/Ops#commit grid is array of strings
117 118 119 120 121 122 123 124 125 |
# 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 ⇒ Object
for now, setting up to have a single connection per project
39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 ⇒ Object
return meta data for all equip with related points
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 102 103 104 105 |
# File 'lib/haystack_ruby/project.rb', line 76 def 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 ⇒ Object
107 108 109 |
# File 'lib/haystack_ruby/project.rb', line 107 def ops JSON.parse!(self.connection.get("ops").body)['rows'] end |
#read(params) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# 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) ⇒ Object
end
156 157 158 159 160 161 |
# 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? ⇒ Boolean
111 112 113 |
# File 'lib/haystack_ruby/project.rb', line 111 def valid? !(@name.nil? || @haystack_version.nil? || @url.nil?) end |