sape 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa574ed4c40af1747da53671206439a965f58d62
4
- data.tar.gz: cccb9aa17fdffe735d12459d4324f1a31ebb1da4
3
+ metadata.gz: 6bb0853e31de1b10077c70056900dd4ce842964b
4
+ data.tar.gz: 7b3ad0343f91a2597dee1364b48f363a37742b62
5
5
  SHA512:
6
- metadata.gz: 9161b2e93cd973d729cdeeaf9f7085b97ef6fc0f19f801dc1ae82b960ae50c300c5812ea06f90d01d793462d1975b089dbf2f28d07490af185bd77d17e881016
7
- data.tar.gz: f2dedcc6c60a0a9e426579af34ed640190dd5095fe8fab3d7d23a23f83ef917375a133c1c9add04138c9bb5e16de36cfb583ab8624042b8f66c342805a56f452
6
+ metadata.gz: 0f2933e1e758f069b46a4a6aefdb1a95c92f297b8fe117ac70702b9b4354d6f7d1580f21b9f6ab8cfa9c51fee658e40e8ed25591fade75573a95cdd1e24c30d1
7
+ data.tar.gz: 902a0313a2e93fee8373a5455da86d2a5a1e69fb245ce3d57ed0acdbd8c63f4f01752c120afe1887164c2d3120aae23bf1ed385a4384328de37c03ca5aa90808
data/README.md CHANGED
@@ -25,7 +25,10 @@ gem 'sape'
25
25
  * create config/sape.yml
26
26
  ```yml
27
27
  sape_user: _YOUR_SAPE_HASH_
28
- host: example.com
28
+ host:
29
+ - example.com
30
+ - subdomain1.example.com
31
+ - subdomain1.example.com
29
32
  charset: UTF-8
30
33
  ```
31
34
 
@@ -1,7 +1,7 @@
1
1
  module SapeHelper
2
2
  def sape_links_block
3
3
  request.original_fullpath.chomp!("/") if request.original_fullpath.last == "/" && request.original_fullpath != '/'
4
- options = { links: SapeLink.where(page: request.original_fullpath, link_type: "simple") }
4
+ options = { links: SapeLink.where(page: request.original_fullpath, link_type: "simple", site_host: request.host) }
5
5
  if SapeConfig.bot_ips.include?(request.remote_addr)
6
6
  options.merge!(check_code: SapeConfig.check_code)
7
7
  end
@@ -13,7 +13,7 @@ module SapeHelper
13
13
 
14
14
  def sape_links
15
15
  request.original_fullpath.chomp!("/") if request.original_fullpath.last == "/" && request.original_fullpath != '/'
16
- links = SapeLink.where(page: request.original_fullpath, link_type: "simple").
16
+ links = SapeLink.where(page: request.original_fullpath, link_type: "simple", site_host: request.host).
17
17
  pluck(:raw_link).
18
18
  join(SapeConfig.delimiter)
19
19
 
@@ -24,7 +24,7 @@ module SapeHelper
24
24
 
25
25
  def sape_context_links(text)
26
26
  request.original_fullpath.chomp!("/") if request.original_fullpath.last == "/" && request.original_fullpath != '/'
27
- SapeLink.where(page: request.original_fullpath, link_type: "context").each do |link|
27
+ SapeLink.where(page: request.original_fullpath, link_type: "context", site_host: request.host).each do |link|
28
28
  text.sub!(link.anchor, link.raw_link)
29
29
  end
30
30
 
@@ -1,15 +1,15 @@
1
1
  class SapeConfig < ActiveRecord::Base
2
2
  class << self
3
3
  def bot_ips
4
- where(name: 'ip').pluck(:value)
4
+ where(name: 'ip', site_host: request.host).pluck(:value)
5
5
  end
6
6
 
7
7
  def check_code
8
- where(name: 'sape_new_url').first.try(:value) || " "
8
+ where(name: 'sape_new_url', site_host: request.host).first.try(:value) || " "
9
9
  end
10
10
 
11
11
  def delimiter
12
- where(name: 'sape_delimiter').first.try(:value) || " "
12
+ where(name: 'sape_delimiter', site_host: request.host).first.try(:value) || " "
13
13
  end
14
14
  end
15
15
  end
@@ -1,11 +1,13 @@
1
1
  class CreateSapeStorage < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :sape_configs do |t|
4
+ t.string :site_host
4
5
  t.string :name
5
6
  t.text :value, limit: 500
6
7
  end
7
8
 
8
9
  create_table :sape_links do |t|
10
+ t.string :site_host
9
11
  t.string :page
10
12
  t.string :anchor
11
13
  t.string :url
@@ -13,6 +15,6 @@ class CreateSapeStorage < ActiveRecord::Migration
13
15
  t.text :raw_link, limit: 500
