cnf 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0be447ca1371b02e03647742a5f43ea44f684290
4
+ data.tar.gz: 168bf46f1e70811be0da5267491ebdfb93f12e62
5
+ SHA512:
6
+ metadata.gz: ead22cfa545185e5e0abc8a993af3a9e2db2b3a18017a349a25a9c96e7fbdd5c2bbcafe250a057053e9722abe125a371d8fcdb5d264844e15d53115174f253ed
7
+ data.tar.gz: 4014641fa7d5ad89b50ba06298bb6d93d652c4c615a608182259770a37cfc15f2bb22d6d7cbd932756c760592f497573f0fb637c5cf24dfde56d2e0f12ca1c66
data/lib/cnf.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'json'
2
+ require 'excon'
3
+ require 'recursive-open-struct'
4
+ require 'cnf/watcher'
5
+
6
+ module Cnf
7
+ extend self
8
+
9
+ def fetch_data
10
+ @_cnf ||= begin
11
+ res = Excon.get(url, headers: headers)
12
+ RecursiveOpenStruct.new(JSON.load(res.body), recurse_over_arrays: true)
13
+ end
14
+ end
15
+ alias_method :all, :fetch_data
16
+
17
+ def method_missing(m, *args, &block)
18
+ fetch_data.send(m)
19
+ end
20
+
21
+ def hosts
22
+ return if amazon.nil?
23
+
24
+ amazon.map do |catalog|
25
+ [ catalog.marketplace.country_code, catalog.api_hostname ]
26
+ end.to_h
27
+ end
28
+
29
+ def marketplace_ids
30
+ return if amazon.nil?
31
+
32
+ amazon.map do |catalog|
33
+ [ catalog.marketplace.country_code, catalog.marketplace.id ]
34
+ end.to_h
35
+ end
36
+
37
+ private
38
+
39
+ def headers
40
+ {
41
+ header_key => header_value
42
+ }
43
+ end
44
+
45
+ def url
46
+ ENV['CONFIGURATION_URL']
47
+ end
48
+
49
+ def header_key
50
+ ENV['CONFIGURATION_HEADER_KEY']
51
+ end
52
+
53
+ def header_value
54
+ ENV['CONFIGURATION_HEADER_VALUE']
55
+ end
56
+ end
@@ -0,0 +1,199 @@
1
+ require 'forwardable'
2
+ require 'virtus'
3
+ require 'cnf/marketplace'
4
+ require 'cnf'
5
+
6
+ module Cnf
7
+ class Catalog
8
+ include Virtus.model
9
+
10
+ attribute :aws_access_key_id, String
11
+ attribute :aws_secret_key, String
12
+ attribute :merchant_id, String
13
+ attribute :marketplace, Marketplace
14
+
15
+ class << self
16
+ extend Forwardable
17
+ include Enumerable
18
+
19
+ def_delegator :data, :each
20
+ alias :all :to_a
21
+
22
+ def data
23
+ @data ||= Cnf.amazon.map { |country| new(country.to_h) }
24
+ end
25
+ end
26
+
27
+ def name
28
+ marketplace.hostname
29
+ end
30
+
31
+ def self.names
32
+ map(&:name) + Cnf::AbebooksCatalog.names
33
+ end
34
+
35
+ def country
36
+ marketplace.country_code
37
+ end
38
+
39
+ def currency
40
+ marketplace.currency_code
41
+ end
42
+
43
+ def supported_parent_offer?(_)
44
+ true
45
+ end
46
+
47
+ alias_method :seller, :merchant_id
48
+
49
+ def marketplace_id
50
+ marketplace.id
51
+ end
52
+
53
+ def in?(marketplace_ids)
54
+ active? && marketplace_ids.include?(marketplace_id)
55
+ end
56
+
57
+ def active?
58
+ Cnf.active_channels.include? marketplace.country_code
59
+ end
60
+
61
+ def language
62
+ marketplace.language_code
63
+ end
64
+
65
+ def self.filter_by_marketplace(marketplace_ids)
66
+ Catalog.select { |catalog| catalog.in? marketplace_ids }
67
+ end
68
+
69
+ def self.find_catalog_by_country_code(country_code)
70
+ Catalog.find { |catalog| catalog.country == country_code }
71
+ end
72
+
73
+ def self.find_by_name(name)
74
+ return TestCatalog.call if name == 'www.amazon.com-richardson'
75
+ return AbebooksCatalog.find_by_name(name) if name =~ /abebooks/
76
+ return MiraklCatalog.find_by_name(name) if name =~ /libro/
77
+ Catalog.find { |catalog| catalog.name == name }
78
+ end
79
+ end
80
+
81
+ class AbebooksCatalog
82
+ include Virtus.model
83
+
84
+ attribute :id, String
85
+ attribute :password, String
86
+ attribute :name, String
87
+ attribute :language, String
88
+ attribute :country, String
89
+ attribute :currency, String
90
+ attribute :pricing_method, String
91
+ attribute :active, Boolean
92
+ attribute :flat_rate, Float
93
+ attribute :parent_offer_conditions, Array
94
+ attribute :borrowable_offer_conditions, Array
95
+
96
+ class << self
97
+ extend Forwardable
98
+ include Enumerable
99
+
100
+ def_delegator :data, :each
101
+ alias :all :to_a
102
+
103
+ def data
104
+ @data ||= Cnf.abebooks_channels.map do |channel|
105
+ new(channel.to_h)
106
+ end
107
+ end
108
+ end
109
+
110
+ def self.names
111
+ map(&:name)
112
+ end
113
+
114
+ def self.call
115
+ warn "[DEPRECATION] `call` is deprecated. Please use `find_by_name(name)` instead."
116
+ Catalog.new(
117
+ marketplace: {
118
+ country_code: "US",
119
+ currency_code: "USD",
120
+ hostname: "www.abebooks.com",
121
+ id: "abebooks",
122
+ language_code: "en"
123
+ }
124
+ )
125
+ end
126
+
127
+ def self.active
128
+ AbebooksCatalog.select { |catalog| catalog.active }
129
+ end
130
+
131
+ def self.find_by_name(name)
132
+ AbebooksCatalog.find { |catalog| catalog.name == name }
133
+ end
134
+
135
+ def use_flat_rate?
136
+ pricing_method == 'flat_rate'
137
+ end
138
+
139
+ def use_competition?
140
+ pricing_method == 'competition'
141
+ end
142
+
143
+ def supported_parent_offer?(offer)
144
+ matches_any_conditions?(parent_offer_conditions, offer)
145
+ end
146
+
147
+ def borrowable_offer?(offer)
148
+ matches_any_conditions?(borrowable_offer_conditions, offer)
149
+ end
150
+
151
+ def matches_any_conditions?(conditions, offer)
152
+ conditions.any? do |hash|
153
+ hash.all? do |attribute, value|
154
+ offer[attribute.to_sym] == value
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ class MiraklCatalog < AbebooksCatalog
161
+ attribute :api_url, String
162
+
163
+ class << self
164
+ extend Forwardable
165
+ include Enumerable
166
+
167
+ def_delegator :data, :each
168
+ alias :all :to_a
169
+
170
+ def data
171
+ @data ||= Cnf.mirakl_channels.map do |channel|
172
+ new(channel.to_h)
173
+ end
174
+ end
175
+
176
+ def active
177
+ MiraklCatalog.select { |catalog| catalog.active }
178
+ end
179
+
180
+ def find_by_name(name)
181
+ MiraklCatalog.find { |catalog| catalog.name == name }
182
+ end
183
+ end
184
+ end
185
+
186
+ class TestCatalog
187
+ def self.call
188
+ Catalog.new(
189
+ marketplace: {
190
+ country_code: "US",
191
+ currency_code: "USD",
192
+ hostname: "www.amazon.com",
193
+ language_code: "en"
194
+ },
195
+ merchant_id: "A2R2OHJS21PGNN"
196
+ )
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,15 @@
1
+ require 'forwardable'
2
+ require 'virtus'
3
+ require 'cnf'
4
+
5
+ module Cnf
6
+ class Marketplace
7
+ include Virtus.model
8
+
9
+ attribute :id, String
10
+ attribute :country_code, String
11
+ attribute :currency_code, String
12
+ attribute :language_code, String
13
+ attribute :hostname, String
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ require 'redis'
2
+ require 'cnf'
3
+
4
+ module Cnf
5
+ class Watcher
6
+ def self.call
7
+ new().call
8
+ end
9
+
10
+ attr_reader :redis
11
+
12
+ def initialize
13
+ @redis = Redis.new(url: ENV["REDIS_PUBSUB_URL"])
14
+ end
15
+
16
+ def channel
17
+ "console"
18
+ end
19
+
20
+ def call
21
+ keep_connecting(redis) do
22
+ redis.subscribe(channel) do |on|
23
+ on.message do |_, msg|
24
+ on_message_received(msg)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def on_message_received(_)
31
+ raise NotImplementedError.new
32
+ end
33
+
34
+ private
35
+
36
+ def keep_connecting(redis)
37
+ begin
38
+ yield
39
+ rescue Redis::CannotConnectError
40
+ sleep 5
41
+ retry
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cnf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Jacek Jakubik
8
+ - Marek Waligórski
9
+ - Hakan Ensari
10
+ - Marek Mikołajczyk
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2017-03-28 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: excon
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.45.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.45.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: recursive-open-struct
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '1.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: redis
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '3.3'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.3'
58
+ - !ruby/object:Gem::Dependency
59
+ name: virtus
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.0.5
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.0.5
72
+ - !ruby/object:Gem::Dependency
73
+ name: rspec
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ - !ruby/object:Gem::Dependency
87
+ name: rspec-its
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ description:
101
+ email:
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - lib/cnf.rb
107
+ - lib/cnf/catalog.rb
108
+ - lib/cnf/marketplace.rb
109
+ - lib/cnf/watcher.rb
110
+ homepage:
111
+ licenses: []
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Fetches stack configuration
133
+ test_files: []