kavenegar-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/lib/generators/kavenegar-ruby/install_generator.rb +18 -0
  3. data/lib/generators/templates/kavenegar-ruby_initializer.rb +18 -0
  4. data/lib/kavenegar-ruby.rb +39 -0
  5. data/lib/kavenegar-ruby/core.rb +11 -0
  6. data/lib/kavenegar-ruby/helper.rb +3 -0
  7. data/lib/kavenegar-ruby/helpers/convertor.rb +39 -0
  8. data/lib/kavenegar-ruby/helpers/validator.rb +31 -0
  9. data/lib/kavenegar-ruby/meaning.rb +42 -0
  10. data/lib/kavenegar-ruby/request.rb +5 -0
  11. data/lib/kavenegar-ruby/requests/RequestBase.rb +11 -0
  12. data/lib/kavenegar-ruby/requests/cancel.rb +40 -0
  13. data/lib/kavenegar-ruby/requests/config.rb +41 -0
  14. data/lib/kavenegar-ruby/requests/countinbox.rb +34 -0
  15. data/lib/kavenegar-ruby/requests/countpostalcode.rb +34 -0
  16. data/lib/kavenegar-ruby/requests/deliver.rb +32 -0
  17. data/lib/kavenegar-ruby/requests/info.rb +19 -0
  18. data/lib/kavenegar-ruby/requests/latestoutbox.rb +28 -0
  19. data/lib/kavenegar-ruby/requests/lookup.rb +45 -0
  20. data/lib/kavenegar-ruby/requests/receive.rb +29 -0
  21. data/lib/kavenegar-ruby/requests/selectoutbox.rb +32 -0
  22. data/lib/kavenegar-ruby/requests/send.rb +54 -0
  23. data/lib/kavenegar-ruby/requests/tts.rb +45 -0
  24. data/lib/kavenegar-ruby/respond.rb +3 -0
  25. data/lib/kavenegar-ruby/responds/cancel.rb +32 -0
  26. data/lib/kavenegar-ruby/responds/config.rb +34 -0
  27. data/lib/kavenegar-ruby/responds/countinbox.rb +34 -0
  28. data/lib/kavenegar-ruby/responds/countpostalcode.rb +34 -0
  29. data/lib/kavenegar-ruby/responds/deliver.rb +38 -0
  30. data/lib/kavenegar-ruby/responds/info.rb +32 -0
  31. data/lib/kavenegar-ruby/responds/latestoutbox.rb +38 -0
  32. data/lib/kavenegar-ruby/responds/lookup.rb +38 -0
  33. data/lib/kavenegar-ruby/responds/receive.rb +38 -0
  34. data/lib/kavenegar-ruby/responds/selectoutbox.rb +38 -0
  35. data/lib/kavenegar-ruby/responds/send.rb +38 -0
  36. data/lib/kavenegar-ruby/responds/tts.rb +38 -0
  37. data/lib/kavenegar-ruby/version.rb +3 -0
  38. metadata +208 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d178cd3577f4c107778a733337677e337d1562b81951c6d75eb3ef219d241c7
