Class: BBNW::Chrometa

Inherits:
Object
  • Object
show all
Includes:
Activities
Defined in:
lib/adapters/chrometa.rb

Instance Attribute Summary

Attributes included from Activities

#activities

Instance Method Summary collapse

Constructor Details

#initialize(email, token, clients, projects, start_date, end_date, debug = false) ⇒ Chrometa

Returns a new instance of Chrometa.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/adapters/chrometa.rb', line 14

def initialize(email, token, clients, projects, start_date, end_date, debug = false)
  @email      = email
  @token      = token
  @start_date = start_date.is_a?(String) ? Date.parse(start_date) : start_date
  @end_date   = end_date.is_a?(String) ? Date.parse(end_date) : end_date
  @debug      = debug

  @clients    = clients.is_a?(String) ? [clients] : clients
  @projects   = projects.is_a?(String) ? [projects] : projects

end

Instance Method Details

#get_activitiesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
94
95
# File 'lib/adapters/chrometa.rb', line 30

def get_activities
  activities   = []
  current_date = @start_date
  while current_date <= @end_date
    url = URI.parse("https://api.chrometa.com/api/report")
    params = {"date" => current_date.strftime("%m/%d/%Y")}
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth @email, @token
    req.set_form_data(params)
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp = http.start { |http| http.request(req) }

    body = resp.body.clone

    xml  = REXML::Document.new(body)
    log "==================="
    log "date  : #{current_date}"
    log "Body  : #{body}"
    log "==================="

    root = xml.elements["chrometa-info"]

    log "Email : #{root.attributes["email"]}"
    log "Date  : #{root.attributes["date"]}"

    root.elements.each do |client|
      log "  Client Name : #{client.attributes["name"]}"

      next unless (@clients.blank? || @clients.include?(client.attributes["name"]))

      client.elements.each do |project|
        log "    Project Name : #{project.attributes["name"]}"
        log "    Project Rate : #{project.attributes["rate"]}"

        next unless (@projects.blank? || @projects.include?(project.attributes["name"]))

        project.elements.each do |time_entry|
          start_time     = Time.parse(time_entry.attributes["timestamp"])
          end_time       = start_time + time_entry.attributes["seconds"].to_i

          activity_entry = BBNW::Activity.new(time_entry.attributes["activity"],
                                              start_time, end_time)
          activities << activity_entry
          log "      Application : #{time_entry.attributes['application']}"
          log "      Timestamp   : #{time_entry.attributes['timestamp']}"
          log "      Activity    : #{time_entry.attributes['activity']}"
          log "      URL         : #{time_entry.attributes['url']}"
          log "      Seconds     : #{time_entry.attributes['seconds']}"
          log "      Billable    : #{time_entry.attributes['billable']}"
        end

      end
    end
    current_date+=1
  end

  File.open('chrometa.csv', 'w') do |f|
    activities.each do |activity|
      f.puts("#{CGI.escapeHTML(activity.activity)},#{activity.start_time},#{activity.end_time}")
    end
  end

  activities
end

#log(message) ⇒ Object



26
27
28
# File 'lib/adapters/chrometa.rb', line 26

def log(message)
  puts message if @debug
end