whmcs_api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +93 -0
  4. data/Rakefile +32 -0
  5. data/lib/tasks/config/whmcs.yml +15 -0
  6. data/lib/tasks/whmcs_tasks.rake +10 -0
  7. data/lib/whmcs.rb +20 -0
  8. data/lib/whmcs/api.rb +86 -0
  9. data/lib/whmcs/configuration.rb +26 -0
  10. data/lib/whmcs/railtie.rb +10 -0
  11. data/lib/whmcs/version.rb +3 -0
  12. data/test/dummy/README.rdoc +28 -0
  13. data/test/dummy/Rakefile +6 -0
  14. data/test/dummy/app/assets/javascripts/application.js +13 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  16. data/test/dummy/app/controllers/application_controller.rb +5 -0
  17. data/test/dummy/app/helpers/application_helper.rb +2 -0
  18. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  19. data/test/dummy/bin/bundle +3 -0
  20. data/test/dummy/bin/rails +4 -0
  21. data/test/dummy/bin/rake +4 -0
  22. data/test/dummy/config.ru +4 -0
  23. data/test/dummy/config/application.rb +23 -0
  24. data/test/dummy/config/boot.rb +5 -0
  25. data/test/dummy/config/database.yml +25 -0
  26. data/test/dummy/config/environment.rb +5 -0
  27. data/test/dummy/config/environments/development.rb +37 -0
  28. data/test/dummy/config/environments/production.rb +82 -0
  29. data/test/dummy/config/environments/test.rb +39 -0
  30. data/test/dummy/config/initializers/assets.rb +8 -0
  31. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  33. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  34. data/test/dummy/config/initializers/inflections.rb +16 -0
  35. data/test/dummy/config/initializers/mime_types.rb +4 -0
  36. data/test/dummy/config/initializers/session_store.rb +3 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +23 -0
  39. data/test/dummy/config/routes.rb +56 -0
  40. data/test/dummy/config/secrets.yml +22 -0
  41. data/test/dummy/config/whmcs.yml +15 -0
  42. data/test/dummy/log/development.log +0 -0
  43. data/test/dummy/public/404.html +67 -0
  44. data/test/dummy/public/422.html +67 -0
  45. data/test/dummy/public/500.html +66 -0
  46. data/test/dummy/public/favicon.ico +0 -0
  47. data/test/test_helper.rb +15 -0
  48. data/test/whmcs_test.rb +7 -0
  49. metadata +184 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f2bc65a8ff5dd9ea70567790aa507ed77c01060
4
+ data.tar.gz: 8c39f3ad2f7c0cf0e6d0c0d9b8ab95550f27af69
5
+ SHA512:
6
+ metadata.gz: d85ef8cc744fc624291864e23f3547d1853332c0036c4bfec044ba39cbdf3633e85c98bf53ec43c2d5c011d7c6fd9889554a03e0c07d0dabbdc9966269ee7865
7
+ data.tar.gz: 72d414fbc9f96d2765691130f460038ad41db0e1b9920596f0332abb0c6e34072dada5eba9ca20ca24d592855c1ff63bef71aecb1907b3325008fdc9f4a036e8
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ = Whmcs API
2
+
3
+ Ruby library for the WHMCS API ({API Reference}[http://docs.whmcs.com/API]).
4
+
5
+ == Instalation
6
+
7
+ WHMCS API library requires Ruby 1.9.3 or later. To install, add this line to your
8
+ +Gemfile+ and run <tt>bundle install</tt>:
9
+
10
+ gem 'whmcs_api', require: 'whmcs'
11
+
12
+ You need use <tt>require: 'whmcs'</tt> parameter to automatically include the library. It's need, becouse
13
+ the gem and the library have different names.
14
+
15
+
16
+ == Configuration
17
+
18
+ By default module uses configuration from <tt>config/whmcs.yml</tt> file. To generate it run:
19
+
20
+ rake whmcs:install
21
+
22
+ This command generates file <tt>config/whmcs.yml</tt> which contains configuration for each environment
23
+
24
+ default: &default
25
+ api_user: xxxxxxxxx
26
+ api_password: xxxxxxxxx
27
+ api_url: https://your.domain.com/includes/api.php
28
+ http_auth_user: xxx
29
+ http_auth_password: xxx
30
+
31
+ production:
32
+ <<: *default
33
+
34
+ development:
35
+ <<: *default
36
+
37
+ test:
38
+ <<: *default
39
+
40
+ Fill in +api_user+, +api_password+ and +api_url+ for each environment or use +default+ template. If your
41
+ WHMCS installation uses Basic HTTP Auth, use +http_auth_user+ and +http_auth_password+ options.
42
+
43
+ You can configure module at runtime if you need:
44
+
45
+ Whmcs.configure do |config|
46
+ config.api_user = 'xxxxx'
47
+ config.api_password = 'xxxxxx'
48
+ config.api_url = 'https://your.domain.com/includes/api.php'
49
+ end
50
+
51
+ or set each configuration option separately:
52
+
53
+ Whmcs.config.api_user = 'xxxxxx'
54
+
55
+ == Usage
56
+
57
+ To use library you need to create an instance of Whmcs::Api class and call it methods:
58
+
59
+ @api = Whmcs::Api.new
60
+ @api.execute_command(:getclientsdetails, clientid: 2803)
61
+
62
+ Last line returns a result hash:
63
+ {:result=>"success",
64
+ :userid=>"2803",
65
+ :id=>"2803",
66
+ :firstname=>"Bogdan",
67
+ :lastname=>"Guban",
68
+ :fullname=>"Bogdan Guban"
69
+ ...
70
+
71
+ or +false+ if an error was occurred. All error messages stores in an +errors+ method.
72
+ +errors+ method is a functionality of <tt>ActiveModel::Validations</tt> module.
73
+
74
+ @api.errors.messages
75
+
76
+ returns:
77
+
78
+ {:base=>["Client Not Found"]}
79
+
80
+ If you prefer to work with exceptions you can use functions with <tt>!</tt> suffix. For example:
81
+
82
+ begin
83
+ @api = Whmcs::Api.new
84
+ @api.execute_command!(:getclientsdetails, clientid: 10000)
85
+ rescue Exception => e
86
+ puts e.message # => Client Not Found
87
+ end
88
+
89
+
90
+ ====== TODO
91
+
92
+ - Write tests
93
+ - Write documentation
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Whmcs'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,15 @@
1
+ default: &default
2
+ api_user: xxxxxxxxx
3
+ api_password: xxxxxxxxx
4
+ api_url: https://your.domain.com/includes/api.php
5
+ http_auth_user: xxx
6
+ http_auth_password: xxx
7
+
8
+ production:
9
+ <<: *default
10
+
11
+ development:
12
+ <<: *default
13
+
14
+ test:
15
+ <<: *default
@@ -0,0 +1,10 @@
1
+ namespace :whmcs do
2
+
3
+ desc 'Configure environment to use whmcs plugin'
4
+ task :install do
5
+ # copy configuration file template to config directory
6
+ FileUtils.cp(File.expand_path('../config/whmcs.yml', __FILE__), File.expand_path('config', Rails.root))
7
+ puts 'Copied file whmcs.yml to config path'
8
+ end
9
+
10
+ end
data/lib/whmcs.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'whmcs/railtie'
2
+ require 'whmcs/configuration'
3
+ require 'curb'
4
+ require 'php_serialize'
5
+
6
+ module Whmcs
7
+ autoload :Api, 'whmcs/api'
8
+
9
+ # Gives access to the current configuration
10
+ def self.config
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ # Allows easy setting of multiple configuration options. See Configuration
15
+ # for all available options.
16
+ def self.configure
17
+ config = self.config
18
+ yield(config)
19
+ end
20
+ end
data/lib/whmcs/api.rb ADDED
@@ -0,0 +1,86 @@
1
+ module Whmcs
2
+
3
+ class Api
4
+ include ActiveModel::Validations
5
+
6
+ def execute_command!(command, params={})
7
+ # Clear errors from previous request
8
+ errors.clear
9
+
10
+ # Set local variables
11
+ @command = command.to_s
12
+ @params = process_params(params)
13
+
14
+
15
+ # Assemble request_options
16
+ request_options = {
17
+ action: @command,
18
+ username: Whmcs.config.api_user,
19
+ password: Digest::MD5.hexdigest(Whmcs.config.api_password),
20
+ responsetype: 'json'
21
+ }
22
+ request_options.merge! @params
23
+
24
+ begin
25
+ # send curl request to the Whmcs server
26
+ curl = Curl::Easy.new(Whmcs.config.api_url)
27
+ curl.ssl_verify_peer = false
28
+ curl.ssl_verify_host=0
29
+ if Whmcs.config.http_auth_user.present?
30
+ curl.http_auth_types = :basic
31
+ curl.username = Whmcs.config.http_auth_user
32
+ curl.password = Whmcs.config.http_auth_password
33
+ end
34
+ curl.http_post(request_options.to_query)
35
+ curl.perform
36
+
37
+ # handle response
38
+ result = JSON.parse(curl.body_str, symbolize_names: true)
39
+ rescue Exception => e
40
+ # If error occurs, it adds to instance errors
41
+ errors.add(:base, e.message)
42
+ raise e
43
+ end
44
+
45
+ # check if error occurred
46
+ if result[:result] == 'error'
47
+ errors.add(:base, result[:message])
48
+ raise StandardError, result[:message]
49
+ end
50
+
51
+ # If all pass success, returns a result hash
52
+ result
53
+ end
54
+
55
+ # Method provides the same functionality as #execute_command! except
56
+ # it does not raises exceptions. When error occurs, it just returns +false+.
57
+ # So if method returns +false+, errors message should be in #errors
58
+ #
59
+ # == Example
60
+ #
61
+ # @api = Whmcs::Api.new
62
+ # @api.execute_command('some command', some_param: 'some value') # => false
63
+ # @api.errors.messages # => {:command=>["'some command' is invalid command"]}
64
+ #
65
+
66
+ def execute_command(command, options={})
67
+ begin
68
+ return execute_command!(command, options)
69
+ rescue
70
+ return false
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ # converts params keys to symbols, joins array values and encodes hashes
77
+ def process_params(options)
78
+ options = options.clone
79
+ options.each do |key, value|
80
+ options[key] = value.join(',') if value.is_a?(Array)
81
+ options[key] = Base64.encode64(PHP.serialize(value)) if value.is_a?(Hash)
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,26 @@
1
+ module Whmcs
2
+
3
+ class Configuration
4
+
5
+ attr_accessor :api_user,
6
+ :api_password,
7
+ :api_url,
8
+ :http_auth_user,
9
+ :http_auth_password
10
+
11
+ def initialize
12
+ if defined?(Rails) and File.exist?(File.expand_path('config/whmcs.yml', Rails.root))
13
+ config = YAML.load_file(File.expand_path('config/whmcs.yml', Rails.root))[Rails.env]
14
+
15
+ @api_user = config['api_user'] if config['api_user'].present?
16
+ @api_password = config['api_password'] if config['api_password'].present?
17
+ @api_url = config['api_url'] if config['api_url'].present?
18
+
19
+ @http_auth_user = config['http_auth_user'] if config['http_auth_user'].present?
20
+ @http_auth_password = config['http_auth_password'] if config['http_auth_password'].present?
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ module Whmcs
3
+ class Railtie < ::Rails::Railtie
4
+ railtie_name :whmcs
5
+
6
+ rake_tasks do
7
+ load 'tasks/whmcs_tasks.rake'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Whmcs
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')