Module: MakeplansSmsPricing
- Defined in:
- lib/makeplans_sms_pricing.rb,
lib/makeplans_sms_pricing/import.rb,
lib/makeplans_sms_pricing/railtie.rb,
lib/makeplans_sms_pricing/version.rb
Defined Under Namespace
Modules: Import
Classes: Railtie
Constant Summary
collapse
- VERSION =
"0.2.1"
Class Method Summary
collapse
-
.configure {|_self| ... } ⇒ Object
-
.export(provider, provider_per_country = nil) ⇒ Object
-
.import(provider, country_code, currency, price) ⇒ Object
-
.import_from_csv(provider, url, currency) ⇒ Object
-
.lowest_price(providers, country_code, currency) ⇒ Object
-
.redis ⇒ Object
-
.redis=(config) ⇒ Object
-
.sms_price(provider, country_code, currency) ⇒ Object
Class Method Details
82
83
84
|
# File 'lib/makeplans_sms_pricing.rb', line 82
def configure
yield self if block_given?
end
|
.export(provider, provider_per_country = nil) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/makeplans_sms_pricing.rb', line 49
def export(provider, provider_per_country = nil)
raise ArgumentError, "You need to pass value to default provider" if provider.nil?
all_list = {}
MakeplansSmsPricing.redis.with do |conn|
conn.scan_each(match: "sms_pricing:#{provider}:*:*") do |key|
split = key.split ":"
set_price_list(all_list, split[1], split[2], split[3])
end
unless provider_per_country.nil?
provider_per_country.each do |country_code, provider|
conn.scan_each(match: "sms_pricing:#{provider}:#{country_code}:*") do |key|
split = key.split ":"
set_price_list(all_list, split[1], split[2], split[3])
end
end
end
end
all_list
end
|
.import(provider, country_code, currency, price) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/makeplans_sms_pricing.rb', line 42
def import(provider, country_code, currency, price)
key = "sms_pricing:#{provider}:#{country_code}:#{currency}"
MakeplansSmsPricing.redis.then do |conn|
conn.rpush key, price
end
end
|
.import_from_csv(provider, url, currency) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/makeplans_sms_pricing.rb', line 30
def import_from_csv(provider, url, currency)
require "csv"
response = Net::HTTP.get_response(URI.parse(url))
csv = response.body
rows = CSV.parse csv, headers: true, return_headers: false
rows.each do |row|
country_code, price = row[0], row[3]
import(provider, country_code, currency, price)
end
end
|
.lowest_price(providers, country_code, currency) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/makeplans_sms_pricing.rb', line 21
def lowest_price(providers, country_code, currency)
providers.map{|provider|
{
provider: provider,
price: sms_price(provider, country_code, currency)
}
}.min_by {|provider|provider[:price]}
end
|
.redis ⇒ Object
70
71
72
|
# File 'lib/makeplans_sms_pricing.rb', line 70
def redis
@redis ||= ConnectionPool.new(size: 5) { Redis.new }
end
|
.redis=(config) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/makeplans_sms_pricing.rb', line 74
def redis=(config)
if config.is_a? ConnectionPool
@redis = config
else
@redis = Redis.new(config)
end
end
|
.sms_price(provider, country_code, currency) ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/makeplans_sms_pricing.rb', line 12
def sms_price(provider, country_code, currency)
key = "sms_pricing:#{provider}:#{country_code}:#{currency}"
prices = redis.then do |conn|
conn.lrange key, 0, -1
end
prices.map(&:to_f).max
end
|