4
+ data.tar.gz: 5aad1705a4b8b3cf22605d865b1895f111f29cdcec6b7281aca50886904f8465
5
+ SHA512:
6
+ metadata.gz: 6589a4a3345cbba760f0c5fe0e48796e6b9d645f0c1fc67d1eb242732b69d1ec82812b0735fe0231fb4d6c49e4b529020a908296d061adac9aceed3d2eafde89
7
+ data.tar.gz: 6195a85a45c152ce2ccdaf0d0b3ffd8aac087b1eb40347b196828a2138af826b661ba56b0f0243d702a04dcae3e5e15db92a84b296301c40c908f937056f5971
@@ -0,0 +1,18 @@
1
+ module KaveRestApi
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates KaveRestApi initializer for your application"
6
+
7
+ def copy_initializer
8
+ template "kavenegar-ruby_initializer.rb", "config/initializers/kavenegar-ruby.rb"
9
+
10
+ puts <<~EOF
11
+ \e[36mInstall complete 👻 \e[0m
12
+ For report issues or suggest feature contact me on twitter/github: \e[32m@mm580486\e[0m
13
+ EOF
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ KaveRestApi.configure do |config|
2
+
3
+ # To completely ignore debug mode events(No Errors) uncomment this line *optional
4
+ # config.debugmode = false #by default it's true
5
+
6
+ # It is recommended that you pull your API keys from environment settings. *required
7
+ config.api_key = 'XXXX-XXXX-XXXX-XXXX'
8
+
9
+ # Default response format is json (you can use xml too). *optional
10
+ config.format = 'json'
11
+
12
+ #If you don't set your sender number in your request, this is the default number used instead *required
13
+ config.default_sender = '10000777070777'
14
+
15
+ # You can remove problematic emojis (like android emojis) and replace with standard emojis listed here:(https://www.webpagefx.com/tools/emoji-cheat-sheet/)
16
+ # config.strip_emoji = false # can include false , true and matcher
17
+
18
+ end
@@ -0,0 +1,39 @@
1
+ %w(faraday faraday_middleware validatable i18n).each do |dependency|
2
+ require dependency
3
+ end
4
+
5
+ begin
6
+ require "pry"
7
+ rescue LoadError
8
+
9
+ end
10
+
11
+ module KaveRestApi
12
+
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.configure
18
+ self.configuration ||= Configuration.new
19
+ yield configuration
20
+ end
21
+
22
+ class Configuration
23
+ attr_accessor :api_key, :default_sender,:debugmode,:format,:strip_emoji
24
+
25
+ def initialize
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ %w(version core helper request meaning respond).each do |identify|
34
+ require "kavenegar-ruby/#{identify}"
35
+ end
36
+
37
+
38
+
39
+
@@ -0,0 +1,11 @@
1
+ module KaveRestApi
2
+
3
+ I18n.config.available_locales = :en
4
+
5
+ def self.require_libs(lib_path,*libs)
6
+ libs.each do |lib|
7
+ require_relative "#{lib_path}/#{lib}"
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module KaveRestApi
2
+ self.require_libs './helpers','convertor','validator'
3
+ end
@@ -0,0 +1,39 @@
1
+
2
+ module KaveRestApi
3
+ module NumberHelper
4
+ def ctpd
5
+ return self.to_s.tr('0123456789','Û°Ù¡Û²Û³Û´ÛµÛ¶Û·Û¸Û¹')
6
+ end
7
+
8
+ end
9
+
10
+ module StringHelper
11
+
12
+ def ctsd
13
+ return self.to_s.tr('۰١۱۲۳۴۵۶۷۸۹،١٢٣٤٥٦٧٨٩٠','01123456789,1234567890')
14
+ end
15
+
16
+ def ctpd
17
+ return self.to_s.tr('01123456789,','۰١۱۲۳۴۵۶۷۸۹،')
18
+ end
19
+
20
+ def strip_emoji
21
+ str = self.force_encoding('utf-8').encode
22
+ arr_regex=[/[\u{1f600}-\u{1f64f}]/,/[\u{2702}-\u{27b0}]/,/[\u{1f680}-\u{1f6ff}]/,/[\u{24C2}-\u{1F251}]/,/[\u{1f300}-\u{1f5ff}]/]
23
+ arr_regex.each do |regex|
24
+ str = str.gsub regex, ''
25
+ end
26
+ return str
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ class Integer
34
+ include KaveRestApi::NumberHelper
35
+ end
36
+
37
+ class String
38
+ include KaveRestApi::StringHelper
39
+ end
@@ -0,0 +1,31 @@
1
+
2
+ module KaveRestApi
3
+ module NumberHelper
4
+
5
+ def is_phone?
6
+ return !!self.match(/^(09{1})+([1-3]{1})+(\d{8})$/)
7
+ end
8
+
9
+ end
10
+
11
+ module StringHelper
12
+
13
+ def multibyte?
14
+ chars.count < bytes.count
15
+ end
16
+
17
+ def is_phone?
18
+ return !!self.ctsd.match(/^(09{1})+([1-3]{1})+(\d{8})$/)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ class Integer
26
+ include KaveRestApi::NumberHelper
27
+ end
28
+
29
+ class String
30
+ include KaveRestApi::StringHelper
31
+ end
@@ -0,0 +1,42 @@
1
+ module KaveRestApi
2
+ ERRORS = {
3
+ '200' => I18n.t("KaveRestApi.200", :default => "Request Approved"),
4
+ '400' => I18n.t("KaveRestApi.400", :default => "Argument Error"),
5
+ '401' => I18n.t("KaveRestApi.401", :default => "Account has been disabled"),
6
+ '402' => I18n.t("KaveRestApi.402", :default => "Operation failed"),
7
+ '403' => I18n.t("KaveRestApi.403", :default => "Not a valid API key"),
8
+ '404' => I18n.t("KaveRestApi.404", :default => "Undeifined method"),
9
+ '405' => I18n.t("KaveRestApi.405", :default => "Get / Post wrong"),
10
+ '406' => I18n.t("KaveRestApi.406", :default => "Eequired field"),
11
+ '407' => I18n.t("KaveRestApi.407", :default => "You don't currently have permission to access this method"),
12
+ '408' => I18n.t("KaveRestApi.408", :default => "fallback text"),
13
+ '409' => I18n.t("KaveRestApi.409", :default => "Server not able to respond"),
14
+ '411' => I18n.t("KaveRestApi.411", :default => "Not a valid receptor"),
15
+ '412' => I18n.t("KaveRestApi.412", :default => "Not a valid sender"),
16
+ '413' => I18n.t("KaveRestApi.413", :default => "Message is invalid"),
17
+ '414' => I18n.t("KaveRestApi.414", :default => "Request is limit"),
18
+ '415' => I18n.t("KaveRestApi.415", :default => "fallback text"),
19
+ '417' => I18n.t("KaveRestApi.417", :default => "Not a valid UnixTime"),
20
+ '418' => I18n.t("KaveRestApi.418", :default => "Your credit is not enough"),
21
+ '419' => I18n.t("KaveRestApi.419", :default => "fallback text"),
22
+ '422' => I18n.t("KaveRestApi.422", :default => "fallback text"),
23
+ '424' => I18n.t("KaveRestApi.424", :default => "Template not found"),
24
+ '426' => I18n.t("KaveRestApi.426", :default => "fallback text"),
25
+ '428' => I18n.t("KaveRestApi.428", :default => "fallback text"),
26
+ '431' => I18n.t("KaveRestApi.431", :default => "fallback text"),
27
+ }
28
+
29
+
30
+ ENTRIES = {
31
+ :inÙ€queue => '1',
32
+ :givenÙ€date => '2',
33
+ :deliver_to_telecommunications => '4',
34
+ :failed => '6',
35
+ :delivered => '10',
36
+ :inaccessible => '11',
37
+ :ads_block => '14',
38
+ :sms_id_is_invalid => '100'
39
+ }
40
+
41
+
42
+ end
@@ -0,0 +1,5 @@
1
+ module KaveRestApi
2
+
3
+ self.require_libs './requests','RequestBase','tts','send','deliver','latestoutbox','selectoutbox','info','config','receive','lookup','countinbox','cancel','countpostalcode'
4
+
5
+ end
@@ -0,0 +1,11 @@
1
+ module KaveRestApi
2
+ class RequestBase
3
+ def initialize(args = {})
4
+ config = KaveRestApi.configuration
5
+ raise(ArgumentError, 'not a valid configuration class') if config.nil? || config.api_key.nil? || config.default_sender.nil?
6
+ @API_URL = "https://api.kavenegar.com/v1/#{config.api_key}"
7
+ @DEFAULT_SENDER = config.default_sender.freeze
8
+ @FORMAT= config.format rescue 'json'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ module KaveRestApi
2
+ class Cancel < KaveRestApi::RequestBase
3
+
4
+ include Validatable
5
+ attr_accessor :messageid
6
+ attr_reader :response
7
+ validates_presence_of :messageid
8
+
9
+ validates_format_of :messageid, :with => /^\d*$/, :if => Proc.new { !messageid.nil? }
10
+
11
+
12
+ def initialize(args = {})
13
+ super
14
+ @ACTION_NAME = [:countinbox,@FORMAT].join('.').freeze
15
+ @messageid = args.fetch(:messageid)
16
+ if @messageid.kind_of?(Array)
17
+ @valid= false if @messageid.length > 200
18
+ @messageid = @messageid.join(',')
19
+ end
20
+ @messageid = @messageid.ctsd
21
+
22
+ @response = ResponseCancel.new
23
+ end
24
+
25
+ def valid?
26
+ @valid ||= true
27
+ end
28
+
29
+ def call
30
+ connection = Faraday.new(url: "#{API_URL}/sms/") do |faraday|
31
+ faraday.adapter Faraday.default_adapter
32
+ faraday.response FORMAT.to_sym
33
+ end
34
+ response = connection.get(ACTION_NAME,isread: @isread, linenumber: @linenumber,startdate: @startdate,enddate: @enddate)
35
+ @response.validate(response.body)
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,41 @@
1
+ module KaveRestApi
2
+ class Config < KaveRestApi::RequestBase
3
+
4
+ include Validatable
5
+ attr_accessor :apilogs,:dailyreport,:debugmode,:defaultsender,:mincreditalarm,:resendfailed
6
+ attr_reader :response
7
+
8
+ def initialize(args = {})
9
+ @ACTION_NAME = [:config,@FORMAT].join('.').freeze
10
+ @apilogs = args.fetch(:apilogs,nil)
11
+ @dailyreport = args.fetch(:dailyreport,nil)
12
+ @debugmode = args.fetch(:debugmode,nil)
13
+ @defaultsender = args.fetch(:defaultsender,nil)
14
+ @mincreditalarm = args.fetch(:mincreditalarm,nil)
15
+ @resendfailed = args.fetch(:resendfailed,nil)
16
+ @response = ResponseConfig.new
17
+ end
18
+
19
+ def valid?
20
+ @valid
21
+ end
22
+
23
+ def full_message_errors
24
+
25
+ end
26
+
27
+ def call
28
+ connection = Faraday.new(url: "#{@API_URL}/account/") do |faraday|
29
+ faraday.adapter Faraday.default_adapter
30
+ faraday.response @FORMAT.to_sym
31
+ end
32
+ response = connection.get(@ACTION_NAME,apilogs: @apilogs,dailyreport: @dailyreport,debugmode: @debugmode,defaultsender: @defaultsender,mincreditalarm: @mincreditalarm,resendfailed: @resendfailed)
33
+ @response.validate(response.body)
34
+ end
35
+
36
+
37
+
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,34 @@
1
+ module KaveRestApi
2
+ class CountInBox < KaveRestApi::RequestBase
3
+
4
+ include Validatable
5
+ attr_accessor :startdate,:linenumber,:enddate
6
+ attr_reader :response
7
+ validates_presence_of :startdate
8
+ validates_presence_of :linenumber
9
+ validates_format_of :linenumber, :with => /^\d*$/, :if => Proc.new { !linenumber.nil? }
10
+ validates_format_of :startdate, :with => /^\d*$/, :if => Proc.new { !unixdate.nil? }
11
+ validates_format_of :enddate, :with => /^\d*$/, :if => Proc.new { !unixdate.nil? }
12
+
13
+ def initialize(args = {})
14
+ super
15
+ @ACTION_NAME = [:countinbox,@FORMAT].join('.').freeze
16
+ @startdate = args.fetch(:startdate)
17
+ @enddate = args.fetch(:enddate,nil)
18
+ @linenumber = args.fetch(:linenumber,nil)
19
+ @isread = args.fetch(:isread,nil)
20
+ @response = ResponseCountInBox.new
21
+ end
22
+
23
+ def call
24
+ connection = Faraday.new(url: "#{API_URL}/sms/") do |faraday|
25
+ faraday.adapter Faraday.default_adapter
26
+ faraday.response FORMAT.to_sym
27
+ end
28
+ response = connection.get(ACTION_NAME,isread: @isread, linenumber: @linenumber,startdate: @startdate,enddate: @enddate)
29
+ @response.validate(response.body)
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ module KaveRestApi
2
+ class CountPostalCode < KaveRestApi::RequestBase
3
+
4
+ include Validatable
5
+ attr_accessor :postalcode
6
+ attr_reader :response
7
+ validates_presence_of :postalcode
8
+
9
+ validates_format_of :postalcode, :with => /^\d*$/, :if => Proc.new { !messageid.nil? }
10
+
11
+
12
+ def initialize(args = {})
13
+ super
14
+ @ACTION_NAME = [:countpostalcode,@FORMAT].join('.').freeze
15
+ @postalcode = args.fetch(:postalcode)
16
+ @postalcode = @postalcode.ctsd
17
+
18
+ @response = ResponseCountPostalCode.new
19
+ end
20
+
21
+
22
+
23
+ def call
24
+ connection = Faraday.new(url: "#{API_URL}/sms/") do |faraday|
25
+ faraday.adapter Faraday.default_adapter
26
+ faraday.response FORMAT.to_sym
27
+ end
28
+ response = connection.get(ACTION_NAME,countpostalcode: @countpostalcode)
29
+ @response.validate(response.body)
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,32 @@
1
+ module KaveRestApi
2
+ class Deliver < KaveRestApi::RequestBase
3
+
4
+ include Validatable
5
+ attr_accessor :messageid
6
+ attr_reader :response
7
+ validates_presence_of :messageid
8
+ validates_format_of :messageid, :with => /^\d*$/, :if => Proc.new { !messageid.nil? }
9
+
10
+ def initialize(args = {})
11
+ @ACTION_NAME = [:status,@FORMAT].join('.').freeze
12
+ @messageid = args.fetch(:messageid)
13
+ if @messageid.kind_of?(Array)
14
+ raise(ArgumentError,'Message Id Arrays: Buffer Overflow: Max length 3000 of array') if @messageid.length > 3000
15
+ @messageid = @messageid.join(',')
16
+ end
17
+ @messageid = @messageid.ctsd
18
+ @response = ResponseDeliver.new
19
+ end
20
+
21
+ def call
22
+ connection = Faraday.new(url: "#{@API_URL}/sms/") do |faraday|
23
+ faraday.adapter Faraday.default_adapter
24
+ faraday.response FORMAT.to_sym
25
+ end
26
+ response = connection.get(@ACTION_NAME, messageid: @messageid)
27
+ @response.validate(response.body)
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,19 @@
1
+ module KaveRestApi
2
+ class Info < KaveRestApi::RequestBase
3
+ def initialize(args = {})
4
+ @ACTION_NAME = [:info,@FORMAT].join('.').freeze
5
+ @response = ResponseInfo.new
6
+ end
7
+
8
+ def call
9
+ connection = Faraday.new(url: "#{API_URL}/account/") do |faraday|
10
+ faraday.adapter Faraday.default_adapter
11
+ faraday.response FORMAT.to_sym
12
+ end
13
+ response = connection.get(@ACTION_NAME)
14
+ @response.validate(response.body)
15
+ end
16
+
17
+ end
18
+
19
+ end