bstat2google 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be59fd1d31bacb83659c6fbcb5d6fec55e58981c
4
+ data.tar.gz: 5bcb20375abef5c4ce25e76b26d409baf7bb449a
5
+ SHA512:
6
+ metadata.gz: 90e3250595d79b47d2a62ec1f3fe305df329ffeec6d57a5058b0eddd12143608c4583d5b1e8c7d5d7d85ffe96ee3a2ec7918a8b0e57458c5a80be3d34dacb84f
7
+ data.tar.gz: b853b49fdc17d71b37c8c23c1dd2f19ec0ffd2e1c2e84f69a58cfdfae5e84a9dde4b35464339fa434e65feadd8842f14418a3405e12d3c3c19ebff56d7eab8cc
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bstat2google.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 EmergeAdapt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bstat2google
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bstat2google'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bstat2google
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/bstat2google ADDED
@@ -0,0 +1,21 @@
1
+ #! /usr/bin/env ruby
2
+ require File.expand_path('../../lib/bstat2google', __FILE__)
3
+ require "csv"
4
+
5
+ first_row = true
6
+ CSV.foreach(ARGV.first) do |bucket_id, sheet_id, worksheet_no, row, col, timestamp_cell, gmail, g_password, end_point, auth_token, *mapped_cols|
7
+ if first_row
8
+ first_row = false
9
+ next
10
+ end
11
+
12
+ api = Bstat2google::ApiClient.new auth_token: auth_token, end_point: end_point
13
+ source = Bstat2google::Source.new(api: api, bucket_ids: [bucket_id])
14
+ target = Bstat2google::GoogleTarget.new(sheet_id: sheet_id, gmail: gmail, password: g_password, row: row, col: col, worksheet_no: worksheet_no, mapped_cols: mapped_cols, timestamp_cell: timestamp_cell)
15
+
16
+
17
+ source.each do |bucket_stat|
18
+ target.write bucket_stat
19
+ end
20
+
21
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bstat2google/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bstat2google"
8
+ spec.version = Bstat2google::VERSION
9
+ spec.authors = ["Ijonas Kisselbach"]
10
+ spec.email = ["ijonas.kisselbach@gmail.com"]
11
+ spec.description = %q{Copies CaseBlocks Bucket Stats to Google Spreadsheets}
12
+ spec.summary = %q{Copies CaseBlocks Bucket Stats to Google Spreadsheets}
13
+ spec.homepage = "http://www.caseblocks.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "awesome_print", "~> 1.2.0"
24
+ spec.add_dependency "google_drive", "~> 0.3.6"
25
+ spec.add_dependency "rest-client", "~> 1.6.7"
26
+ end
@@ -0,0 +1,24 @@
1
+ class BucketStatsRequest
2
+ attr_accessor :api, :id
3
+
4
+ def initialize(attrs = {})
5
+ attrs.each {|k,v| self.send("#{k}=", v)}
6
+ end
7
+
8
+ def get
9
+ response = api.get url, content_type: :json, accept: :json
10
+ if response.code >= 200 and response.code < 300
11
+ return Response.new(code: response.code, payload: JSON.parse(response.body))
12
+ else
13
+ return Response.new(code: response.code, payload: response.description)
14
+ end
15
+ rescue Exception => e
16
+ puts e.message
17
+ puts e.backtrace.join("\n")
18
+ return Response.new(code: 0, payload: e.message)
19
+ end
20
+
21
+ def url
22
+ "#{api.end_point}/case_blocks/bucket_stats/#{id}.json?auth_token=#{api.auth_token}"
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ class Response
2
+ attr_accessor :code, :payload
3
+ def initialize(attrs = {})
4
+ attrs.each {|k,v| self.send("#{k}=", v)}
5
+ end
6
+ def successful?
7
+ code >= 200 and code < 300
8
+ end
9
+ def equal?(other)
10
+ !other.nil? && code.equal?(other.code) && payload.equal?(other.payload)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Bstat2google
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path('bstat2google/response', File.dirname(__FILE__))
2
+ require File.expand_path('bstat2google/bucket_stats_request', File.dirname(__FILE__))
3
+ require "time"
4
+ require "rubygems"
5
+ require "google_drive"
6
+ require "rest_client"
7
+ require "json"
8
+ require "awesome_print"
9
+
10
+ module Bstat2google
11
+
12
+ class BucketStats
13
+ attr_accessor :id, :kpis, :error
14
+ def initialize(attrs = {})
15
+ attrs[:kpis] = flatten_kpis_into_simple_hash(attrs.delete(:kpis))
16
+ attrs.each {|k,v| self.send("#{k}=", v)}
17
+ end
18
+ def flatten_kpis_into_simple_hash(kpi_tuples_array)
19
+ results = {}
20
+ kpi_tuples_array.each do |kpi_tuple|
21
+ results[kpi_tuple["term"]] = kpi_tuple["count"]
22
+ end
23
+ results
24
+ end
25
+ end
26
+
27
+ class ApiClient
28
+ attr_accessor :auth_token, :end_point, :rest_client
29
+ def initialize(attrs = {})
30
+ attrs.each {|k,v| self.send("#{k}=", v)}
31
+ @rest_client = RestClient unless rest_client
32
+ end
33
+ def get(url, options = {})
34
+ rest_client.get(url, options)
35
+ end
36
+ end
37
+
38
+ class Source
39
+ attr_accessor :api, :bucket_ids, :requester
40
+ def initialize(attrs = {})
41
+ attrs.each {|k,v| self.send("#{k}=", v)}
42
+ @requester = BucketStatsRequest unless requester
43
+ end
44
+
45
+ def each
46
+ if block_given?
47
+ Array(bucket_ids).each do |bucket_id|
48
+ req = requester.new api: api, id: bucket_id
49
+ response = req.get
50
+ if response.successful?
51
+ yield(BucketStats.new id: bucket_id, kpis: response.payload["bucket_stats"])
52
+ else
53
+ yield(BucketStats.new id: bucket_id, error: response.payload)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+
61
+ class Target
62
+ def write(bucket_stats)
63
+ ap bucket_stats
64
+ end
65
+ end
66
+
67
+ class GoogleTarget < Target
68
+ attr_accessor :sheet_id, :gmail, :password, :row, :col, :worksheet_no, :mapped_cols, :timestamp_cell
69
+ def initialize(attrs = {})
70
+ attrs.each {|k,v| self.send("#{k}=", v)}
71
+ end
72
+ def write(bucket_stats)
73
+ ap bucket_stats.kpis
74
+ session = GoogleDrive.login(gmail, password)
75
+ ws = session.spreadsheet_by_key(sheet_id).worksheets[worksheet_no.to_i]
76
+
77
+ puts "-------------------------"
78
+ mapped_cols.each_with_index do |kpi_term, i|
79
+ ws[row.to_i, col.to_i+i] = bucket_stats.kpis[kpi_term]
80
+ end
81
+ ws[timestamp_cell] = Time.now.strftime("%d/%m/%y %H:%M")
82
+ ws.save
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ require "ostruct"
3
+ require "json"
4
+
5
+ module Bstat2google
6
+
7
+ describe ApiClient do
8
+ before do
9
+ @api = ApiClient.new(auth_token: "test", end_point: "https://staging.caseblocks.com")
10
+ end
11
+
12
+ it "should initialize an API client" do
13
+ @api.auth_token.must_equal "test"
14
+ @api.end_point.must_equal "https://staging.caseblocks.com"
15
+ end
16
+ end
17
+
18
+ describe Source do
19
+
20
+ before do
21
+ @bucket_ids = [123, 456, 789]
22
+ @api = ApiClient.new(auth_token: "test", end_point: "https://staging.caseblocks.com")
23
+
24
+ @response = Response.new(code: 200, payload: {"a"=>10, "b"=>20, "c"=>30})
25
+ class MockBucketStatsRequest
26
+ def initialize(attrs = {}); end
27
+ def get; @response end
28
+ def successful?; true end
29
+ end
30
+
31
+ @mock_bucket_requester = MockBucketStatsRequest
32
+ @s = Source.new(api: @api, bucket_ids: @bucket_ids, requester: @mock_bucket_requester)
33
+ end
34
+
35
+ # it "should iterate over the list of buckets" do
36
+ # bucket_stat_processor = Minitest::Mock.new
37
+ # @bucket_ids.each {|id| bucket_stat_processor.expect :write, :nil, [@response]}
38
+ # @s.each do |bucket_stat|
39
+ # bucket_stat_processor.write(bucket_stat)
40
+ # end
41
+ # end
42
+ end
43
+
44
+ describe BucketStatsRequest do
45
+ before do
46
+ @mock_rest_client = Minitest::Mock.new
47
+ @api = ApiClient.new(auth_token: "test", end_point: "https://staging.caseblocks.com", rest_client: @mock_rest_client)
48
+ @req = BucketStatsRequest.new(api: @api, id: 3)
49
+ end
50
+
51
+ it "should call the API and return data if successful" do
52
+ expected_payload = {"a" => 10, "b" => 20, "c" => 30}
53
+ rest_response = OpenStruct.new(code: 200, body:JSON.generate(expected_payload))
54
+ @mock_rest_client.expect :get, rest_response, ["https://staging.caseblocks.com/case_blocks/bucket_stats/3.json?auth_token=test", {content_type: :json, accept: :json}]
55
+
56
+ response = @req.get
57
+ response.code.must_equal 200
58
+ response.payload.must_equal expected_payload
59
+ end
60
+
61
+ it "should call the API and return an error if unsuccesful" do
62
+ rest_response = OpenStruct.new(code: 301, description: "redirect")
63
+ @mock_rest_client.expect :get, rest_response, ["https://staging.caseblocks.com/case_blocks/bucket_stats/3.json?auth_token=test", {content_type: :json, accept: :json}]
64
+
65
+ response = @req.get
66
+ response.code.must_equal 301
67
+ response.payload.must_equal "redirect"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../../lib/bstat2google', __FILE__)
2
+ require "minitest/autorun"
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bstat2google
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ijonas Kisselbach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: awesome_print
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: google_drive
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.6
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.7
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.7
83
+ description: Copies CaseBlocks Bucket Stats to Google Spreadsheets
84
+ email:
85
+ - ijonas.kisselbach@gmail.com
86
+ executables:
87
+ - bstat2google
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/bstat2google
97
+ - bstat2google.gemspec
98
+ - lib/bstat2google.rb
99
+ - lib/bstat2google/bucket_stats_request.rb
100
+ - lib/bstat2google/response.rb
101
+ - lib/bstat2google/version.rb
102
+ - test/bstat2google_test.rb
103
+ - test/test_helper.rb
104
+ homepage: http://www.caseblocks.com
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Copies CaseBlocks Bucket Stats to Google Spreadsheets
128
+ test_files:
129
+ - test/bstat2google_test.rb
130
+ - test/test_helper.rb