makeplans_sms_pricing 0.2.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
+ SHA256:
3
+ metadata.gz: 5f8803c6a1c8796be7b934c09162e2c98fb17153bae826a179ef920d08c9ce1f
4
+ data.tar.gz: 9a292fb41eb4ccbde073b30592194cdaf38089fab5cc7e261bbc876390c4fd32
5
+ SHA512:
6
+ metadata.gz: 6d6bf20ed74999554ab74b74c525499fc665842b5f2303e336d1716ab91bda1440d1c5857d9c068596f282e56a8731e8f6d57a26d0a34c4a2f3ce66268ee2322
7
+ data.tar.gz: d2c0a40c93e7b2deb186727bcf568c7cf337dd396bbfbefd094cfbc64bc77e9ee479e480045611d4abba7d58f2290edb9de75e42dd7f54c1695e1b2fa6b9df31
data/.env.test ADDED
@@ -0,0 +1 @@
1
+ REDIS_URL=redis://localhost:6380
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-22
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in makeplans_sms_pricing.gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+ gem "minitest"
10
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ makeplans_sms_pricing (0.2.0)
5
+ connection_pool
6
+ countries
7
+ redis
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ connection_pool (2.4.1)
13
+ countries (6.0.1)
14
+ unaccent (~> 0.3)
15
+ dotenv (3.1.2)
16
+ minitest (5.25.1)
17
+ rake (13.2.1)
18
+ redis (5.2.0)
19
+ redis-client (>= 0.22.0)
20
+ redis-client (0.22.2)
21
+ connection_pool
22
+ unaccent (0.4.0)
23
+
24
+ PLATFORMS
25
+ arm64-darwin-21
26
+ arm64-darwin-22
27
+ arm64-darwin-23
28
+ x86_64-linux
29
+
30
+ DEPENDENCIES
31
+ dotenv
32
+ makeplans_sms_pricing!
33
+ minitest
34
+ rake
35
+
36
+ BUNDLED WITH
37
+ 2.4.7
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # MakeplansSmsPricing
2
+
3
+ ## Installation
4
+
5
+ Add this in the Gemfile:
6
+ ```ruby
7
+ gem "makeplans_sms_pricing", github: "makeplans/makeplans_sms_pricing", branch: "main"
8
+ ```
9
+
10
+ Follow these [steps](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) to generate a GitHub personal access token to allow access to private repositories. Then, set it in your application environment:
11
+
12
+ ´´´shell
13
+ bundle config --local github.com x-access-token:your-token
14
+ ```
15
+
16
+ Fetch the dependencies:
17
+
18
+ ´´´shell
19
+ bundle
20
+ ```
21
+
22
+ ## Usage
23
+ Let's say that we use Twilio as a SMS provider.
24
+
25
+ You can import SMS pricing data like so:
26
+
27
+ ´´´shell
28
+ bundle exec rake "makeplans_sms_pricing:import[twilio, https://assets.cdn.prod.twilio.com/pricing-csv/SMSPricing.csv, USD]"
29
+ ```
30
+
31
+ And fetch SMS prices using a country code:
32
+ ```ruby
33
+ MakeplansSmsPricing.sms_price "twilio", "US", "USD"
34
+ ```
35
+
36
+ ## Configuration
37
+
38
+ You can configure Redis using `MakeplansSmsPricing.configure`:
39
+ ```ruby
40
+ MakeplansSmsPricing.configure do |config|
41
+ config.redis = { ... } # or ConnectionPool.new(...)
42
+ end
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/makeplans/makeplans_sms_pricing.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
13
+
14
+ require "bundler/setup"
15
+ require "makeplans_sms_pricing"
16
+
17
+ gem_path = File.expand_path(__dir__)
18
+ Dir.glob("#{gem_path}/lib/makeplans_sms_pricing/tasks/**/*.rake").each { |task| import task }
@@ -0,0 +1,4 @@
1
+ require "makeplans_sms_pricing"
2
+
3
+ path = File.expand_path(__dir__)
4
+ Dir.glob("#{path}/tasks/**/*.rake").each { |task| import task }
@@ -0,0 +1,22 @@
1
+ module MakeplansSmsPricing
2
+ module Import
3
+ class << self
4
+ def teletopia(provider, url, currency)
5
+ require "json"
6
+ require "countries"
7
+ response = Net::HTTP.get_response(URI.parse(url))
8
+ json = response.body
9
+ rows = JSON.parse(json)
10
+ rows.each do |row|
11
+ country_name = row["country"].strip
12
+ country = ISO3166::Country.find_country_by_any_name(country_name)
13
+ unless country.nil?
14
+ country_code = country.alpha2
15
+ price = row["price"][currency]["standard"]
16
+ MakeplansSmsPricing.import(provider, country_code, currency, price)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module MakeplansSmsPricing
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :makeplans_sms_pricing
4
+
5
+ rake_tasks do
6
+ lib_path = File.expand_path __dir__
7
+
8
+ Dir["#{lib_path}/tasks/**/*.rake"].each do |task|
9
+ load task
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ namespace :makeplans_sms_pricing do
2
+ desc "Import SMS pricing data"
3
+ task :import, [:provider, :url, :currency] do |t, args|
4
+ provider, url, currency = args[:provider], args[:url], args[:currency] || "USD"
5
+ MakeplansSmsPricing.import_from_csv(provider, url, currency) if url.end_with?(".csv")
6
+ MakeplansSmsPricing::Import.teletopia(provider, url, currency) if (url.end_with?(".json") && provider == "teletopia")
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MakeplansSmsPricing
4
+ VERSION = "0.2.1"
5
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "makeplans_sms_pricing/version"
4
+
5
+ module MakeplansSmsPricing
6
+ require "makeplans_sms_pricing/railtie" if defined? Rails::Railtie
7
+ require "redis"
8
+ require "connection_pool"
9
+ require "net/http"
10
+
11
+ class << self
12
+ def sms_price(provider, country_code, currency)
13
+ key = "sms_pricing:#{provider}:#{country_code}:#{currency}"
14
+ prices = redis.then do |conn|
15
+ conn.lrange key, 0, -1
16
+ end
17
+
18
+ prices.map(&:to_f).max
19
+ end
20
+
21
+ def lowest_price(providers, country_code, currency)
22
+ providers.map{|provider|
23
+ {
24
+ provider: provider,
25
+ price: sms_price(provider, country_code, currency)
26
+ }
27
+ }.min_by {|provider|provider[:price]}
28
+ end
29
+
30
+ def import_from_csv(provider, url, currency)
31
+ require "csv"
32
+ response = Net::HTTP.get_response(URI.parse(url))
33
+ csv = response.body
34
+ rows = CSV.parse csv, headers: true, return_headers: false
35
+
36
+ rows.each do |row|
37
+ country_code, price = row[0], row[3]
38
+ import(provider, country_code, currency, price)
39
+ end
40
+ end
41
+
42
+ def import(provider, country_code, currency, price)
43
+ key = "sms_pricing:#{provider}:#{country_code}:#{currency}"
44
+ MakeplansSmsPricing.redis.then do |conn|
45
+ conn.rpush key, price
46
+ end
47
+ end
48
+
49
+ def export(provider, provider_per_country = nil)
50
+ raise ArgumentError, "You need to pass value to default provider" if provider.nil?
51
+ all_list = {}
52
+ MakeplansSmsPricing.redis.with do |conn|
53
+ conn.scan_each(match: "sms_pricing:#{provider}:*:*") do |key|
54
+ split = key.split ":"
55
+ set_price_list(all_list, split[1], split[2], split[3])
56
+ end
57
+
58
+ unless provider_per_country.nil?
59
+ provider_per_country.each do |country_code, provider|
60
+ conn.scan_each(match: "sms_pricing:#{provider}:#{country_code}:*") do |key|
61
+ split = key.split ":"
62
+ set_price_list(all_list, split[1], split[2], split[3])
63
+ end
64
+ end
65
+ end
66
+ end
67
+ all_list
68
+ end
69
+
70
+ def redis
71
+ @redis ||= ConnectionPool.new(size: 5) { Redis.new }
72
+ end
73
+
74
+ def redis=(config)
75
+ if config.is_a? ConnectionPool
76
+ @redis = config
77
+ else
78
+ @redis = Redis.new(config)
79
+ end
80
+ end
81
+
82
+ def configure
83
+ yield self if block_given?
84
+ end
85
+
86
+ private
87
+
88
+ def set_price_list(all_list, provider, country_code, currency)
89
+ if all_list.key?(country_code)
90
+ all_list[country_code][:price]["#{currency}".to_sym] = MakeplansSmsPricing.sms_price(provider, country_code, currency)
91
+ else
92
+ all_list[country_code] = { price: { "#{currency}": MakeplansSmsPricing.sms_price(provider, country_code, currency) } }
93
+ end
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: makeplans_sms_pricing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - mansakondo
8
+ - Espen Antonsen
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2024-08-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: connection_pool
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: countries
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description:
57
+ email:
58
+ - mansakondo22@gmail.com
59
+ - espen@inspired.no
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".env.test"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - README.md
69
+ - Rakefile
70
+ - lib/makeplans_sms_pricing.rb
71
+ - lib/makeplans_sms_pricing/Rakefile
72
+ - lib/makeplans_sms_pricing/import.rb
73
+ - lib/makeplans_sms_pricing/railtie.rb
74
+ - lib/makeplans_sms_pricing/tasks/import.rake
75
+ - lib/makeplans_sms_pricing/version.rb
76
+ homepage: https://github.com/makeplans/makeplans_sms_pricing
77
+ licenses: []
78
+ metadata:
79
+ homepage_uri: https://github.com/makeplans/makeplans_sms_pricing
80
+ source_code_uri: https://github.com/makeplans/makeplans_sms_pricing
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.5.14
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: MakePlans SMS Pricing
100
+ test_files: []