14
16
  t.string :link_type
15
17
  end
16
- add_index :sape_links, [:link_type, :page]
18
+ add_index :sape_links, [:link_type, :page, :site_host]
17
19
  end
18
20
  end
data/lib/sape/fetcher.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require 'open-uri'
3
+ require 'domainatrix'
3
4
 
4
5
  class Fetcher
5
6
  class << self
@@ -8,15 +9,14 @@ class Fetcher
8
9
  puts text if Rails.env.development?
9
10
  end
10
11
 
11
- def get_data(config, link_type)
12
+ def get_data(config, link_type, site_host)
12
13
 
13
14
  sape_user = config['sape_user']
14
- host = config['host'].downcase
15
15
  charset = config['charset'] || 'utf-8'
16
16
  server = config['server'] || 'dispenser-01.sape.ru'
17
17
  links_type = {'simple' => 'code.php', 'context' => 'code_context.php'}
18
18
 
19
- url = "http://#{server}/#{links_type[link_type]}?user=#{sape_user}&host=#{host}&format=json&no_slash_fix=true"
19
+ url = "http://#{server}/#{links_type[link_type]}?user=#{sape_user}&host=#{site_host}&format=json&no_slash_fix=true"
20
20
 
21
21
  begin
22
22
  data = open(url)
@@ -27,8 +27,8 @@ class Fetcher
27
27
  JSON.parse(data.read)
28
28
  end
29
29
 
30
- def fetch_pages(pages, link_type, delete_old = false)
31
- SapeLink.delete_all if delete_old
30
+ def fetch_pages(pages, link_type, delete_old = false, site_host)
31
+ SapeLink.where(site_host: site_host).delete_all if delete_old
32
32
  say "Links:: #{link_type}"
33
33
  pages.each do |page_url, links|
34
34
  say "Page: #{page_url}"
@@ -37,15 +37,15 @@ class Fetcher
37
37
 
38
38
  anchor = item.css('a').text
39
39
  url = item.css('a').attr('href').text
40
- host = Domainatrix.parse(url).host
41
- SapeLink.create page: page_url, anchor: anchor, host: host, raw_link: link, url: url, link_type: link_type
42
- say " Added #{anchor} #{host} #{url}"
40
+ url_host = Domainatrix.parse(url).host
41
+ SapeLink.create site_host: site_host, page: page_url, anchor: anchor, host: url_host, raw_link: link, url: url, link_type: link_type
42
+ say " Added #{site_host} :: #{anchor} #{url_host} #{url}"
43
43
  end
44
44
  end
45
45
  end
46
46
 
47
- def fetch_config(config_data, bot_ips)
48
- SapeConfig.delete_all
47
+ def fetch_config(config_data, bot_ips, site_host)
48
+ SapeConfig.where(site_host: site_host).delete_all
49
49
  say "Ips:"
50
50
  bot_ips.each do |ip|
51
51
  SapeConfig.create name: 'ip', value: ip
@@ -53,7 +53,7 @@ class Fetcher
53
53
  end
54
54
  say "Config"
55
55
  config_data.each do |item, data|
56
- SapeConfig.create name: item, value: data
56
+ SapeConfig.create site_host: site_host, name: item, value: data
57
57
  say " Added #{item} = #{data}"
58
58
  end
59
59
  end
data/lib/sape/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sape
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/tasks/sape.rake CHANGED
@@ -3,26 +3,28 @@ namespace :sape do
3
3
  desc "Fetch links from server"
4
4
  task fetch: :environment do
5
5
  begin
6
- config = YAML.load_file('config/sape.yml')
6
+ config = YAML.load_file('config/sape.yml')
7
7
  rescue Errno::ENOENT
8
8
  fail "Config file not found (config/sape.yml)"
9
9
  end
10
+ [*config['host']].each do |site_host|
11
+ puts "Start work with: #{site_host}" if Rails.env.development?
10
12
 
11
- data = Fetcher.get_data(config, 'simple')
12
- data_context = Fetcher.get_data(config, 'context')
13
- configs, config_data = {}, {}
13
+ data = Fetcher.get_data(config, 'simple', site_host)
14
+ data_context = Fetcher.get_data(config, 'context', site_host)
15
+ configs, config_data = {}, {}
14
16
 
15
- pages = data['__sape_links__']
16
- pages_context = data_context['__sape_links__']
17
- bot_ips = data['__sape_ips__']
17
+ pages = data['__sape_links__']
18
+ pages_context = data_context['__sape_links__']
19
+ bot_ips = data['__sape_ips__']
18
20
 
