neuron-client 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -8
- data/lib/neuron-client.rb +61 -12
- data/lib/neuron-client/{connection.rb → admin_connection.rb} +7 -7
- data/lib/neuron-client/api.rb +48 -31
- data/lib/neuron-client/membase_connection.rb +18 -0
- data/lib/neuron-client/model/admin/ad.rb +22 -0
- data/lib/neuron-client/model/admin/ad_zone.rb +15 -0
- data/lib/neuron-client/model/admin/base.rb +91 -0
- data/lib/neuron-client/model/admin/blocked_referer.rb +12 -0
- data/lib/neuron-client/model/admin/blocked_user_agent.rb +12 -0
- data/lib/neuron-client/model/admin/geo_target.rb +16 -0
- data/lib/neuron-client/model/admin/report.rb +15 -0
- data/lib/neuron-client/model/admin/s3_file.rb +12 -0
- data/lib/neuron-client/model/admin/zone.rb +15 -0
- data/lib/neuron-client/model/base.rb +38 -0
- data/lib/neuron-client/model/common/ad.rb +40 -0
- data/lib/neuron-client/model/common/ad_calculations.rb +329 -0
- data/lib/neuron-client/model/common/ad_zone.rb +17 -0
- data/lib/neuron-client/model/common/base.rb +67 -0
- data/lib/neuron-client/model/common/blocked_referer.rb +16 -0
- data/lib/neuron-client/model/common/blocked_user_agent.rb +16 -0
- data/lib/neuron-client/model/common/geo_target.rb +16 -0
- data/lib/neuron-client/model/common/report.rb +21 -0
- data/lib/neuron-client/model/common/s3_file.rb +16 -0
- data/lib/neuron-client/model/common/zone.rb +22 -0
- data/lib/neuron-client/model/common/zone_calculations.rb +41 -0
- data/lib/neuron-client/model/membase/ad.rb +31 -0
- data/lib/neuron-client/model/membase/ad_zone.rb +11 -0
- data/lib/neuron-client/model/membase/blocked_referer.rb +18 -0
- data/lib/neuron-client/model/membase/blocked_user_agent.rb +18 -0
- data/lib/neuron-client/model/membase/geo_target.rb +11 -0
- data/lib/neuron-client/model/membase/report.rb +11 -0
- data/lib/neuron-client/model/membase/s3_file.rb +11 -0
- data/lib/neuron-client/model/membase/zone.rb +19 -0
- data/lib/neuron-client/model/models.rb +14 -0
- data/lib/neuron-client/version.rb +1 -1
- data/neuron-client.gemspec +18 -11
- data/spec/fixtures/vcr_cassettes/s3_file.yml +186 -4
- data/spec/lib/admin_connection_spec.rb +82 -0
- data/spec/lib/api_spec.rb +80 -0
- data/spec/lib/membase_connection_spec.rb +27 -0
- data/spec/lib/model/admin/ad_spec.rb +34 -0
- data/spec/lib/model/admin/ad_zone_spec.rb +19 -0
- data/spec/lib/model/admin/base_spec.rb +11 -0
- data/spec/lib/model/admin/blocked_referer_spec.rb +11 -0
- data/spec/lib/model/admin/blocked_user_agent_spec.rb +11 -0
- data/spec/lib/model/admin/geo_target_spec.rb +30 -0
- data/spec/lib/model/admin/report_spec.rb +21 -0
- data/spec/lib/model/admin/s3_spec.rb +11 -0
- data/spec/lib/model/admin/zone_spec.rb +21 -0
- data/spec/lib/model/base_spec.rb +89 -0
- data/spec/lib/model/common/ad_calculations_spec.rb +1148 -0
- data/spec/lib/model/common/ad_spec.rb +11 -0
- data/spec/lib/model/common/ad_zone_spec.rb +11 -0
- data/spec/lib/model/common/base_spec.rb +11 -0
- data/spec/lib/model/common/blocked_referer_spec.rb +11 -0
- data/spec/lib/model/common/blocked_user_agent_spec.rb +11 -0
- data/spec/lib/model/common/geo_target_spec.rb +11 -0
- data/spec/lib/model/common/report_spec.rb +11 -0
- data/spec/lib/model/common/s3_spec.rb +11 -0
- data/spec/lib/model/common/zone_calculations_spec.rb +54 -0
- data/spec/lib/model/common/zone_spec.rb +11 -0
- data/spec/lib/model/membase/ad_spec.rb +50 -0
- data/spec/lib/model/membase/ad_zone_spec.rb +11 -0
- data/spec/lib/model/membase/base_spec.rb +11 -0
- data/spec/lib/model/membase/blocked_referer_spec.rb +30 -0
- data/spec/lib/model/membase/blocked_user_agent_spec.rb +30 -0
- data/spec/lib/model/membase/geo_target_spec.rb +11 -0
- data/spec/lib/model/membase/report_spec.rb +11 -0
- data/spec/lib/model/membase/s3_spec.rb +11 -0
- data/spec/lib/model/membase/zone_spec.rb +28 -0
- data/spec/lib/old_spec.rb +192 -149
- data/spec/lib/s3_file_spec.rb +45 -42
- data/spec/spec_helper.rb +2 -1
- metadata +296 -57
- data/lib/neuron-client/ad.rb +0 -39
- data/lib/neuron-client/ad_zone.rb +0 -16
- data/lib/neuron-client/blocked_referer.rb +0 -12
- data/lib/neuron-client/blocked_user_agent.rb +0 -12
- data/lib/neuron-client/connected.rb +0 -138
- data/lib/neuron-client/geo_target.rb +0 -16
- data/lib/neuron-client/real_time_stats.rb +0 -0
- data/lib/neuron-client/report.rb +0 -20
- data/lib/neuron-client/s3_file.rb +0 -10
- data/lib/neuron-client/zone.rb +0 -16
@@ -0,0 +1,17 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class AdZone
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("ad_zone")
|
9
|
+
resources_name("ad_zones")
|
10
|
+
|
11
|
+
attr_accessor :ad_id, :zone_id, :priority, :weight,
|
12
|
+
:created_at, :updated_at
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
module Base
|
6
|
+
def initialize(attrs=nil)
|
7
|
+
(attrs || {}).each do |k,v|
|
8
|
+
next if k.to_s == 'id' && self.class.remote_id != 'id'
|
9
|
+
k = 'id' if k.to_s == self.class.remote_id
|
10
|
+
self.send("#{k}=", v) if self.respond_to?("#{k}=")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.send(:attr_accessor, :id)
|
16
|
+
base.extend(ClassMethods)
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def api
|
21
|
+
class_name_to_load = superclass.name.split('::').last
|
22
|
+
Neuron::Client::Model.const_get(class_name_to_load).api
|
23
|
+
end
|
24
|
+
|
25
|
+
def connection
|
26
|
+
api.connection
|
27
|
+
end
|
28
|
+
|
29
|
+
def attr_accessor(*vars)
|
30
|
+
@attributes ||= []
|
31
|
+
@attributes += vars
|
32
|
+
super(*vars)
|
33
|
+
end
|
34
|
+
|
35
|
+
def attributes
|
36
|
+
@attributes
|
37
|
+
end
|
38
|
+
|
39
|
+
def resource_name(res=nil)
|
40
|
+
if res
|
41
|
+
@resource_name = res.to_s
|
42
|
+
end
|
43
|
+
@resource_name
|
44
|
+
end
|
45
|
+
|
46
|
+
def resources_name(res=nil)
|
47
|
+
if res
|
48
|
+
@resources_name = res.to_s.downcase
|
49
|
+
end
|
50
|
+
if @resources_name.nil? && !@resource_name.nil?
|
51
|
+
@resources_name = "#{@resource_name}s"
|
52
|
+
end
|
53
|
+
@resources_name
|
54
|
+
end
|
55
|
+
|
56
|
+
def remote_id(remote_id=nil)
|
57
|
+
if remote_id
|
58
|
+
@remote_id = remote_id.to_s
|
59
|
+
end
|
60
|
+
@remote_id || 'id'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class BlockedReferer
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("blocked_referer")
|
9
|
+
resources_name("blocked_referers")
|
10
|
+
|
11
|
+
attr_accessor :referer, :reversed_referer, :created_at, :updated_at
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class BlockedUserAgent
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("blocked_user_agent")
|
9
|
+
resources_name("blocked_user_agents")
|
10
|
+
|
11
|
+
attr_accessor :user_agent, :description, :created_at, :updated_at
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class GeoTarget
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("geo_target")
|
9
|
+
resources_name("geo_targets")
|
10
|
+
|
11
|
+
attr_accessor :geo_type, :full_name, :name, :abbreviation, :updated_at
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class Report
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("report")
|
9
|
+
resources_name("reports")
|
10
|
+
|
11
|
+
attr_accessor :errors
|
12
|
+
attr_accessor :template, :parameters, :state
|
13
|
+
|
14
|
+
def status
|
15
|
+
@state
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class S3File
|
6
|
+
include Base
|
7
|
+
|
8
|
+
resource_name("s3_file")
|
9
|
+
resources_name("s3_files")
|
10
|
+
|
11
|
+
attr_accessor :bucket, :filename, :purpose, :filesize, :created_at, :updated_at
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
class Zone
|
6
|
+
include Base
|
7
|
+
include ZoneCalculations
|
8
|
+
|
9
|
+
resource_name("zone")
|
10
|
+
resources_name("zones")
|
11
|
+
|
12
|
+
attr_accessor :slug, :response_type, :template_slug, :parameters,
|
13
|
+
:created_at, :updated_at, :ad_links
|
14
|
+
|
15
|
+
def find_ad(ad_id)
|
16
|
+
Neuron::Client::Model::Ad.find(ad_id)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Common
|
5
|
+
module ZoneCalculations
|
6
|
+
# This module expects the following methods to be defined:
|
7
|
+
#
|
8
|
+
# ad_links (Hash, keys are ad IDs, values are a sub-hash: {'priority' => p, 'weight' => w})
|
9
|
+
# find_ad(ad_id) (nil, or an object that responds to :active? and :pressure)
|
10
|
+
|
11
|
+
def ads_by_priority
|
12
|
+
calculate_ads_by_priority
|
13
|
+
end
|
14
|
+
|
15
|
+
def calculate_ads_by_priority
|
16
|
+
entries = {}
|
17
|
+
ad_links.each do |ad_id, link|
|
18
|
+
next unless ad = find_ad(ad_id)
|
19
|
+
next unless ad.active?
|
20
|
+
weight = link['weight'].to_f
|
21
|
+
priority = link['priority'].to_f
|
22
|
+
entries[priority] ||= []
|
23
|
+
entries[priority] << [ad_id, weighted_pressure(weight, ad.pressure)]
|
24
|
+
end
|
25
|
+
entries.sort_by do |priority, entry|
|
26
|
+
priority
|
27
|
+
end.map do |priority, entry|
|
28
|
+
entry.sort_by(&:first)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def weighted_pressure(weight, pressure)
|
35
|
+
[( [weight, 0.0].max * [pressure, 1.0].max ), 1.0].max.to_f
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
class Ad < Common::Ad
|
6
|
+
def total_impressed
|
7
|
+
key = "count_delivery_ad_#{self.id}"
|
8
|
+
self.class.connection.get(key).to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
def today_impressed
|
12
|
+
now_adjusted_for_ad_time_zone = Time.now.in_time_zone(self.time_zone)
|
13
|
+
formatted_date = now_adjusted_for_ad_time_zone.strftime('%Y%m%d') # format to YYYYMMDD
|
14
|
+
key = "count_delivery_#{formatted_date}_ad_#{self.id}"
|
15
|
+
self.class.connection.get(key).to_f
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def find(id)
|
20
|
+
ad = nil
|
21
|
+
membase_key = "Ad:#{id}"
|
22
|
+
cached_json = self.connection.get(membase_key)
|
23
|
+
ad = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
|
24
|
+
ad
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
class BlockedReferer < Common::BlockedReferer
|
6
|
+
class << self
|
7
|
+
def all
|
8
|
+
blocked_referers = []
|
9
|
+
cached_json = self.connection.get('blocked_referers')
|
10
|
+
blocked_referers = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
|
11
|
+
blocked_referers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
class BlockedUserAgent < Common::BlockedUserAgent
|
6
|
+
class << self
|
7
|
+
def all
|
8
|
+
blocked_user_agents = []
|
9
|
+
cached_json = self.connection.get('blocked_user_agents')
|
10
|
+
blocked_user_agents = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
|
11
|
+
blocked_user_agents
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
module Membase
|
5
|
+
class Zone < Common::Zone
|
6
|
+
class << self
|
7
|
+
def find(id)
|
8
|
+
zone = nil
|
9
|
+
membase_key = "Zone:#{id}"
|
10
|
+
cached_json = self.connection.get(membase_key)
|
11
|
+
zone = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
|
12
|
+
zone
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Neuron
|
2
|
+
module Client
|
3
|
+
module Model
|
4
|
+
class Ad < Base; end
|
5
|
+
class AdZone < Base; end
|
6
|
+
class BlockedReferer < Base; end
|
7
|
+
class BlockedUserAgent < Base; end
|
8
|
+
class GeoTarget < Base; end
|
9
|
+
class Report < Base; end
|
10
|
+
class S3File < Base; end
|
11
|
+
class Zone < Base; end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/neuron-client.gemspec
CHANGED
@@ -5,7 +5,7 @@ require "neuron-client/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "neuron-client"
|
7
7
|
s.version = Neuron::Client::VERSION
|
8
|
-
s.authors = ["
|
8
|
+
s.authors = ["RMM Online"]
|
9
9
|
s.email = ["devteam@rmmonline.com"]
|
10
10
|
s.homepage = "http://github.com/rmm/neuron-client"
|
11
11
|
s.summary = "Neuron Admin Client Gem"
|
@@ -17,14 +17,21 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
-
s.add_dependency "rest-client", "
|
21
|
-
s.add_dependency "yajl-ruby", "
|
22
|
-
s.
|
23
|
-
s.
|
24
|
-
s.
|
25
|
-
s.
|
26
|
-
|
27
|
-
s.add_development_dependency "
|
28
|
-
s.add_development_dependency "
|
29
|
-
s.add_development_dependency
|
20
|
+
s.add_dependency "rest-client", "~> 1.6.3"
|
21
|
+
s.add_dependency "yajl-ruby", "~> 0.8.2"
|
22
|
+
s.add_dependency "activesupport", "~> 3.0.10"
|
23
|
+
s.add_dependency "tzinfo", "~> 0.3.29"
|
24
|
+
s.add_dependency "i18n", "~> 0.6.0"
|
25
|
+
s.add_dependency "dalli", "~> 1.0.5"
|
26
|
+
|
27
|
+
s.add_development_dependency "rspec", "~> 2.6.0"
|
28
|
+
s.add_development_dependency "simplecov", "~> 0.4.2"
|
29
|
+
s.add_development_dependency("rb-fsevent", "~> 0.4.3") if RUBY_PLATFORM =~ /darwin/i
|
30
|
+
s.add_development_dependency "guard", "~> 0.6.2"
|
31
|
+
s.add_development_dependency "guard-bundler", "~> 0.1.3"
|
32
|
+
s.add_development_dependency "guard-rspec", "~> 0.4.2"
|
33
|
+
s.add_development_dependency "fakeweb", "~> 1.3.0"
|
34
|
+
s.add_development_dependency "vcr", "~> 1.11.1"
|
35
|
+
s.add_development_dependency "timecop", "~> 0.3.5"
|
36
|
+
s.add_development_dependency "chronic", "~> 0.6.2"
|
30
37
|
end
|