Skip to content

Commit

Permalink
Merge pull request #17 from ninoseki/switch-to-redis-hashes
Browse files Browse the repository at this point in the history
Switch to redis hashes
  • Loading branch information
ninoseki committed Nov 26, 2018
2 parents 98fb9d9 + 16a1416 commit a713854
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
13 changes: 6 additions & 7 deletions lib/ayashige/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ class Application < Sinatra::Base
content_type :json

array = []
data = Store.all
data.each do |updated_on, domains|
domains.each do |name|
domain = Domain.new(name)
hash = Store.all
hash.keys.each do |updated_on|
hash[updated_on].keys.each do |domain|
array << {
domain: domain.to_s,
score: domain.score,
updated_on: updated_on
updated_on: updated_on,
domain: domain,
score: hash.dig(updated_on, domain).to_i
}
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ayashige/sources/web_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def store_newly_registered_domains
domains = get_domains_from_doc(page)
break if domains.empty?

domains.each do |elem|
domain = Domain.new(elem[:domain])
updated = elem[:updated]
domains.each do |domain|
updated = domain[:updated]
domain = Domain.new(domain[:domain])
next unless domain.suspicious?

@store.store updated, domain.to_s
@store.store updated, domain.to_s, domain.score
puts "#{domain} is stored."
end
index += 1
Expand Down
10 changes: 6 additions & 4 deletions lib/ayashige/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
require "json"

module Ayashige
DEFAULT_TTL = 60 * 60 * 48

class Store
def initialize
@client = Redis.client
end

def store(updated, domain)
def store(key, field, value)
@client.multi do
@client.sadd updated, domain.to_s
@client.expire updated, 60 * 60 * 48
@client.hset key, field, value
@client.expire key, DEFAULT_TTL
end
end

Expand All @@ -20,7 +22,7 @@ def exists?(key)
end

def get(key)
@client.smembers(key)
@client.hgetall key
end

def keys
Expand Down
15 changes: 12 additions & 3 deletions spec/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ def app
describe "GET /feed" do
before do
allow(Ayashige::Store).to receive(:all).and_return(
"2018-02-01": ["paypal.pay.pay.com", "google.apple.microsoft.com"],
"2018-01-01": ["paypal.pay.pay.com", "google.apple.microsoft.com"],
"2018-03-01": ["paypal.pay.pay.com", "google.apple.microsoft.com"]
"2018-02-01" => {
"paypal.pay.pay.com" => 80,
"google.apple.microsoft.com" => 80
},
"2018-01-01" => {
"paypal.pay.pay.com" => 80,
"google.apple.microsoft.com" => 80
},
"2018-03-01" => {
"paypal.pay.pay.com" => 80,
"google.apple.microsoft.com" => 80
}
)
end

Expand Down
8 changes: 7 additions & 1 deletion spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
describe "#available?" do
context "when ROLLBAR_ACCESS_TOKEN is set" do
before do
allow(ENV).to receive(:key?).with("ROLLBAR_ACCESS_TOKEN").and_return(true)
allow(ENV).to receive(:key?).with(subject::ROLLBAR_KEY).and_return(true)
end

it "should return true" do
expect(subject.available?).to eq(true)
end
end

context "when ROLLBAR_ACCESS_TOKEN is not set" do
before do
allow(ENV).to receive(:key?).with(subject::ROLLBAR_KEY).and_return(false)
end

it "should return false" do
expect(subject.available?).to eq(false)
end
Expand Down
23 changes: 12 additions & 11 deletions spec/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

describe "#store" do
it "should store arguments as a Set" do
subject.store "2018-01-01", "test.com"
expect(redis.smembers("2018-01-01")).to eq(["test.com"])
subject.store "2018-01-01", "test2.com"
expect(redis.smembers("2018-01-01")).to eq(["test2.com", "test.com"])
subject.store "2018-01-01", "test.com", 80
expect(redis.hgetall("2018-01-01")).to eq("test.com" => "80")
end
end

describe "#get" do
it "should return a Set value" do
redis.sadd "2018-01-01", "test.com"
redis.sadd "2018-01-01", "test2.com"
expect(subject.get("2018-01-01")).to eq(["test2.com", "test.com"])
redis.hset "2018-01-01", "test.com", 80
redis.hset "2018-01-01", "test2.com", 80
expect(subject.get("2018-01-01")).to eq(
"test.com" => "80",
"test2.com" => "80"
)
end
end

Expand All @@ -44,13 +45,13 @@

describe "#all" do
before do
redis.sadd "2018-01-01", "test.com"
redis.sadd "2018-01-02", "test2.com"
redis.hset "2018-01-01", "test.com", 80
redis.hset "2018-01-02", "test2.com", 80
end
it "should return all data as a Hash" do
expect(subject.all).to eq(
"2018-01-01" => ["test.com"],
"2018-01-02" => ["test2.com"]
"2018-01-01" => { "test.com" => "80" },
"2018-01-02" => { "test2.com" => "80" }
)
end
end
Expand Down

0 comments on commit a713854

Please sign in to comment.