Class: HaystackRuby::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/haystack_ruby/project.rb

Overview

may consider making this a mixin instead

Instance Attribute Summary collapse

Instance Method Summary collapse

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']
    authorize user
  end
end

Instance Attribute Details

#haystack_versionObject

required



7
8
9
# File 'lib/haystack_ruby/project.rb', line 7

def haystack_version
  @haystack_version
end

#nameObject

required



7
8
9
# File 'lib/haystack_ruby/project.rb', line 7

def name
  @name
end

#urlObject

required



7
8
9
# File 'lib/haystack_ruby/project.rb', line 7

def url
  @url
end

Instance Method Details

#add_rec(params) ⇒ Object

params is array of hashes: xx, type: xx, value: xx



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/haystack_ruby/project.rb', line 116

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

#authorize(user) ⇒ Object



30
31
32
33
34
35
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/haystack_ruby/project.rb', line 105

def commit grid
  puts 'grid = '
  pp grid.join "\n"
  res = self.connection.post('commit') do |req|
    req.headers['Content-Type'] = 'text/plain'
    req.body = grid.join "\n"
  end
  JSON.parse! res.body
end

#connectionObject

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_metaObject

return meta data for all equip with related points



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
# File 'lib/haystack_ruby/project.rb', line 64

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

#opsObject



95
96
97
# File 'lib/haystack_ruby/project.rb', line 95

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/plain'
    req.body = body.join("\n")
  end
  JSON.parse! res.body
end

#remove_rec(id) ⇒ Object

end



144
145
146
147
148
149
# File 'lib/haystack_ruby/project.rb', line 144

def remove_rec id
  grid = ["ver:\"#{@haystack_version}\" commit:\"remove\""]
  grid << 'id,mod' 
  grid << "#{id},#{DateTime.now}"
  commit grid      
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/haystack_ruby/project.rb', line 99

def valid?
  !(@name.nil? || @haystack_version.nil? || @url.nil?)
end