19
- %W{sape_delimiter sape_show_only_block sape_page_obligatory_output sape_new_url}.each do |item|
20
- config_data[item] = data["__#{item}__"]
21
- end
22
-
23
- Fetcher.fetch_config(config_data, bot_ips) if pages.any?
24
- Fetcher.fetch_pages(pages, 'simple', true) if pages.any?
25
- Fetcher.fetch_pages(pages_context, 'context') if pages_context.any?
21
+ %W{sape_delimiter sape_show_only_block sape_page_obligatory_output sape_new_url}.each do |item|
22
+ config_data[item] = data["__#{item}__"]
23
+ end
26
24
 
25
+ Fetcher.fetch_config(config_data, bot_ips, site_host) if pages.any?
26
+ Fetcher.fetch_pages(pages, 'simple', true, site_host) if pages.any?
27
+ Fetcher.fetch_pages(pages_context, 'context', false, site_host) if pages_context.any?
28
+ end
27
29
  end
28
30
  end
data/spec/dummy/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
 
4
4
  # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5
- gem 'rails', '4.1.1'
5
+ gem 'rails', '4.1.8'
6
6
  # Use sqlite3 as the database for Active Record
7
7
  gem 'sqlite3'
8
8
  # Use SCSS for stylesheets
@@ -0,0 +1,6 @@
1
+ sape_user: abcdefg
2
+ host:
3
+ - kremlin.ru
4
+ - kremlin2.ru
5
+ - kremlin3.ru
6
+ charset: UTF-8
@@ -1,18 +1,20 @@
1
1
  class CreateSapeStorage < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :sape_configs do |t|
4
+ t.string :site_host
4
5
  t.string :name
5
- t.string :value
6
+ t.text :value, limit: 500
6
7
  end
7
8
 
8
9
  create_table :sape_links do |t|
10
+ t.string :site_host
9
11
  t.string :page
10
12
  t.string :anchor
11
13
  t.string :url
12
14
  t.string :host
13
- t.string :raw_link
15
+ t.text :raw_link, limit: 500
14
16
  t.string :link_type
15
17
  end
16
- add_index :sape_links, [:link_type, :page]
18
+ add_index :sape_links, [:link_type, :page, :site_host]
17
19
  end
18
20
  end
@@ -11,22 +11,24 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20140715193855) do
14
+ ActiveRecord::Schema.define(version: 20141225123124) do
15
15
 
16
16
  create_table "sape_configs", force: true do |t|
17
+ t.string "site_host"
17
18
  t.string "name"
18
- t.string "value"
19
+ t.text "value", limit: 500
19
20
  end
20
21
 
21
22
  create_table "sape_links", force: true do |t|
23
+ t.string "site_host"
22
24
  t.string "page"
23
25
  t.string "anchor"
24
26
  t.string "url"
25
27
  t.string "host"
26
- t.string "raw_link"
28
+ t.text "raw_link", limit: 500
27
29
  t.string "link_type"
28
30
  end
29
31
 
30
- add_index "sape_links", ["link_type", "page"], name: "index_sape_links_on_link_type_and_page"
32
+ add_index "sape_links", ["link_type", "page", "site_host"], name: "index_sape_links_on_link_type_and_page_and_site_host"
31
33
 
32
34
  end
@@ -1,3 +1,6 @@
1
1
  sape_user: abcdefg
2
- host: kremlin.ru
2
+ host:
3
+ - kremlin.ru
4
+ - kremlin2.ru
5
+ - kremlin3.ru
3
6
  charset: UTF-8
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Rodionov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -258,6 +258,7 @@ files:
258
258
  - spec/dummy/config/initializers/wrap_parameters.rb
259
259
  - spec/dummy/config/locales/en.yml
260
260
  - spec/dummy/config/routes.rb
261
+ - spec/dummy/config/sape.yml
261
262
  - spec/dummy/config/secrets.yml
262
263
  - spec/dummy/db/migrate/20140715193855_create_sape_storage.rb
263
264
  - spec/dummy/db/schema.rb
@@ -307,7 +308,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
307
308
  version: '0'
308
309
  requirements: []
309
310
  rubyforge_project:
310
- rubygems_version: 2.2.2
311
+ rubygems_version: 2.4.5
311
312
  signing_key:
312
313
  specification_version: 4
313
314
  summary: Sape.ru Ruby On Rails module
@@ -349,6 +350,7 @@ test_files:
349
350
  - spec/dummy/config/initializers/wrap_parameters.rb
350
351
  - spec/dummy/config/locales/en.yml
351
352
  - spec/dummy/config/routes.rb
353
+ - spec/dummy/config/sape.yml
352
354
  - spec/dummy/config/secrets.yml
353
355
  - spec/dummy/db/migrate/20140715193855_create_sape_storage.rb
354
356
  - spec/dummy/db/schema.rb