Class: WorkReport::RedmineActivity

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

Instance Method Summary (collapse)

Constructor Details

- (RedmineActivity) initialize(redmine_url, redmine_api_key, redmine_user_id)

Returns a new instance of RedmineActivity



104
105
106
107
108
# File 'lib/work_report.rb', line 104

def initialize(redmine_url, redmine_api_key, redmine_user_id)
  @redmine_url = redmine_url
  @redmine_api_key = redmine_api_key
  @redmine_user_id = redmine_user_id
end

Instance Method Details

- (Object) activities_to_csv



144
145
146
147
148
149
150
151
152
# File 'lib/work_report.rb', line 144

def activities_to_csv
  activities = activities_to_hash

  csv = CSV.generate do |line|
    activities.each do |activity|
      line << [activity["project"], activity["title"], activity["url"], activity["timestamp"].strftime("%F %T")]
    end
  end
end

- (Object) activities_to_hash



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/work_report.rb', line 110

def activities_to_hash
  url = "#{@redmine_url}/activity.atom?key=#{@redmine_api_key}&user_id=#{@redmine_user_id}"

  atom = open(url) do |f|
    f.read
  end

  doc = Nokogiri::XML.parse(atom, nil, "UTF-8")
  doc.remove_namespaces!

  activities = doc.xpath("/feed/entry").map do |entry|
    title = entry.at_xpath("title").to_str
    project = title.split(" - ", 2)[0]
    title = title.split(" - ", 2)[1]

    url = entry.at_xpath("link/@href").to_str

    timestamp = entry.at_xpath("updated").to_str
    #timestamp = Time.parse(timestamp).strftime("%Y-%m-%d %H:%M:%S")
    timestamp = Time.parse(timestamp)
    #ENV["TZ"] = "Asia/Tokyo"
    #timestamp = timestamp.localtime
    #ENV["TZ"] = "UTC"
    #timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")

    {
      "project" => project,
      "title" => title,
      "url" => url,
      "timestamp" => timestamp
    }
  end
end