crawl_station 0.1.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.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +24 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +98 -0
  6. data/.travis.yml +9 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +14 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +43 -0
  11. data/Rakefile +6 -0
  12. data/benchmarks/.gitkeep +0 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/crawl_station.gemspec +32 -0
  16. data/examples/.gitkeep +0 -0
  17. data/exe/station +3 -0
  18. data/lib/crawl_station/application_record.rb +17 -0
  19. data/lib/crawl_station/cache.rb +13 -0
  20. data/lib/crawl_station/cache_adapters/abstract_adapter.rb +6 -0
  21. data/lib/crawl_station/cache_adapters/db_adapter.rb +6 -0
  22. data/lib/crawl_station/cache_adapters/memory_adapter.rb +17 -0
  23. data/lib/crawl_station/cli.rb +26 -0
  24. data/lib/crawl_station/command/create.rb +20 -0
  25. data/lib/crawl_station/command/generate.rb +88 -0
  26. data/lib/crawl_station/command.rb +26 -0
  27. data/lib/crawl_station/concerns/adapter_concern.rb +26 -0
  28. data/lib/crawl_station/configuration.rb +7 -0
  29. data/lib/crawl_station/fundation/parse_struct.rb +45 -0
  30. data/lib/crawl_station/launcher.rb +28 -0
  31. data/lib/crawl_station/logger.rb +12 -0
  32. data/lib/crawl_station/producer.rb +64 -0
  33. data/lib/crawl_station/ruby_version_check.rb +9 -0
  34. data/lib/crawl_station/schedule.rb +13 -0
  35. data/lib/crawl_station/schedule_adapters/abstract_adapter.rb +18 -0
  36. data/lib/crawl_station/schedule_adapters/db_adapter.rb +18 -0
  37. data/lib/crawl_station/schedule_adapters/memory_adapter.rb +21 -0
  38. data/lib/crawl_station/tasks/db.rake +33 -0
  39. data/lib/crawl_station/tasks/launcher.rake +6 -0
  40. data/lib/crawl_station/tasks/module.rake +6 -0
  41. data/lib/crawl_station/tasks.rb +3 -0
  42. data/lib/crawl_station/templates/create/Gemfile +11 -0
  43. data/lib/crawl_station/templates/create/Rakefile +2 -0
  44. data/lib/crawl_station/templates/create/config/boot.rb +6 -0
  45. data/lib/crawl_station/templates/create/config/initializers/station_config.rb +4 -0
  46. data/lib/crawl_station/templates/create/lib/tasks/.gitkeep +0 -0
  47. data/lib/crawl_station/templates/create/module/.gitkeep +0 -0
  48. data/lib/crawl_station/templates/generate/item.erb +9 -0
  49. data/lib/crawl_station/templates/generate/migration.erb +4 -0
  50. data/lib/crawl_station/templates/generate/module/config/.gitkeep +0 -0
  51. data/lib/crawl_station/templates/generate/module/db/migrate/.gitkeep +0 -0
  52. data/lib/crawl_station/templates/generate/module/item/.gitkeep +0 -0
  53. data/lib/crawl_station/templates/generate/module/parser/.gitkeep +0 -0
  54. data/lib/crawl_station/templates/generate/module/tasks/.gitkeep +0 -0
  55. data/lib/crawl_station/templates/generate/parser.erb +10 -0
  56. data/lib/crawl_station/utils.rb +36 -0
  57. data/lib/crawl_station/version.rb +3 -0
  58. data/lib/crawl_station.rb +152 -0
  59. metadata +199 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6d109fe2c385fdce5228dc5804145edb350025f
