active_worksheet 0.1.0

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
+ SHA256:
3
+ metadata.gz: a0031addc268eedea37cafe6cdb9c612c0ce330627e1acb8cc4ca6ef78ff1cc1
4
+ data.tar.gz: 11399a3608f0922f4a8f775bc7e189e1297f07486b9c64ef77237e11ac6d9acc
5
+ SHA512:
6
+ metadata.gz: 747c598cde14bbb44a4cdb13095abf2e2a18369bee90cf8393514c3fcc97418a3d3288516a9e5be1024971b34ba6b7bedfa7472801ccd2c287a5c543c6977f8a
7
+ data.tar.gz: c81eff853c1fb37494f1499383e7c016a798179e58e9b5be3351994156d6363a6d8eae652689455e21793334300dd62831b05117a8818ba02071316775302966
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,162 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/**/*
4
+ - node_modules/**/*
5
+ - log/**/*
6
+ - tmp/**/*
7
+ - vendor/**/*
8
+ - db/migrate/*
9
+ - db/schema.rb
10
+ DisabledByDefault: true
11
+
12
+ Layout/AccessModifierIndentation:
13
+ EnforcedStyle: indent
14
+
15
+ Layout/BlockAlignment:
16
+ EnforcedStyleAlignWith: either
17
+
18
+ Layout/DefEndAlignment:
19
+ EnforcedStyleAlignWith: start_of_line
20
+
21
+ Layout/DotPosition:
22
+ EnforcedStyle: leading
23
+
24
+ Layout/EmptyComment:
25
+ Enabled: true
26
+
27
+ Layout/EmptyLineBetweenDefs:
28
+ Enabled: true
29
+ NumberOfEmptyLines: 1
30
+
31
+ Layout/EmptyLinesAroundBlockBody:
32
+ EnforcedStyle: no_empty_lines
33
+
34
+ Layout/EmptyLinesAroundClassBody:
35
+ EnforcedStyle: no_empty_lines
36
+
37
+ Layout/EmptyLinesAroundModuleBody:
38
+ EnforcedStyle: no_empty_lines
39
+
40
+ Layout/EndAlignment:
41
+ EnforcedStyleAlignWith: start_of_line
42
+
43
+ Layout/ExtraSpacing:
44
+ AllowForAlignment: true
45
+
46
+ Layout/IndentFirstArrayElement:
47
+ EnforcedStyle: consistent
48
+
49
+ Layout/IndentFirstHashElement:
50
+ EnforcedStyle: consistent
51
+
52
+ Layout/IndentationConsistency:
53
+ EnforcedStyle: normal
54
+
55
+ Layout/IndentationWidth:
56
+ Width: 2
57
+
58
+ Layout/SpaceAroundBlockParameters:
59
+ EnforcedStyleInsidePipes: no_space
60
+
61
+ Layout/SpaceAroundEqualsInParameterDefault:
62
+ EnforcedStyle: space
63
+
64
+ Layout/SpaceAroundOperators:
65
+ Enabled: true
66
+
67
+ Layout/SpaceBeforeBlockBraces:
68
+ EnforcedStyle: space
69
+
70
+ Layout/SpaceInLambdaLiteral:
71
+ EnforcedStyle: require_no_space
72
+
73
+ Layout/SpaceInsideBlockBraces:
74
+ EnforcedStyle: space
75
+ EnforcedStyleForEmptyBraces: no_space
76
+
77
+ Layout/SpaceInsideHashLiteralBraces:
78
+ EnforcedStyle: space
79
+ EnforcedStyleForEmptyBraces: no_space
80
+
81
+ Layout/SpaceInsideReferenceBrackets:
82
+ EnforcedStyle: no_space
83
+
84
+ Layout/SpaceInsideStringInterpolation:
85
+ EnforcedStyle: no_space
86
+
87
+ Layout/Tab:
88
+ Enabled: true
89
+
90
+ Layout/TrailingWhitespace:
91
+ Enabled: true
92
+
93
+ Lint/SafeNavigationChain:
94
+ Enabled: true
95
+
96
+ Lint/ShadowedArgument:
97
+ Enabled: true
98
+
99
+ Lint/UnusedBlockArgument:
100
+ Enabled: true
101
+
102
+ Lint/UnusedMethodArgument:
103
+ Enabled: true
104
+
105
+ Metrics/LineLength:
106
+ Max: 100
107
+ IgnoredPatterns: [(\A|\s)#]
108
+
109
+ Naming/VariableName:
110
+ EnforcedStyle: snake_case
111
+
112
+ Style/BlockDelimiters:
113
+ EnforcedStyle: line_count_based
114
+
115
+ Style/BracesAroundHashParameters:
116
+ EnforcedStyle: no_braces
117
+
118
+ Style/EmptyElse:
119
+ EnforcedStyle: both
120
+
121
+ Style/EmptyMethod:
122
+ EnforcedStyle: expanded
123
+
124
+ Style/GlobalVars:
125
+ AllowedVariables: ["$rollout"]
126
+
127
+ Style/HashSyntax:
128
+ EnforcedStyle: ruby19_no_mixed_keys
129
+
130
+ Style/MethodDefParentheses:
131
+ EnforcedStyle: require_parentheses
132
+
133
+ Style/NumericLiterals:
134
+ MinDigits: 5
135
+ Strict: false
136
+
137
+ Style/RedundantReturn:
138
+ Enabled: true
139
+
140
+ Style/Semicolon:
141
+ Enabled: true
142
+
143
+ Style/SingleLineMethods:
144
+ Enabled: true
145
+
146
+ Style/StabbyLambdaParentheses:
147
+ EnforcedStyle: require_parentheses
148
+
149
+ Style/StringLiterals:
150
+ EnforcedStyle: double_quotes
151
+
152
+ Style/StringLiteralsInInterpolation:
153
+ EnforcedStyle: single_quotes
154
+
155
+ Style/TrailingCommaInArguments:
156
+ EnforcedStyleForMultiline: no_comma
157
+
158
+ Style/TrailingCommaInArrayLiteral:
159
+ EnforcedStyleForMultiline: no_comma
160
+
161
+ Style/TrailingCommaInHashLiteral:
162
+ EnforcedStyleForMultiline: no_comma
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] 2020-02-08
4
+
5
+ ### Changed/Added
6
+ - Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hpneo@hotmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_worksheet.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,130 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_worksheet (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (6.0.2.1)
10
+ activesupport (= 6.0.2.1)
11
+ activemodel-serializers-xml (1.0.2)
12
+ activemodel (> 5.x)
13
+ activesupport (> 5.x)
14
+ builder (~> 3.1)
15
+ activeresource (5.1.0)
16
+ activemodel (>= 5.0, < 7)
17
+ activemodel-serializers-xml (~> 1.0)
18
+ activesupport (>= 5.0, < 7)
19
+ activesupport (6.0.2.1)
20
+ concurrent-ruby (~> 1.0, >= 1.0.2)
21
+ i18n (>= 0.7, < 2)
22
+ minitest (~> 5.1)
23
+ tzinfo (~> 1.1)
24
+ zeitwerk (~> 2.2)
25
+ addressable (2.7.0)
26
+ public_suffix (>= 2.0.2, < 5.0)
27
+ axlsx (3.0.0.pre)
28
+ htmlentities (~> 4.3, >= 4.3.4)
29
+ mimemagic (~> 0.3)
30
+ nokogiri (~> 1.8, >= 1.8.2)
31
+ rubyzip (~> 1.2, >= 1.2.1)
32
+ builder (3.2.4)
33
+ concurrent-ruby (1.1.5)
34
+ declarative (0.0.10)
35
+ declarative-option (0.1.0)
36
+ diff-lcs (1.3)
37
+ faraday (0.17.3)
38
+ multipart-post (>= 1.2, < 3)
39
+ google-api-client (0.36.4)
40
+ addressable (~> 2.5, >= 2.5.1)
41
+ googleauth (~> 0.9)
42
+ httpclient (>= 2.8.1, < 3.0)
43
+ mini_mime (~> 1.0)
44
+ representable (~> 3.0)
45
+ retriable (>= 2.0, < 4.0)
46
+ signet (~> 0.12)
47
+ google_drive (3.0.4)
48
+ google-api-client (>= 0.11.0, < 0.37.0)
49
+ googleauth (>= 0.5.0, < 1.0.0)
50
+ nokogiri (>= 1.5.3, < 2.0.0)
51
+ googleauth (0.10.0)
52
+ faraday (~> 0.12)
53
+ jwt (>= 1.4, < 3.0)
54
+ memoist (~> 0.16)
55
+ multi_json (~> 1.11)
56
+ os (>= 0.9, < 2.0)
57
+ signet (~> 0.12)
58
+ htmlentities (4.3.4)
59
+ httpclient (2.8.3)
60
+ i18n (1.8.2)
61
+ concurrent-ruby (~> 1.0)
62
+ json (2.3.0)
63
+ jwt (2.2.1)
64
+ memoist (0.16.2)
65
+ mimemagic (0.3.4)
66
+ mini_mime (1.0.2)
67
+ mini_portile2 (2.4.0)
68
+ minitest (5.14.0)
69
+ multi_json (1.14.1)
70
+ multipart-post (2.1.1)
71
+ nokogiri (1.10.7)
72
+ mini_portile2 (~> 2.4.0)
73
+ os (1.0.1)
74
+ public_suffix (4.0.3)
75
+ rake (10.4.2)
76
+ rchardet (1.8.0)
77
+ representable (3.0.4)
78
+ declarative (< 0.1.0)
79
+ declarative-option (< 0.2.0)
80
+ uber (< 0.2.0)
81
+ retriable (3.1.2)
82
+ rspec (3.8.0)
83
+ rspec-core (~> 3.8.0)
84
+ rspec-expectations (~> 3.8.0)
85
+ rspec-mocks (~> 3.8.0)
86
+ rspec-core (3.8.2)
87
+ rspec-support (~> 3.8.0)
88
+ rspec-expectations (3.8.4)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.8.0)
91
+ rspec-mocks (3.8.1)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.8.0)
94
+ rspec-support (3.8.2)
95
+ ruby-ole (1.2.12.2)
96
+ rubyzip (1.3.0)
97
+ signet (0.12.0)
98
+ addressable (~> 2.3)
99
+ faraday (~> 0.9)
100
+ jwt (>= 1.5, < 3.0)
101
+ multi_json (~> 1.10)
102
+ spreadsheet (1.2.6)
103
+ ruby-ole (>= 1.0)
104
+ thread_safe (0.3.6)
105
+ tzinfo (1.2.6)
106
+ thread_safe (~> 0.1)
107
+ uber (0.1.0)
108
+ workbook (0.8.1)
109
+ axlsx (~> 3.0.0.pre)
110
+ json (~> 2.1)
111
+ nokogiri (~> 1.8)
112
+ rchardet (~> 1.3)
113
+ spreadsheet (~> 1.1)
114
+ zeitwerk (2.2.2)
115
+
116
+ PLATFORMS
117
+ ruby
118
+
119
+ DEPENDENCIES
120
+ active_worksheet!
121
+ activeresource (= 5.1.0)
122
+ activesupport (= 6.0.2.1)
123
+ bundler (~> 2.0)
124
+ google_drive (= 3.0.4)
125
+ rake (~> 10.0)
126
+ rspec (~> 3.0)
127
+ workbook (= 0.8.1)
128
+
129
+ BUNDLED WITH
130
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 hpneo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # ActiveWorksheet
2
+
3
+ ActiveWorksheet reads local spreadsheet files (XLS, XLSX and CSV) and presents them as ActiveRecord/ActiveResource objects, mapping rows as records and columns and attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_worksheet'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_worksheet
20
+
21
+ ## Usage
22
+
23
+ Having the following CSV file:
24
+
25
+ ```csv
26
+ id,first_name,last_name,email,company
27
+ 1,Brendis,MacCahee,bmaccahee0@privacy.gov.au,Klocko and Sons
28
+ 2,Abbe,John,ajohn1@ibm.com,"Mayer, Weimann and Stark"
29
+ 3,Buffy,Schiersch,bschiersch2@chronoengine.com,"Gusikowski, Schmidt and Watsica"
30
+ 4,Flore,Aberhart,faberhart3@boston.com,Beatty Group
31
+ ```
32
+
33
+ ```ruby
34
+ class ImportedAccount < ActiveWorksheet::Base
35
+ self.source = "./accounts.csv"
36
+ end
37
+ ```
38
+
39
+ The `ImportedAccount` class will read the file as a spreadsheet and treat the first row as the collection's headers, where each column is an attribute name.
40
+
41
+ ```ruby
42
+ ImportedAccount.count
43
+ # => 4
44
+
45
+ ImportedAccount.first
46
+ # => #<ImportedAccount id=1, first_name="Brendis", last_name="MacCahee", email="bmaccahee0@privacy.gov.au", company="Klocko and Sons">
47
+ ```
48
+
49
+ ActiveWorksheet works with .xls, .xlsx and .csv files.
50
+
51
+ `ActiveWorksheet::Base` exposes the following class methods, similar to `ActiveRecord::Base` or `ActiveResource::Base`:
52
+ * `.all`
53
+ * `.find(index)`
54
+ * `.first`
55
+ * `.last`
56
+ * `.count`
57
+
58
+ ### Using Google Sheets (experimental)
59
+
60
+ ActiveWorksheet can connect to Google Sheets files by setting a URL as `source` and an `authorization` strategy.
61
+
62
+ ```ruby
63
+ class Savings < ActiveWorksheet::Base
64
+ self.source = "https://docs.google.com/spreadsheets/d/YOUR_GOOGLE_SHEETS_ID/edit"
65
+ self.authorization = {
66
+ credentials: Google::Auth::UserRefreshCredentials.new(
67
+ client_id: "CLIENT_ID",
68
+ client_secret: "CLIENT_SECRET",
69
+ refresh_token: "REFRESH_TOKEN",
70
+ type: "authorized_user"
71
+ )
72
+ }
73
+ end
74
+ ```
75
+
76
+ > **Note:** At the moment, all attributes are formatted strings (the same way they're represented in Google Sheets).
77
+
78
+ ```ruby
79
+ Savings.last.week
80
+ # => "52"
81
+
82
+ Savings.last.weekly_savings
83
+ # => "S/.1,300.00"
84
+ ```
85
+
86
+ #### Authorization
87
+
88
+ ActiveWorksheet supports the following authorization strategies:
89
+
90
+ ##### OAuth client ID (For "Other" Application type)
91
+
92
+ With a JSON file containing `client_id` and `client_secret`:
93
+
94
+ ```ruby
95
+ self.authorization = {
96
+ config: "YOUR_CONFIG_FILE_PATH.json",
97
+ }
98
+ ```
99
+
100
+ ##### OAuth client ID (For "Web application" Application type)
101
+
102
+ With a credentials object:
103
+
104
+ ```ruby
105
+ require "googleauth"
106
+
107
+ self.authorization = {
108
+ credentials: Google::Auth::UserRefreshCredentials.new(
109
+ client_id: "CLIENT_ID",
110
+ client_secret: "CLIENT_SECRET",
111
+ refresh_token: "USER_REFRESH_TOKEN",
112
+ type: "authorized_user"
113
+ )
114
+ }
115
+ ```
116
+
117
+ ##### Service account key
118
+
119
+ With a JSON file downloaded from Google Developer Console:
120
+
121
+ ```ruby
122
+ self.authorization = {
123
+ service_account_key: "my-service-account-xxxxxxxxxxxx.json",
124
+ }
125
+ ```
126
+
127
+ > **Note:** You can check available configuration strategies and a more thorough explanation [here](https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md).
128
+
129
+ ## Development
130
+
131
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
132
+
133
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hpneo/active_worksheet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
142
+
143
+ ## Code of Conduct
144
+
145
+ Everyone interacting in the ActiveWorksheet project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hpneo/active_worksheet/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "active_worksheet/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_worksheet"
7
+ spec.version = ActiveWorksheet::VERSION
8
+ spec.authors = ["Gustavo Leon"]
9
+ spec.email = ["hpneo@hotmail.com"]
10
+
11
+ spec.summary = %q{Worksheets as models.}
12
+ spec.description = %q{Work with local and remote worksheets like ActiveRecord/ActiveResource models.}
13
+ spec.homepage = "https://hpneo.dev/active_worksheet/"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/hpneo/active_worksheet"
20
+ spec.metadata["changelog_uri"] = "https://github.com/hpneo/active_worksheet/blob/master/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "activesupport", "6.0.2.1"
35
+ spec.add_development_dependency "activeresource", "5.1.0"
36
+ spec.add_development_dependency "workbook", "0.8.1"
37
+ spec.add_development_dependency "google_drive", "3.0.4"
38
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_worksheet"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "active_worksheet/version"
2
+ require "active_worksheet/base"
3
+
4
+ module ActiveWorksheet
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,38 @@
1
+ module ActiveWorksheet
2
+ module Adapters
3
+ class BaseAdapter
4
+ ADAPTER_NAME = "Base"
5
+
6
+ attr_accessor :source, :authorization
7
+
8
+ def initialize(source: nil, authorization: {})
9
+ @source = source
10
+ @authorization = authorization
11
+ end
12
+
13
+ def all
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def find
18
+ raise NotImplementedError
19
+ end
20
+
21
+ def first
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def last
26
+ raise NotImplementedError
27
+ end
28
+
29
+ def new
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def count
34
+ raise NotImplementedError
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ require_relative "./base_adapter"
2
+ require "workbook"
3
+ require "active_support/inflector"
4
+
5
+ module ActiveWorksheet
6
+ module Adapters
7
+ class FileAdapter < BaseAdapter
8
+ ADAPTER_NAME = "File"
9
+
10
+ def initialize(source: nil)
11
+ super(source: source)
12
+
13
+ @workbook = Workbook::Book.open(source)
14
+ end
15
+
16
+ def all
17
+ table = @workbook.sheet.table
18
+
19
+ headers = table.shift.map do |header|
20
+ ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.parameterize(header.value))
21
+ end
22
+
23
+ table.map do |row|
24
+ row.each_with_index.reduce({}) do |result, (cell, index)|
25
+ result[headers[index]] = cell.value
26
+ result
27
+ end
28
+ end
29
+ end
30
+
31
+ def find(index)
32
+ all[index]
33
+ end
34
+
35
+ def first
36
+ find(0)
37
+ end
38
+
39
+ def last
40
+ find(-1)
41
+ end
42
+
43
+ def count
44
+ all.count
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,64 @@
1
+ require_relative "./base_adapter"
2
+ require "google_drive"
3
+ require "active_support/inflector"
4
+
5
+ module ActiveWorksheet
6
+ module Adapters
7
+ class GoogleSheetsAdapter < BaseAdapter
8
+ ADAPTER_NAME = "GoogleSheets"
9
+
10
+ def initialize(source: nil, authorization: {})
11
+ super(source: source, authorization: authorization)
12
+ end
13
+
14
+ def all
15
+ session = build_session(authorization)
16
+ @workbook = session.spreadsheet_by_url(source)
17
+ table = @workbook.worksheets.first
18
+
19
+ headers = table.rows[0].map do |header|
20
+ ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.parameterize(header))
21
+ end
22
+
23
+ table.rows(1).map do |row|
24
+ row.each_with_index.reduce({}) do |result, (value, index)|
25
+ result[headers[index]] = value
26
+ result
27
+ end
28
+ end
29
+ end
30
+
31
+ def find(index)
32
+ all[index]
33
+ end
34
+
35
+ def first
36
+ find(0)
37
+ end
38
+
39
+ def last
40
+ find(-1)
41
+ end
42
+
43
+ def count
44
+ all.count
45
+ end
46
+
47
+ private
48
+
49
+ def build_session(authorization)
50
+ return GoogleDrive::Session.new_dummy if authorization.nil?
51
+
52
+ if authorization.has_key?(:access_token)
53
+ GoogleDrive::Session.login_with_oauth(authorization[:access_token])
54
+ elsif authorization.has_key?(:config)
55
+ GoogleDrive::Session.from_config(authorization[:config])
56
+ elsif authorization.has_key?(:credentials)
57
+ GoogleDrive::Session.from_credentials(authorization[:credentials])
58
+ elsif authorization.has_key?(:service_account_key)
59
+ GoogleDrive::Session.from_service_account_key(authorization[:service_account_key])
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,55 @@
1
+ require "active_worksheet/adapters/file_adapter"
2
+ require "active_worksheet/adapters/google_sheets_adapter"
3
+ require "active_resource/threadsafe_attributes"
4
+ require "ostruct"
5
+
6
+ module ActiveWorksheet
7
+ class Base < OpenStruct
8
+ class << self
9
+ include ThreadsafeAttributes
10
+
11
+ threadsafe_attribute :source, :authorization
12
+
13
+ def all
14
+ adapter.all.map do |row|
15
+ new(row)
16
+ end
17
+ end
18
+
19
+ def find(index)
20
+ new(adapter.find(index))
21
+ end
22
+
23
+ def first
24
+ new(adapter.first)
25
+ end
26
+
27
+ def last
28
+ new(adapter.last)
29
+ end
30
+
31
+ def count
32
+ adapter.count
33
+ end
34
+
35
+ def adapter
36
+ if is_source_local?
37
+ ActiveWorksheet::Adapters::FileAdapter.new(source: source)
38
+ else
39
+ ActiveWorksheet::Adapters::GoogleSheetsAdapter.new(
40
+ source: source,
41
+ authorization: authorization
42
+ )
43
+ end
44
+ end
45
+
46
+ def is_source_local?
47
+ self.source.is_a?(File) || File.exists?(self.source)
48
+ end
49
+
50
+ def is_source_remote?
51
+ !is_source_local?
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveWorksheet
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_worksheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gustavo Leon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-08 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 6.0.2.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 6.0.2.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: activeresource
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 5.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 5.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: workbook
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: google_drive
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.4
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.4
111
+ description: Work with local and remote worksheets like ActiveRecord/ActiveResource
112
+ models.
113
+ email:
114
+ - hpneo@hotmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - CHANGELOG.md
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - active_worksheet.gemspec
131
+ - bin/console
132
+ - bin/setup
133
+ - lib/active_worksheet.rb
134
+ - lib/active_worksheet/adapters/base_adapter.rb
135
+ - lib/active_worksheet/adapters/file_adapter.rb
136
+ - lib/active_worksheet/adapters/google_sheets_adapter.rb
137
+ - lib/active_worksheet/base.rb
138
+ - lib/active_worksheet/version.rb
139
+ homepage: https://hpneo.dev/active_worksheet/
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ allowed_push_host: https://rubygems.org
144
+ homepage_uri: https://hpneo.dev/active_worksheet/
145
+ source_code_uri: https://github.com/hpneo/active_worksheet
146
+ changelog_uri: https://github.com/hpneo/active_worksheet/blob/master/CHANGELOG.md
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 3.0.3
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Worksheets as models.
166
+ test_files: []