Skip to content

Commit

Permalink
Merge 567e02d into dd805ec
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Dec 1, 2018
2 parents dd805ec + 567e02d commit 49477b5
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -31,13 +31,14 @@ REDIS_PORT = YOUR_REDIS_PORT
REDIS_PASSWORD = YOUR_REDIS_PASSWORD
```

### Run a Cron job
### Run Cron jobs

```sh
bundle exec ruby bin/cron_job.rb
bundle exec ruby bin/web_analyzer_job.rb
bundle exec ruby bin/whoisds_job.rb
```

- The job crawls WebAnalyzer websites and parsed the latest registered domains.
- The jobs collects the latest registered domains from WebAnalyzer & WhoisDS.
- It checks a suspicious score of a given each domain and stores a suspicious one into a Redis instance.

### Run a Web app
Expand Down
16 changes: 0 additions & 16 deletions bin/cron_job.rb

This file was deleted.

7 changes: 7 additions & 0 deletions bin/web_analyzer_job.rb
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift("#{__dir__}/../lib")

require "ayashige"

Ayashige::Jobs::WebAnalyzer.new.perform
7 changes: 7 additions & 0 deletions bin/whoisds_job.rb
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift("#{__dir__}/../lib")

require "ayashige"

Ayashige::Jobs::WhoisDS.new.perform
4 changes: 4 additions & 0 deletions lib/ayashige.rb
Expand Up @@ -13,6 +13,10 @@
require "ayashige/sources/web_analyzer"
require "ayashige/sources/whoisds"

require "ayashige/jobs/job"
require "ayashige/jobs/web_analyzer"
require "ayashige/jobs/whoisds"

require "ayashige/rollbar"

require "ayashige/application"
Expand Down
21 changes: 21 additions & 0 deletions lib/ayashige/jobs/job.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Ayashige
module Jobs
class Job
def perform
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

def with_error_handling
yield
rescue StandardError => e
if Ayashige::Rollbar.available?
Rollbar.error e
else
puts e
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/ayashige/jobs/web_analyzer.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Ayashige
module Jobs
class WebAnalyzer < Job
def initialize
@source = Ayashige::Sources::WebAnalyzer.new
end

def perform
with_error_handling { @source.store_newly_registered_domains }
end
end
end
end
15 changes: 15 additions & 0 deletions lib/ayashige/jobs/whoisds.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Ayashige
module Jobs
class WhoisDS < Job
def initialize
@source = Ayashige::Sources::WhoisDS.new
end

def perform
with_error_handling { @source.store_newly_registered_domains }
end
end
end
end
6 changes: 6 additions & 0 deletions spec/jobs/web_analyzer_spec.rb
@@ -0,0 +1,6 @@
# frozen_string_literal: true

RSpec.describe Ayashige::Jobs::WebAnalyzer, :vcr do
include_context "job context"
subject { Ayashige::Jobs::WebAnalyzer.new }
end
6 changes: 6 additions & 0 deletions spec/jobs/whoisds_spec.rb
@@ -0,0 +1,6 @@
# frozen_string_literal: true

RSpec.describe Ayashige::Jobs::WhoisDS, :vcr do
include_context "job context"
subject { Ayashige::Jobs::WhoisDS.new }
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -27,6 +27,12 @@
config.include Spec::Support::IOHelper
end

require_relative "./support/shared_contexts/job_context"

RSpec.configure do |rspec|
rspec.include_context "job context", include_shared: true
end

VCR.configure do |config|
config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
config.configure_rspec_metadata!
Expand Down
30 changes: 30 additions & 0 deletions spec/support/shared_contexts/job_context.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

RSpec.shared_context "job context", shared_context: :metadata do
describe "#perform" do
context "when no error is raised" do
before do
mock = double
allow(mock).to receive(:store_newly_registered_domains).and_return([])
subject.instance_variable_set(:@source, mock)
end

it "should return the latest indexed date as a String" do
output = capture(:stdout){ subject.perform }
expect(output.empty?).to eq(true)
end
end
context "when an error is raised" do
before do
mock = double
allow(mock).to receive(:store_newly_registered_domains).and_raise(ArgumentError, "argument error")
subject.instance_variable_set(:@source, mock)
end

it "should return the latest indexed date as a String" do
output = capture(:stdout) { subject.perform }
expect(output).to include("argument error")
end
end
end
end

0 comments on commit 49477b5

Please sign in to comment.