4
+ data.tar.gz: a2ccadba7a2f6965a49c7bca5e0cb9f46739dc6d
5
+ SHA512:
6
+ metadata.gz: 68440f18e400e3800b08363973fd989210b2c06ab74888b8f5827085fb01008f76907af84c4bb3e7b2c6221f5eadac31fdf0cc150cf086a9d5c6d16798aaa48e
7
+ data.tar.gz: 67253f586d2e836f1265126af5ba999da119a66462fb0957372a0e5e41f10803c27062489325bdb362ee8993d31c2ad7a9bb41807c5aa5e7ceb48e7a5f0d71bb
data/.codeclimate.yml ADDED
@@ -0,0 +1,24 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ - javascript
9
+ - python
10
+ - php
11
+ fixme:
12
+ enabled: true
13
+ rubocop:
14
+ enabled: true
15
+ ratings:
16
+ paths:
17
+ - "**.inc"
18
+ - "**.js"
19
+ - "**.jsx"
20
+ - "**.module"
21
+ - "**.py"
22
+ - "**.rb"
23
+ exclude_paths:
24
+ - spec/
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,98 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
4
+ # to ignore them, so only the ones explicitly set in this file are enabled.
5
+ DisabledByDefault: true
6
+ Exclude:
7
+ - '**/templates/**/*'
8
+ - '**/vendor/**/*'
9
+
10
+ # Prefer &&/|| over and/or.
11
+ Style/AndOr:
12
+ Enabled: true
13
+
14
+ # Do not use braces for hash literals when they are the last argument of a
15
+ # method call.
16
+ Style/BracesAroundHashParameters:
17
+ Enabled: true
18
+
19
+ # Align `when` with `case`.
20
+ Style/CaseIndentation:
21
+ Enabled: true
22
+
23
+ # Align comments with method definitions.
24
+ Style/CommentIndentation:
25
+ Enabled: true
26
+
27
+ # No extra empty lines.
28
+ Style/EmptyLines:
29
+ Enabled: true
30
+
31
+ # In a regular class definition, no empty lines around the body.
32
+ Style/EmptyLinesAroundClassBody:
33
+ Enabled: true
34
+
35
+ # In a regular module definition, no empty lines around the body.
36
+ Style/EmptyLinesAroundModuleBody:
37
+ Enabled: true
38
+
39
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
40
+ Style/HashSyntax:
41
+ Enabled: true
42
+
43
+ # Method definitions after `private` or `protected` isolated calls need one
44
+ # extra level of indentation.
45
+ Style/IndentationConsistency:
46
+ Enabled: true
47
+ EnforcedStyle: rails
48
+
49
+ # Two spaces, no tabs (for indentation).
50
+ Style/IndentationWidth:
51
+ Enabled: false
52
+
53
+ # Defining a method with parameters needs parentheses.
54
+ Style/MethodDefParentheses:
55
+ Enabled: true
56
+
57
+ # Use `foo {}` not `foo{}`.
58
+ Style/SpaceBeforeBlockBraces:
59
+ Enabled: true
60
+
61
+ # Use `foo { bar }` not `foo {bar}`.
62
+ Style/SpaceInsideBlockBraces:
63
+ Enabled: true
64
+
65
+ # Use `{ a: 1 }` not `{a:1}`.
66
+ Style/SpaceInsideHashLiteralBraces:
67
+ Enabled: true
68
+
69
+ # Check quotes usage according to lint rule below.
70
+ Style/StringLiterals:
71
+ Enabled: true
72
+ EnforcedStyle: single_quotes
73
+
74
+ # Detect hard tabs, no hard tabs.
75
+ Style/Tab:
76
+ Enabled: true
77
+
78
+ # Blank lines should not have any spaces.
79
+ Style/TrailingBlankLines:
80
+ Enabled: true
81
+
82
+ # No trailing whitespace.
83
+ Style/TrailingWhitespace:
84
+ Enabled: true
85
+
86
+ # Use quotes for string literals when they are enough.
87
+ Style/UnneededPercentQ:
88
+ Enabled: true
89
+
90
+ # Align `end` with the matching keyword or starting expression except for
91
+ # assignments, where it should be aligned with the LHS.
92
+ Lint/EndAlignment:
93
+ Enabled: true
94
+ AlignWith: variable
95
+
96
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
97
+ Lint/RequireParentheses:
98
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: required
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.4
6
+ cache: bundler
7
+ script:
8
+ - bundle exec rake
9
+ - bundle exec codeclimate-test-reporter
@@ -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 watsy0007@gmail.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,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crawl_station.gemspec
4
+ gemspec
5
+
6
+ gem 'thor'
7
+ gem 'celluloid', github: 'celluloid/celluloid', submodules: true
8
+ gem 'wombat', github: 'watsy0007/wombat'
9
+
10
+ group :test do
11
+ gem 'byebug'
12
+ gem 'simplecov'
13
+ gem 'codeclimate-test-reporter', '1.0.0.pre.rc2'
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 watsy0007
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,43 @@
1
+ # CrawlStation
2
+
3
+ [![Build Status](https://travis-ci.org/watsy0007/crawl_station.svg?branch=master)](https://travis-ci.org/watsy0007/crawl_station)
4
+ [![Code Climate](https://codeclimate.com/github/watsy0007/crawl_station/badges/gpa.svg)](https://codeclimate.com/github/watsy0007/crawl_station)
5
+ [![Test Coverage](https://codeclimate.com/github/watsy0007/crawl_station/badges/coverage.svg)](https://codeclimate.com/github/watsy0007/crawl_station/coverage)
6
+ [![Issue Count](https://codeclimate.com/github/watsy0007/crawl_station/badges/issue_count.svg)](https://codeclimate.com/github/watsy0007/crawl_station)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/watsy0007/crawl_station.svg)](https://gemnasium.com/github.com/watsy0007/crawl_station)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'crawl_station'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install crawl_station
24
+
25
+ ## Usage
26
+
27
+ [![asciicast](https://asciinema.org/a/90711.png)](https://asciinema.org/a/90711)
28
+
29
+ ## Development
30
+
31
+ 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.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/crawl_station. 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.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
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
File without changes
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'crawl_station'
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
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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crawl_station/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'crawl_station'
8
+ s.version = CrawlStation::VERSION
9
+ s.authors = ['watsy0007']
10
+ s.email = ['watsy0007@gmail.com']
11
+
12
+ s.summary = 'crawl station system'
13
+ s.description = 'crawl station system'
14
+ s.homepage = 'https://github.io/watsy0007/crawl_sation'
15
+ s.license = 'MIT'
16
+
17
+ s.required_ruby_version = '>= 2.3.0'
18
+
19
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features|lib\/exe)/})
21
+ end
22
+ s.bindir = 'exe'
23
+ s.executables = ['station']
24
+ s.require_paths = ['lib']
25
+
26
+ s.add_runtime_dependency 'activesupport', '~> 5.0', '>= 5.0.0'
27
+ s.add_runtime_dependency 'activerecord', '~> 5.0', '>= 5.0.0'
28
+ s.add_development_dependency 'bundler', '~> 1.13'
29
+ s.add_development_dependency 'rake', '~> 11.3'
30
+ s.add_development_dependency 'rspec', '~> 3.5'
31
+ s.add_development_dependency 'pry', '~> 0.10'
32
+ end
data/examples/.gitkeep ADDED
File without changes
data/exe/station ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'crawl_station/cli'
@@ -0,0 +1,17 @@
1
+ module CrawlStation
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+
5
+ def self.table_name_prefix
6
+ "#{module_name}_"
7
+ end
8
+
9
+ def self.database_config
10
+ CS::Utils.database_config(module_name)
11
+ end
12
+
13
+ def self.module_name
14
+ name.to_s.split('::').first.downcase
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module CrawlStation
2
+ module Cache
3
+ extend self
4
+ extend Forwardable
5
+ include Concerns::AdapterConcern
6
+
7
+ def adapter=(adapter_name)
8
+ custom_adapter(adapter_name, CrawlStation::CacheAdapters)
9
+ end
10
+
11
+ def_delegators :@adapter, :[]=, :[], :include?
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module CrawlStation
2
+ module CacheAdapters
3
+ class AbstractAdapter
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module CrawlStation
2
+ module CacheAdapters
3
+ class DbAdapter < AbstractAdapter
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ module CrawlStation
2
+ module CacheAdapters
3
+ class MemoryAdapter < AbstractAdapter
4
+ def initialize
5
+ @cache = {}
6
+ @cache.extend(MonitorMixin)
7
+ ['[]=', '[]', 'include?'].each do |method_name|
8
+ define_singleton_method method_name do |*args|
9
+ @cache.synchronize do
10
+ @cache.send(method_name, *args)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ require 'crawl_station/ruby_version_check'
2
+ Signal.trap('INT') { puts; exit(1) }
3
+ require 'crawl_station'
4
+
5
+ if ARGV.size > 0
6
+ command = ARGV.shift
7
+
8
+ aliases = {
9
+ 'new' => 'create',
10
+ 'g' => 'generate'
11
+ }
12
+ command = aliases[command] || command
13
+ CrawlStation::Command.invoke(command, ARGV)
14
+ exit(0)
15
+ end
16
+
17
+ puts "usage:\n"
18
+ %w(
19
+ create
20
+ generate
21
+ ).each do |command|
22
+ klass = "#{CrawlStation::Command}::#{command.capitalize}".camelize.constantize
23
+ klass.tasks.each do |k, v|
24
+ puts "#{k}: #{v.usage}\n\t#{v.description}\n"
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ require 'fileutils'
2
+ module CrawlStation
3
+ module Command
4
+ class Create < Thor
5
+ desc 'create station', 'station [create|new] hello'
6
+ def create(args)
7
+ dir_root = args.first
8
+ puts "create project #{dir_root}"
9
+ template_create_path = "#{CrawlStation::Utils.templates_path}/create"
10
+ FileUtils.copy_entry template_create_path, dir_root
11
+ puts "cd #{dir_root}"
12
+ path = "#{Dir.pwd}/#{dir_root}"
13
+ Dir.chdir(path)
14
+ puts 'bundle install'
15
+ IO.popen('bundle install').each { |line| puts line.chomp }
16
+ puts 'done'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,88 @@
1
+ require 'erb'
2
+ require 'fileutils'
3
+ module CrawlStation
4
+ module Command
5
+ class Generate < Thor
6
+ desc 'generation migration', 'station g migration t66y create_tech_migration'
7
+ def migration(args)
8
+ raise "generate #{args} error" if !args.is_a?(Array) || args.size < 2
9
+ module_name, file_name = args.shift, args.shift
10
+
11
+ file_path = dest_path(module_name, file_name)
12
+ template_path = CS::Utils.template_filepath('generate/migration.erb')
13
+ render(file_path, template_path, class_name: file_name.camelize)
14
+ logs "generate migration #{module_name}:#{file_name} done"
15
+ end
16
+
17
+ desc 'create parser module', 'station g module t66y'
18
+ def new_module(args)
19
+ raise "create module #{args} error" if !args.is_a?(Array) || args.empty?
20
+ module_name = args.shift
21
+ m_path = CS::Utils.module_path(module_name)
22
+ return logs("#{module_name} module exist!") if File.exist?(m_path)
23
+ logs "create new module #{module_name}"
24
+ template_m_path = "#{CS::Utils.templates_path}/generate/module"
25
+ FileUtils.copy_entry template_m_path, m_path
26
+ logs "create #{module_name} done"
27
+ end
28
+
29
+ desc 'create parser', 'station g parser t66y tech'
30
+ def parser(args)
31
+ raise "generate parser #{args} error" if !args.is_a?(Array) || args.size < 2
32
+ module_name, parser_name = args.shift, args.shift
33
+ logs "create #{module_name} parser #{parser_name}"
34
+ template_parser_path = CS::Utils.template_filepath('generate/parser.erb')
35
+ template_item_path = CS::Utils.template_filepath('generate/item.erb')
36
+ opts = {
37
+ module_class_name: module_name.camelize,
38
+ class_name: parser_name.camelize
39
+ }
40
+ render(parser_path(module_name, parser_name, 'parser'), template_parser_path, opts)
41
+ render(parser_path(module_name, parser_name, 'item'), template_item_path, opts)
42
+ end
43
+
44
+ desc 'create parsers', 'station g parser t66y tech image japan'
45
+ def parsers(args)
46
+ raise "generate parser #{args} error" if !args.is_a?(Array) || args.size < 2
47
+ module_name = args.shift
48
+ loop do
49
+ break if args.empty?
50
+ parser([module_name, args.shift])
51
+ end
52
+ end
53
+
54
+ protected
55
+
56
+ def render(file_path, template_path, opts = {})
57
+ File.open(file_path, 'w') do |f|
58
+ logs "create file #{file_path}"
59
+ f.write render_context(template_path, opts)
60
+ end
61
+ end
62
+
63
+ def render_context(path, opts = {})
64
+ template = IO.read(path)
65
+ ERB.new(template).result(OpenStruct.new(opts).instance_eval { binding })
66
+ end
67
+
68
+ def logs(msg)
69
+ CrawlStation.logger.debug msg
70
+ end
71
+
72
+ def dest_path(module_name, file_name)
73
+ m_path = CrawlStation::Utils.module_path(module_name)
74
+ raise "module: #{module_name} not exist" unless Dir.exist?(m_path)
75
+ migrate_path = "#{m_path}/db/migrate"
76
+ raise "module: migration path #{migrate_path} not exist" unless Dir.exist?(migrate_path)
77
+ ms = Time.now.to_f.to_s.delete '.'
78
+ "#{migrate_path}/#{ms}_#{file_name}.rb"
79
+ end
80
+
81
+ def parser_path(module_name, file_name, type = 'parser')
82
+ m_path = CrawlStation::Utils.module_path(module_name)
83
+ raise "module: #{module_name} not exist" unless Dir.exist?(m_path)
84
+ "#{m_path}/#{type}/#{file_name}.rb"
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,26 @@
1
+ module CrawlStation
2
+ module Command
3
+ class CommandError < Exception
4
+ end
5
+ extend self
6
+ ALIASES = { 'module' => 'new_module' }
7
+
8
+ def invoke(command, args)
9
+ klass = "CrawlStation::Command::#{command.capitalize}".camelize.constantize
10
+ m_name = method_name(command, args)
11
+ obj = klass.new
12
+ return obj.send(m_name, args) if obj.respond_to?(m_name)
13
+ true
14
+ rescue NameError => e
15
+ Logger.warn "#{command}: #{args} not exist"
16
+ rescue Errno::ENOENT, RuntimeError => e
17
+ Logger.warn "#{e.message}\n#{e.backtrace[0..10].join("\n")}"
18
+ end
19
+
20
+ def method_name(command, args)
21
+ return 'create' if command.to_s == 'create'
22
+ name = args.shift
23
+ ALIASES[name] || name
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/concern'
2
+ module CrawlStation
3
+ module Concerns
4
+ module AdapterConcern
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def adapter
9
+ return @adapter if @adapter
10
+ self.adapter = :memory
11
+ @adapter
12
+ end
13
+
14
+ def custom_adapter(adapter_name, adapter_module)
15
+ case adapter_name
16
+ when String, Symbol
17
+ adapter_full_name = "#{adapter_name}_adapter"
18
+ @adapter = adapter_module.const_get(adapter_full_name.camelize).new
19
+ end
20
+ rescue NameError => e
21
+ CS.logger.error "Cache missing adapter #{adapter_name}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module CrawlStation
2
+ module Configuration
3
+ extend self
4
+ attr_accessor :adapter
5
+ attr_accessor :parsers
6
+ end
7
+ end