aranha-rails 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
+ SHA256:
3
+ metadata.gz: ea03bcdec3dd45159d259277c82e9caa7d6aa8469499abfff9567101a7d23e6f
4
+ data.tar.gz: e25011f06e74adc37e3192e6a5fa80e849430b4b8dd0ebc7ddb0e1af3cc7ff98
5
+ SHA512:
6
+ metadata.gz: 458bf075dec14c64b8c63065a81bfde264420940161f7d7475ee84bf1712a86ddb04dc10f45d06d346c0fe9c9963e19d1a756898cd02505c07fe078b017576b4
7
+ data.tar.gz: 6be3e41fb8f7abd3cf950bcfd55f9d0c36778c124d34e1cd0e12bd44ef5944864b164299358f176157488842228cf8d98650330718d6fa771cfa1b8b4bf468ed
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module Aranha
6
+ module Rails
7
+ require_sub __FILE__
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_scaffold'
4
+
5
+ module Aranha
6
+ module Rails
7
+ class Engine < ::Rails::Engine
8
+ isolate_namespace ::Aranha
9
+
10
+ initializer :append_migrations do |app|
11
+ config.paths['db/migrate'].expanded.each do |expanded_path|
12
+ app.config.paths['db/migrate'] << expanded_path
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/parsers/base'
4
+ require 'aranha/parsers/source_address'
5
+ require 'aranha/parsers/source_target_fixtures'
6
+
7
+ module Aranha
8
+ module Rails
9
+ class FixturesDownload
10
+ attr_reader :pending
11
+
12
+ def initialize(options)
13
+ @prefix = options.fetch(:prefix)
14
+ @prefix = '' if @prefix.blank?
15
+ @download = options.fetch(:download)
16
+ @pending = options.fetch(:pending)
17
+ end
18
+
19
+ def run
20
+ url_files.each do |f|
21
+ Rails.logger.info(relative_path(f))
22
+ download(url(f), target(f)) if @download
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def url_files
29
+ Dir["#{fixtures_root}/**/*.url"].select { |path| select_path?(path) }
30
+ end
31
+
32
+ def select_path?(path)
33
+ return false unless match_prefix_pattern(path)
34
+
35
+ !pending || !source_exist?(path)
36
+ end
37
+
38
+ def match_prefix_pattern(path)
39
+ relative_path(path).start_with?(@prefix)
40
+ end
41
+
42
+ def fixtures_root
43
+ Rails.root.to_s
44
+ end
45
+
46
+ def download(url, target)
47
+ Rails.logger.info "Baixando \"#{url}\"..."
48
+ content = ::Aranha::Parsers::Base.new(url).content
49
+ raise "Content is blank for \"#{url}\"" if content.blank?
50
+
51
+ File.open(target, 'wb') { |file| file.write(content) }
52
+ end
53
+
54
+ def url(file)
55
+ ::Aranha::Parsers::SourceAddress.from_file(file)
56
+ end
57
+
58
+ def target(file)
59
+ File.expand_path(File.basename(file, '.url') + '.source.html', File.dirname(file))
60
+ end
61
+
62
+ def relative_path(path)
63
+ path.sub(%r{^#{Regexp.quote(fixtures_root)}/}, '')
64
+ end
65
+
66
+ def source_exist?(path)
67
+ stf = ::Aranha::Parsers::SourceTargetFixtures.new(::File.dirname(path))
68
+ stf.source_file(::File.basename(path, '.url')).present?
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/manager'
4
+ require 'singleton'
5
+
6
+ module Aranha
7
+ module Rails
8
+ class Manager < ::Aranha::Manager
9
+ include ::Singleton
10
+
11
+ def add_address(url, processor, extra_data = nil)
12
+ a = ::Aranha::Address.find_or_initialize_by(url: ::Aranha::Address.sanitize_url(url))
13
+ a.processor = processor
14
+ a.extra_data = extra_data.to_yaml
15
+ a.save!
16
+ end
17
+
18
+ def addresses_count
19
+ ::Aranha::Address.count
20
+ end
21
+
22
+ def clear_expired_addresses
23
+ q = ::Aranha::Address.by_created_at_lt(Time.zone.now - 12.hours)
24
+ ::Rails.logger.info("Addresses expired: #{q.count}")
25
+ q.destroy_all
26
+ end
27
+
28
+ def log_info(message)
29
+ ::Rails.logger.info(message)
30
+ end
31
+
32
+ def log_warn(message)
33
+ ::Rails.logger.warn(message)
34
+ end
35
+
36
+ def unprocessed_addresses
37
+ ::Aranha::Address.unprocessed
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aranha
4
+ module Rails
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace(:aranha) do
4
+ desc 'Process Aranha\'s addresses.'
5
+ task process: :environment do
6
+ ::Aranha::Processor.new
7
+ end
8
+
9
+ desc 'Remove all registered Aranha\'s addresses.'
10
+ task clear: :environment do
11
+ Rails.logger.info("Addresses deleted: #{::Aranha::Address.destroy_all.count}")
12
+ end
13
+
14
+ namespace :fixtures do
15
+ desc 'Download remote content for fixtures.'
16
+ task download: :environment do
17
+ ::Aranha::Rails::FixturesDownload.new(
18
+ prefix: ENV['PREFIX'],
19
+ download: ENV['DOWNLOAD'].present?,
20
+ pending: ENV['PENDING'].present?
21
+ ).run
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aranha-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Put here the authors
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_scaffold
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.5.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.5.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: aranha
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.15'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: aranha-parsers
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.6'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.6'
61
+ - !ruby/object:Gem::Dependency
62
+ name: eac_rails_utils
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.12'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 0.12.1
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0.12'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 0.12.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: eac_ruby_utils
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0.52'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0.52'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rails
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 5.1.7
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 5.1.7
109
+ - !ruby/object:Gem::Dependency
110
+ name: eac_ruby_gem_support
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '0.1'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 0.1.2
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '0.1'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 0.1.2
129
+ description:
130
+ email:
131
+ executables: []
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - lib/aranha/rails.rb
136
+ - lib/aranha/rails/engine.rb
137
+ - lib/aranha/rails/fixtures_download.rb
138
+ - lib/aranha/rails/manager.rb
139
+ - lib/aranha/rails/version.rb
140
+ - lib/tasks/aranha.rake
141
+ homepage:
142
+ licenses: []
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubygems_version: 3.0.8
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Put here de description.
163
+ test_files: []