apiify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 280b2e333c5d3fd4b6781c10a86b6a1ea1da0334
4
+ data.tar.gz: b9626d82aa248f83af83bc714bcea9407e8d194b
5
+ SHA512:
6
+ metadata.gz: 96924428e0d386d07c641cc275d3f05866a627895d608e63631959c785ac1fce8bde82896e5f4cbbcf3ff5c49540e895a33f7e68eb399a32b568804825596d3f
7
+ data.tar.gz: 3dd0185a479d664ed5f95b8ed0286e166e56bf774dfb362eb9cb2675bd2742eacc730ab15f65df1b989587df8a136c44b2571f20fd89b5eaf959cca4178cae7c
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ spec/fixtures/bunny.csv
11
+ *.DS_Store
12
+ apiify-0.1.0.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apiify.gemspec
4
+ gemspec
5
+ gem 'pry'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Oscar Barlow
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.
@@ -0,0 +1,73 @@
1
+ # apiify
2
+
3
+ apiify is a little gem that takes any .csv file and turns it into an API.
4
+
5
+ It's aimed at large organisations, e.g. government departments, that publish a lot of data as .csv and who want a quick way to access this data as JSON in their digital services.
6
+
7
+ ## Requirements
8
+ You will need to initialize a [Rails API-only application](http://edgeguides.rubyonrails.org/api_app.html) with a Postgres database, by running something like the following:
9
+
10
+ $ rails new <my-api> -d=postgresql --api
11
+
12
+ Then make your database with `$ bin/rake db:create`.
13
+
14
+ ## Installation
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'apiify'
19
+ ```
20
+
21
+ And install with `bundle`. Or install it yourself as:
22
+
23
+ $ gem install apiify
24
+
25
+ ## Usage
26
+
27
+ Locate the CSV file you want to turn into an API. The name of the CSV file will become the name of the database table you will create, so ensure it is named appropriately.
28
+
29
+ **Bad:**
30
+ * 2017-list_of_bunnies-FINALv2.csv
31
+
32
+ **Good:**
33
+ * bunny.csv
34
+
35
+ In your Rails API project directory, run `$ apiify new <path-to-csv> [index]`. This will run an API-appropriate scaffold via rails (i.e. a model, controller, and serializer will be created; so will a migration). The optional [index] argument takes the name of a column header and indexes that column in the database.
36
+
37
+ The model's properties will be derived from the CSV column headers, so ensure they are properly named. apiify auto-detects an appropriate property type based on the contents of each column.
38
+
39
+ **Bad:**
40
+ * sub_cat3
41
+
42
+ **Good:**
43
+ * breed_category
44
+
45
+ apiify will prompt you to run `$ bin/rake db:migrate`. Do so.
46
+
47
+ Then run `$ apiify import <path-to-csv>`. This will import each row in your CSV file as a new record in your database.
48
+
49
+ Run `$ bin/rails s` to boot up your rails server. Visit `localhost:3000/<pluralised model name>` to see your data in lovely, lovely JSON format.
50
+
51
+ Run `apiify help [COMMAND]` to see a list of commands and get help with their usage.
52
+
53
+ ## Features not supported
54
+ This gem currently does not work with databases other than Postgresql.
55
+
56
+ It currently does not support updating a database table based on a CSV input - it creates a new table each time the gem is used.
57
+
58
+ Please take a look at the list of known issues before using the gem.
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/oscar-barlow/apiify.
63
+
64
+ ## Authors
65
+ * [Chris Cooper](https://github.com/cjcoops)
66
+ * [Jenna Ramdenee](https://github.com/jennaramdenee)
67
+ * [Keomony Khun](https://github.com/keomony)
68
+ * [Oscar Barlow](https://github.com/oscar-barlow/)
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apiify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apiify"
8
+ spec.version = Apiify::VERSION
9
+ spec.authors = ["Chris Cooper", "Jenna Ramdenee", "Keomony Khun", "Oscar Barlow"]
10
+ spec.email = ["oscar.barlow@gmail.com"]
11
+
12
+ spec.summary = %q{takes a .csv file, churns out an API.}
13
+ spec.description = %q{apiify is a little gem that takes any .csv file and turns it into an API.
14
+
15
+ It's aimed at large organisations, e.g. government departments, that publish a lot of data as .csv and who want a quick way to access this data as JSON in their digital services.}
16
+ spec.homepage = "https://github.com/oscar-barlow/apiify"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
23
+ # else
24
+ # raise "RubyGems 2.0 or newer is required to protect against " \
25
+ # "public gem pushes."
26
+ # end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "cucumber", "~> 1.3"
39
+ spec.add_development_dependency "aruba", "~> 0.14.2"
40
+ spec.add_dependency "thor", "~> 0.19"
41
+ spec.add_runtime_dependency "active_model_serializers", "~> 0.10.0"
42
+
43
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "apiify"
4
+
5
+ Apiify::CLI.start( ARGV )
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apiify"
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
@@ -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,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'apiify/cli'
3
+
4
+ Apiify::CLI.start
@@ -0,0 +1,8 @@
1
+ require "apiify/version"
2
+ require 'apiify/scaffolder'
3
+ require 'apiify/csv_importer'
4
+ require 'CSV'
5
+ require 'thor'
6
+
7
+ module Apiify
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'thor'
2
+ require 'apiify'
3
+
4
+ class Apiify::CLI < Thor
5
+
6
+ desc "new <path-to-your-csv> [index]", "extracts file name and creates a model and properties based on CSV file name and header columns"
7
+ long_desc <<-APIIFYNEW
8
+ This will run an API-appropriate scaffold via rails (i.e. a model,
9
+ controller, and serializer will be created; so will a migration).
10
+
11
+ The optional [index] argument takes the name of a column header and indexes that column in the database.
12
+
13
+ IMPORTANT: the name of the model is derived from the file name. Therefore,
14
+ please ensure your file is sensibly named:
15
+ \x5
16
+ Good: `bunny.csv` \x5
17
+ Bad: `2017-list_of_bunnies-FINALv2.csv`
18
+
19
+ APIIFYNEW
20
+ def new(csv_path, index_col = nil)
21
+ scaffolder = Apiify::Scaffolder.new
22
+ scaffolder.generate(csv_path, index_col)
23
+ end
24
+
25
+ desc "import <path-to-your-csv>", "extracts all rows from CSV and populates your database"
26
+ long_desc <<-APIIFYIMPORT
27
+ This command imports every row in your CSV to a new row your database.
28
+
29
+ IMPORTANT: make sure you have already run `apiify new` with your CSV, or
30
+ import won't know where to put your data!
31
+ APIIFYIMPORT
32
+ def import(csv_path)
33
+ importer = Apiify::CsvImporter.new
34
+ importer.import(csv_path)
35
+ end
36
+
37
+
38
+ end
@@ -0,0 +1,32 @@
1
+ class Apiify::CsvImporter
2
+
3
+ def import(csv_path)
4
+ create_rake_file
5
+ run_rake_task(csv_path)
6
+ output_message(csv_path)
7
+ end
8
+
9
+ def create_rake_file
10
+ File.open("./lib/tasks/importer.rake", 'w') do |f|
11
+ f.write("require 'CSV'
12
+ task :import_csv, [:file_path] => :environment do |t, args|
13
+ file_name = args[:file_path].split('/').last.split('.').first
14
+ model_name = file_name.capitalize.constantize
15
+ table = CSV.table(args[:file_path])
16
+ table.each do |row|
17
+ model_name.create!(row.to_hash)
18
+ end
19
+ end")
20
+ end
21
+ end
22
+
23
+ def run_rake_task(csv_path)
24
+ system("bin/rake import_csv\[#{csv_path}\]")
25
+ end
26
+
27
+ def output_message(csv_path)
28
+ rows = CSV.table("#{csv_path}").length
29
+ puts "#{rows} records imported successfully."
30
+ end
31
+
32
+ end
@@ -0,0 +1,17 @@
1
+ require 'thor/group'
2
+ module Apiify
3
+ module Generators
4
+ class Rake < Thor::Group
5
+ include Thor::Actions
6
+
7
+ argument :group, :type => :string
8
+ argument :name, :type => :string
9
+
10
+ def create_group
11
+ empty_directory(group)
12
+ end
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,70 @@
1
+ class Apiify::Scaffolder
2
+
3
+ def generate(csv_path, index_col)
4
+ run_scaffold(csv_path, index_col)
5
+ confirm(csv_path, index_col)
6
+ end
7
+
8
+ def get_file_name(csv_path)
9
+ if is_a_csv?(csv_path)
10
+ get_file(csv_path).first
11
+ else
12
+ raise "Error: wrong file type. Please supply a .csv file"
13
+ end
14
+ end
15
+
16
+ def find_class(path)
17
+ table = CSV.table(path)
18
+ headers = table.headers
19
+ result = {}
20
+ headers.each do |header|
21
+ table.each do |row|
22
+ if row[header].class != Float
23
+ result[header] = row[header].class
24
+ else
25
+ result[header] = row[header].class
26
+ break
27
+ end
28
+ end
29
+ end
30
+ result
31
+ end
32
+
33
+ def hash_to_string(hash, index_col)
34
+ output_str = ""
35
+ hash.each do |key, value|
36
+ if !index_col.nil? && key.to_s == index_col
37
+ output_str += "#{key}:#{value.to_s.downcase}:index "
38
+ else
39
+ output_str += "#{key}:#{value.to_s.downcase} "
40
+ end
41
+ end
42
+ output_str.strip
43
+ end
44
+
45
+ def create_scaffold(csv_path, index_col)
46
+ model_name = get_file_name(csv_path).capitalize
47
+ hash_result = find_class(csv_path)
48
+ "bin/rails g scaffold #{model_name} #{hash_to_string(hash_result, index_col)}"
49
+ end
50
+
51
+ def run_scaffold(csv_path, index_col)
52
+ system(create_scaffold(csv_path, index_col))
53
+ end
54
+
55
+ def confirm(csv_path, index_col)
56
+ puts "Created #{get_file_name(csv_path).capitalize} model with properties #{hash_to_string(find_class(csv_path),index_col)}"
57
+ puts "Please run `bin/rake db:migrate`"
58
+ end
59
+
60
+ private
61
+
62
+ def is_a_csv?(csv_path)
63
+ get_file(csv_path).last == "csv"
64
+ end
65
+
66
+ def get_file(csv_path)
67
+ csv_path.split("/").last.split(".")
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ module Apiify
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apiify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Cooper
8
+ - Jenna Ramdenee
9
+ - Keomony Khun
10
+ - Oscar Barlow
11
+ autorequire:
12
+ bindir: exe
13
+ cert_chain: []
14
+ date: 2017-01-20 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.13'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.13'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '10.0'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '10.0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: rspec
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '3.0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: cucumber
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '1.3'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.3'
72
+ - !ruby/object:Gem::Dependency
73
+ name: aruba
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: 0.14.2
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: 0.14.2
86
+ - !ruby/object:Gem::Dependency
87
+ name: thor
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '0.19'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '0.19'
100
+ - !ruby/object:Gem::Dependency
101
+ name: active_model_serializers
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - "~>"
105
+ - !ruby/object:Gem::Version
106
+ version: 0.10.0
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: 0.10.0
114
+ description: |-
115
+ apiify is a little gem that takes any .csv file and turns it into an API.
116
+
117
+ It's aimed at large organisations, e.g. government departments, that publish a lot of data as .csv and who want a quick way to access this data as JSON in their digital services.
118
+ email:
119
+ - oscar.barlow@gmail.com
120
+ executables:
121
+ - apiify
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".DS_Store"
126
+ - ".gitignore"
127
+ - ".rspec"
128
+ - ".travis.yml"
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - apiify.gemspec
134
+ - bin/apiify
135
+ - bin/console
136
+ - bin/setup
137
+ - exe/apiify
138
+ - lib/apiify.rb
139
+ - lib/apiify/cli.rb
140
+ - lib/apiify/csv_importer.rb
141
+ - lib/apiify/generators/rake.rb
142
+ - lib/apiify/scaffolder.rb
143
+ - lib/apiify/version.rb
144
+ homepage: https://github.com/oscar-barlow/apiify
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.5.1
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: takes a .csv file, churns out an API.
168
+ test_files: []