Class: Tattle::Collector

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

Overview

Collector gathers the information about the platform on which tattle is being run in order to send it off to the tattle site.

Constant Summary collapse

KEY_FILE =
File.join(Gem.user_home, '.tattle/key')
REPORTING_URL =
if ENV['LOCAL']
  URI.parse('http://localhost:3000/reports/create')
else
  URI.parse('http://tattle.rubygarden.org/reports/create')
end
DESIRED_CONFIG_KEYS =
['target', 
'LIBRUBY', 
'prefix', 
'host_cpu', 
'arch', 
'host_os', 
'build',
'LIBRUBY_SO',
'ruby_install_name',
'target_cpu',
'host_vendor',
'SHELL']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.postObject

Post the report to REPORTING_URL



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tattle.rb', line 32

def self.post
  collector = self.new
  Net::HTTP.new(REPORTING_URL.host, REPORTING_URL.port).start do |http|
    params = collector.system_data.to_params + ($DEBUG ? '&debug=1' : '')
    response = http.post(REPORTING_URL.path, params)
    if $DEBUG
      p response 
      p response.body 
    end
    if !collector.key && (returned_key = response.body) =~ /^\s*[[:alnum:]]{64}\s*$/
      collector.key = returned_key.strip
    end
  end
end

.reportObject

Create a new instance, and call its report method



48
49
50
# File 'lib/tattle.rb', line 48

def self.report
  self.new.report
end

Instance Method Details

#keyObject

Read key (if available)



71
72
73
74
75
76
77
# File 'lib/tattle.rb', line 71

def key
  @key ||= begin
    FileUtils.mkdir(File.dirname(KEY_FILE)) rescue nil
    value = File.read(KEY_FILE) rescue nil
    (value.nil? || value.empty?) ? nil : value
  end
end

#key=(value) ⇒ Object

Write key



80
81
82
83
84
85
86
87
# File 'lib/tattle.rb', line 80

def key=(value)
  @key ||= begin
    FileUtils.mkdir_p File.dirname(KEY_FILE)
    File.open(KEY_FILE, 'w') do |f|
      f << value
    end
  end
end

#report(io = STDOUT) ⇒ Object

Report system_data to io, and return system_data



90
91
92
93
94
95
# File 'lib/tattle.rb', line 90

def report(io = STDOUT)
  system_data.each do |key, value|
    io.puts "#{key}, #{value}"
  end
  system_data
end

#system_dataObject

Collect the DESIRED_CONFIG_KEYS from Ruby’s Config hash, add date, Rubygems version and data read from KEY_FILE.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tattle.rb', line 56

def system_data
   data = DESIRED_CONFIG_KEYS.inject({}) do |hash, key|
     hash[key] = Config::CONFIG[key]
     hash
   end
   data['report_time'] = Time.now.to_s
   data['rubygems_version'] = Gem::RubyGemsVersion
   data['ruby_version'] = RUBY_VERSION
   data['user_key'] = key
   data.extend(StringExtensions)
   $stderr.puts data.inspect if $DEBUG
   data
end