harvest_report 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ chromedriver.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ ## harvest_report
2
+
3
+ Harvert reports for non-admin harvest users
4
+
5
+ ## Installation
6
+
7
+ gem install harvest_report
8
+
9
+ ## Usage
10
+
11
+ Usage: harvest_report [OPTIONS]
12
+ --email EMAIL your harvest email
13
+ --password PASSWORD your harvest password
14
+ --domain DOMAIN harvest domain
15
+ --start-date START_DATE report start date (e.g 2012-01-01)
16
+ --stop-date STOP_DATE report stop date (e.g 2012-01-31)
17
+ --directory DIRECTORY reports directory (default /tmp/harvest_report)
18
+ -h, --help Show this message
19
+
20
+ ## Example
21
+
22
+ $ harvest_report --email bob@example.com --password qwerty --domain mycompany --start-date 2012-01-01 --stop-date 2012-01-31
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'harvest_report'
4
+ HarvestReport::Runner.run
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "harvest_report"
6
+ s.version = "0.0.1"
7
+ s.authors = ["Andriy Yanko"]
8
+ s.email = ["andriy.yanko@gmail.com"]
9
+ s.homepage = "https://github.com/railsware/harvest_report"
10
+ s.summary = %q{Harvest reports for non-admin harvest users}
11
+
12
+ s.rubyforge_project = "harvest_report"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "capybara", "~>1.1.2"
20
+ end
@@ -0,0 +1,103 @@
1
+ require 'capybara'
2
+ require 'capybara/dsl'
3
+ require 'capybara/selenium/driver'
4
+ require 'cgi'
5
+ require 'optparse'
6
+
7
+ module HarvestReport
8
+ class Runner
9
+ include Capybara::DSL
10
+
11
+ def self.run(*args)
12
+ new(ARGV).run
13
+ end
14
+
15
+ def initialize(args)
16
+ @options = {
17
+ :download_dir => '/tmp/harvest_report',
18
+ :download_timeout => 60
19
+ }
20
+
21
+ parse_options!(args)
22
+ validate_options!
23
+ end
24
+
25
+ attr_reader :options
26
+
27
+ def run
28
+ Capybara.register_driver :chrome do |app|
29
+ profile = ::Selenium::WebDriver::Chrome::Profile.new
30
+ profile["download.default_directory"] = options[:download_dir]
31
+ Capybara::Selenium::Driver.new(app, :browser => :chrome, :profile => profile)
32
+ end
33
+ Capybara.current_driver = :chrome
34
+ Capybara.reset_sessions!
35
+
36
+ page.visit "https://#{options[:domain]}.harvestapp.com/account/login"
37
+ page.click_link "Or sign in with your Harvest account"
38
+
39
+ page.fill_in "Email", :with => options[:email]
40
+ page.fill_in "Password", :with => options[:password]
41
+ page.click_button "Sign In"
42
+
43
+ page.click_link "Reports"
44
+
45
+ element = page.find_link("My Profile")
46
+ user_id = element[:href].scan(/\d+/).first
47
+
48
+ export_url = "https://#{options[:domain]}.harvestapp.com/xlsx/export/user/#{user_id}"
49
+ export_url << "?"
50
+ export_url << "start_date=#{CGI.escape(options[:start_date])}&"
51
+ export_url << "end_date=#{CGI.escape(options[:stop_date])}"
52
+
53
+ page.visit export_url
54
+
55
+ wait_for_download
56
+
57
+ puts "Report downloaded to #{options[:download_dir]}"
58
+
59
+ sleep 1
60
+ end
61
+
62
+ protected
63
+
64
+ def parse_options!(args)
65
+ OptionParser.new do |opts|
66
+ opts.banner = "Usage: #{File.basename($0)} [OPTIONS]"
67
+
68
+ opts.on("--email EMAIL", "your harvest email") { |v| options[:email] = v }
69
+ opts.on("--password PASSWORD", "your harvest password") { |v| options[:password] = v }
70
+ opts.on("--domain DOMAIN", "harvest domain") { |v| options[:domain] = v }
71
+ opts.on("--start-date START_DATE", "report start date (e.g 2012-01-01)") { |v| options[:start_date] = v }
72
+ opts.on("--stop-date STOP_DATE", "report stop date (e.g 2012-01-31)") { |v| options[:stop_date] = v }
73
+ opts.on("--directory DIRECTORY", "reports directory (default #{options[:download_dir]})") { |v| options[:download_dir] = v }
74
+
75
+ opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
76
+ end.parse!(args)
77
+ end
78
+
79
+ def validate_options!
80
+ [:email, :password, :domain, :start_date, :stop_date].each do |name|
81
+ raise ArgumentError, "#{name.inspect} required" unless options[name]
82
+ end
83
+ end
84
+
85
+ def wait_for_download
86
+ Timeout.timeout(@download_timeout) do
87
+ sleep 0.1 until downloaded?
88
+ end
89
+ end
90
+
91
+ def downloaded?
92
+ !downloading? && downloads.any?
93
+ end
94
+
95
+ def downloading?
96
+ downloads.grep(/\.crdownload$/).any?
97
+ end
98
+
99
+ def downloads
100
+ Dir["#{@download_dir}/*"]
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module HarvestReport
2
+ autoload :Runner, "harvest_report/runner"
3
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: harvest_report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andriy Yanko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capybara
16
+ requirement: &23726620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *23726620
25
+ description:
26
+ email:
27
+ - andriy.yanko@gmail.com
28
+ executables:
29
+ - harvest_report
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - bin/harvest_report
38
+ - harvest_report.gemspec
39
+ - lib/harvest_report.rb
40
+ - lib/harvest_report/runner.rb
41
+ homepage: https://github.com/railsware/harvest_report
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project: harvest_report
61
+ rubygems_version: 1.8.10
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Harvest reports for non-admin harvest users
65
+ test_files: []
66
+ has_rdoc: