blesta 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
5
+ *~
6
+ doc/
7
+ TODO
8
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ env: RAILS_ENV=test
3
+ script: bundle exec rake
4
+ notifications:
5
+ email: false
6
+ rvm:
7
+ - 1.9.3
8
+ - 1.9.2
9
+ - 1.8.7
10
+ branches:
11
+ only:
12
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Site5.com <http://www.site5.com/>
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.md ADDED
@@ -0,0 +1,100 @@
1
+ Blesta [![Blesta Build Status][Build Icon]][Build Status]
2
+ =========================================================
3
+ A Ruby library for working with the [Blesta Reseller API][].
4
+
5
+ Blesta has been tested on MRI 1.8.7, MRI 1.9.2 and MRI 1.9.3.
6
+
7
+ [Build Status]: http://travis-ci.org/site5/blesta
8
+ [Build Icon]: https://secure.travis-ci.org/site5/blesta.png?branch=master
9
+ [Blesta Reseller API]: http://www.blesta.com/wp-content/uploads/2009/12/Blesta_Reseller_API_Documentation.pdf
10
+
11
+ Install
12
+ -------
13
+
14
+ gem install blesta
15
+ bundle install
16
+
17
+ Configuration
18
+ -------------
19
+
20
+ ```ruby
21
+ require 'blesta'
22
+
23
+ Blesta.config do |c|
24
+ c.base_uri 'https://blestaapi.com' # Root level URI for blesta API
25
+ c.uid 'uid' # Blesta reseller UID
26
+ c.password 'topsecret' # Blesta reseller password
27
+ end
28
+ ```
29
+
30
+ Usage
31
+ -----
32
+
33
+ Get all licenses:
34
+
35
+ ```ruby
36
+ license = Blesta::License.new
37
+ license.all
38
+ ```
39
+
40
+ Search for a specific license:
41
+
42
+ ```ruby
43
+ license.search "mydomain.com", :type => "domain"
44
+ license.search "1234567890AB", :type => "license"
45
+ ```
46
+
47
+ Get the amount of credit in USD on the reseller account:
48
+
49
+ ```ruby
50
+ credit = Blesta::Credit.new
51
+ credit.get_amount
52
+ ```
53
+
54
+ Get a list of all of the packages this reseller account may add:
55
+
56
+ ```ruby
57
+ packages = Blesta::Package.new
58
+ packages.all
59
+ ```
60
+
61
+ Supported Methods
62
+ -----------------
63
+
64
+ This gem implements the blesta reseller's API v1.0.
65
+
66
+ It includes:
67
+
68
+ * *Get Credit.*
69
+ * *Get Licenses.*
70
+ * *Get Packages.*
71
+ * *Add License.*
72
+ * *Cancel License.*
73
+ * *Suspend License.*
74
+ * *Unsuspend License.*
75
+ * *Search Licenses.*
76
+ * *Update Domain.*
77
+
78
+ Tests
79
+ -----
80
+
81
+ Blesta uses rspec for tests. To run:
82
+
83
+ bundle exec rake
84
+
85
+ Note on Patches/Pull Requests
86
+ -----------------------------
87
+
88
+ * Fork the project.
89
+ * Make your feature addition or bug fix.
90
+ * Add tests for it. This is important so I don't break it in a
91
+ future version unintentionally.
92
+ * Commit, do not mess with rakefile, version, or history.
93
+ (if you want to have your own version, that is fine but bump version in a
94
+ commit by itself I can ignore when I pull)
95
+ * Send me a pull request. Bonus points for topic branches.
96
+
97
+ Copyright
98
+ ---------
99
+
100
+ Copyright (c) 2012 Site5.com. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ rescue LoadError
5
+ puts "Please install rspec (bundle install)"
6
+ exit
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new :spec
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ task :default => [:spec]
data/blesta.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "blesta/support/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "blesta"
7
+ s.version = Blesta::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Justin Mazzi"]
10
+ s.email = ["jmazzi@site5.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A Ruby library for working with the Blesta Resellers API}
13
+ s.description = %q{A Ruby library for working with the Blesta Resellers API}
14
+
15
+ s.rubyforge_project = "blesta"
16
+
17
+ s.add_runtime_dependency 'faraday', '~> 0.8.4'
18
+ s.add_runtime_dependency 'faraday_middleware', '~> 0.9.0'
19
+ s.add_runtime_dependency 'json', '~> 1.7.3'
20
+
21
+ s.add_development_dependency 'rspec', '~> 2.10.0'
22
+ s.add_development_dependency 'awesome_print', '~> 1.0.2'
23
+ s.add_development_dependency 'rake', '~> 0.9.2.2'
24
+ s.add_development_dependency 'vcr', '~> 2.3.0'
25
+ s.add_development_dependency 'fakeweb', '~> 1.3'
26
+ s.add_development_dependency 'activesupport', "~> 3.2.9"
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+
31
+ s.require_paths = ["lib"]
32
+ end
data/lib/blesta.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'blesta/support/version'
4
+
5
+ module Blesta
6
+ autoload :Config, 'blesta/support/config'
7
+ autoload :Base, 'blesta/support/base'
8
+ autoload :License, 'blesta/license'
9
+ autoload :Package, 'blesta/package'
10
+ autoload :Credit, 'blesta/credit'
11
+
12
+ # Config
13
+ extend self
14
+ attr_accessor :configuration
15
+
16
+ attr_accessor :configuration_file
17
+
18
+ self.configuration ||= Blesta::Config.new
19
+
20
+ # Public: Specificy the config via block
21
+ #
22
+ # base_uri - URL of your Blesta Resellers API`
23
+ # uid - API UID
24
+ # password - API Password
25
+ #
26
+ # Examples
27
+ #
28
+ # Blesta.config do |c|
29
+ # c.base_uri 'http://test.com/reseller'
30
+ # c.uid '1234'
31
+ # c.password 'mypass'
32
+ # end
33
+ #
34
+ # Returns a Hash
35
+ def config
36
+ yield self.configuration if block_given?
37
+ self.configuration.config
38
+ end
39
+
40
+ # Public: Reset the config (aka, clear it)
41
+ def reset_config
42
+ self.configuration = Blesta::Config.new
43
+ end
44
+
45
+ end
@@ -0,0 +1,13 @@
1
+ module Blesta
2
+ class Credit < Base
3
+
4
+ # Public: Fetch the amount of credit in USD on the reseller account
5
+ #
6
+ # Returns the amount of credit in USD
7
+ def get_amount
8
+ response = request(:get, '', {:query => {:action=> 'getcredit'}})
9
+ response_data_or_error response
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,116 @@
1
+ module Blesta
2
+ class License < Base
3
+ TERM_OPTIONS = ["owned", "monthly", "yearly"]
4
+ TERM_OPTIONS_ERROR = "valid term options are owned, monthly, yearly"
5
+
6
+ # Public: Sets the test mode.
7
+ attr_writer :test_mode
8
+
9
+ # Public: Gets the test mode value. Default value is false.
10
+ def test_mode
11
+ @test_mode || false
12
+ end
13
+
14
+ # Public: Returns all licenses
15
+ #
16
+ # Returns a hash where the keys are the license IDs
17
+ def all
18
+ response = request(:get, '', {:query => {:action=> 'getlicenses'}})
19
+ response_data_or_error response
20
+ end
21
+
22
+ # Public: Adds a new license
23
+ #
24
+ # term - The term that should apply to this license. Options are:
25
+ # owned, monthly, yearly
26
+ # options - options hash. It should include a domain and prorate option
27
+ # is optional (defaul is false)
28
+ #
29
+ # Returns a Hash that contains the license and domain keys
30
+ def add(term, options={})
31
+ raise ArgumentError, TERM_OPTIONS_ERROR unless TERM_OPTIONS.include?(term)
32
+
33
+ license_params = { :term =>term, :action=>'addlicense',
34
+ :test_mode => test_mode }.merge(options)
35
+ response = request(:get, '', {:query => license_params})
36
+ response_data_or_error response do
37
+ response['data'].first
38
+ end
39
+ end
40
+
41
+ # Public: Permanently cancels the given license
42
+ #
43
+ # license_id - The license key for this license
44
+ #
45
+ # Returns nil. In case of error, it returns a hash that contains the
46
+ # error code and message
47
+ def cancel(license_id)
48
+ basic_request(license_id, "cancellicense")
49
+ end
50
+
51
+ # Public: Indefinitely suspend a license
52
+ #
53
+ # license_id - The license key for this license
54
+ #
55
+ # Returns nil. In case of error, it returns a hash that contains the
56
+ # error code and message
57
+ def suspend(license_id)
58
+ basic_request(license_id, "suspendlicense")
59
+ end
60
+
61
+ # Public: Unsuspend a previously suspended license
62
+ #
63
+ # license_id - The license key for this license
64
+ #
65
+ # Returns nil. In case of error, it returns a hash that contains the
66
+ # error code and message
67
+ def unsuspend(license_id)
68
+ basic_request license_id, "unsuspendlicense"
69
+ end
70
+
71
+ # Public: Updates the domain for this license
72
+ #
73
+ # license_id - The license you wish to update
74
+ # options - options hash. It should include a domain.
75
+ #
76
+ # Returns nil. In case of error, it returns a hash that contains the
77
+ # error code and message
78
+ def update_domain(license_id, options={})
79
+ update_params = { :action => "updatedomain",
80
+ :license => license_id }.merge(options)
81
+ response = request(:get, '', {:query => update_params})
82
+ response_data_or_error response
83
+ end
84
+
85
+ # Public: Search through all available licenses for a match
86
+ #
87
+ # term - either a domain or license, depending on the search type
88
+ # options - hash options (default: {}):
89
+ # :type - domain or license as string (required)
90
+ # :page - the page of results to return. Results are limited to
91
+ # 25 per page (optional)
92
+ #
93
+ # Returns a list of licenses that matched the criteria
94
+ def search(term, options={})
95
+ search_params = { :action => "searchlicenses",
96
+ :search => { options[:type] => term}}.merge(options)
97
+ response = request(:get, '', {:query => search_params})
98
+ response_data_or_error response
99
+ end
100
+
101
+ private
102
+ # Private: Makes a basic license request
103
+ #
104
+ # license_id: The license id
105
+ # action: The specific action that you want to perform
106
+ #
107
+ # Return the response data or, in case of error, it returns a hash that
108
+ # contains the error code and message
109
+ def basic_request(license_id, action)
110
+ params = { :action => action, :license => license_id,
111
+ :test_mode => test_mode.to_s }
112
+ response = request(:get, '', {:query => params})
113
+ response_data_or_error response
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,13 @@
1
+ module Blesta
2
+ class Package < Base
3
+
4
+ # Public: Fetches a list of all of the packages this reseller account may
5
+ #
6
+ # Returns a list of all of the packages
7
+ def all
8
+ response = request(:get, '', {:query => {:action=> 'getpackages'}})
9
+ response_data_or_error response
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,74 @@
1
+ module Blesta
2
+ class Base
3
+
4
+ def initialize
5
+ @conn = Faraday.new(:url => Blesta.config[:base_uri]) do |c|
6
+ c.request :url_encoded
7
+ c.response :json
8
+ c.adapter :net_http
9
+ end
10
+ end
11
+
12
+ # Public: Indicates whether the request was successful or not
13
+ #
14
+ # Returns boolean
15
+ def successful?
16
+ @success
17
+ end
18
+
19
+ # Public: authentication parameters
20
+ #
21
+ # Returns a hash
22
+ def authentication_params
23
+ {
24
+ :uid => Blesta.config[:uid],
25
+ :password => Blesta.config[:password]
26
+ }
27
+ end
28
+
29
+ # Public: Peforms an HTTP Request
30
+ #
31
+ # request_method - The HTTP request method for the #request.
32
+ # (:get/:post/:delete etc)
33
+ # path - URL path
34
+ # options - HTTP query params
35
+ #
36
+ # Examples
37
+ #
38
+ # request :get, '/something.json' # GET /seomthing.json
39
+ # request :put, '/something.json', :something => 1
40
+ # # PUT /something.json?something=1
41
+ #
42
+ # Returns the parsed json response
43
+ def request(request_method, path, options = {})
44
+ check_config!
45
+
46
+ params = (options[:query] || {}).merge authentication_params
47
+ response = @conn.send(request_method, path, params)
48
+ @success = (response.body['response_code'].to_s == '200')
49
+
50
+ response.body
51
+ end
52
+
53
+ private
54
+
55
+ # Private: Raises an error if a request is made without first calling
56
+ # Blesta.config
57
+ def check_config!
58
+ raise NoConfig, "Blesta.config must be specified" if Blesta.config.empty?
59
+ end
60
+
61
+ # Private: returns the response data if request was succesfull
62
+ # or a hash that contains the error code and message
63
+ #
64
+ # response - the blesta response
65
+ def response_data_or_error(response)
66
+ if successful?
67
+ block_given? ? yield : response['data']
68
+ else
69
+ { :error_code => response["response_code"],
70
+ :message => response["response_text"] }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,46 @@
1
+ module Blesta
2
+ # Holds the configuration for Blesta
3
+ class Config
4
+ attr_accessor :config
5
+
6
+ def initialize
7
+ @config = {}
8
+ end
9
+
10
+ # Public: get the configuration value
11
+ #
12
+ # configuration - the configuration key
13
+ #
14
+ # Returns the configuration value or nil if it has not been set
15
+ def [](configuration)
16
+ @config[configuration]
17
+ end
18
+
19
+ # Public: set the base uri for Blesta resellers API
20
+ #
21
+ # value - uri for Blesta resellers API
22
+ #
23
+ # Returns the given base uri
24
+ def base_uri(value)
25
+ @config[:base_uri] = value
26
+ end
27
+
28
+ # Public: set the Blesta reseller UID
29
+ #
30
+ # value - UID for Blesta resellers API
31
+ #
32
+ # Returns the given UID
33
+ def uid(value)
34
+ @config[:uid] = value
35
+ end
36
+
37
+ # Public: set the Blesta reseller password
38
+ #
39
+ # value - password for Blesta resellers API
40
+ #
41
+ # Returns the given password
42
+ def password(value)
43
+ @config[:password] = value
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Blesta
2
+ VERSION ='0.0.1'
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blesta::Credit do
4
+ let(:faraday_request_stub) { Faraday::Adapter::Test::Stubs.new }
5
+ let(:credit) { Blesta::Credit.new }
6
+
7
+ before do
8
+ credit.instance_variable_get(:@conn).builder.
9
+ swap Faraday::Adapter::NetHttp,
10
+ Faraday::Adapter::Test, faraday_request_stub
11
+ end
12
+
13
+ def stub_faraday_request(params, response)
14
+ query_param = build_query_params params
15
+ faraday_request_stub.get(query_param) { [200, {}, response] }
16
+ end
17
+
18
+ describe '#get_amount' do
19
+ let (:params) { {:action => "getcredit"} }
20
+
21
+ context 'on success' do
22
+ it 'should return the amount of credit in USD' do
23
+ response = blesta_successful_response "10.00"
24
+ stub_faraday_request(params, response)
25
+
26
+ response = credit.get_amount
27
+ faraday_request_stub.verify_stubbed_calls
28
+ response.should == "10.00"
29
+ end
30
+ end
31
+
32
+ context 'on error' do
33
+ it 'returns a hash that contains the error code and message' do
34
+ params = {"action" => "getcredit" }
35
+ response = blesta_unsuccessful_response "100","Authentication failed"
36
+ stub_faraday_request(params, response)
37
+
38
+ response = credit.get_amount
39
+ credit.should_not be_successful
40
+ response[:error_code].should == "100"
41
+ response[:message].should == "Authentication failed"
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,275 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blesta::License do
4
+ let(:faraday_request_stub) { Faraday::Adapter::Test::Stubs.new }
5
+ let(:license) { Blesta::License.new }
6
+ before do
7
+ license.instance_variable_get(:@conn).builder.
8
+ swap Faraday::Adapter::NetHttp,
9
+ Faraday::Adapter::Test, faraday_request_stub
10
+ end
11
+
12
+ def stub_faraday_request(params, response)
13
+ query_param = build_query_params params
14
+ faraday_request_stub.get(query_param) { [200, {}, response] }
15
+ end
16
+
17
+ describe '#all' do
18
+ let (:params) { {:action => "getlicenses"} }
19
+
20
+ context "on success" do
21
+ before :each do
22
+ stub_faraday_request(params, getlicenses_response)
23
+ end
24
+
25
+ it 'should fetch all licenses' do
26
+ licenses = license.all
27
+ faraday_request_stub.verify_stubbed_calls
28
+ end
29
+
30
+ it 'returns a Hash where the keys are the license IDs' do
31
+ licenses = license.all()
32
+ licenses.should be_a Hash
33
+ licenses.keys.should == ["ABCDEFGH12345678"]
34
+ faraday_request_stub.verify_stubbed_calls
35
+ end
36
+ end
37
+
38
+ context 'on error' do
39
+ it 'returns a hash that contains the error code and message' do
40
+ response = blesta_unsuccessful_response "100", "Authentication failed"
41
+ stub_faraday_request(params, response)
42
+
43
+ response = license.all()
44
+ license.should_not be_successful
45
+ response[:error_code].should == "100"
46
+ response[:message].should == "Authentication failed"
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#add' do
52
+ let (:domain) { 'mydomain.com' }
53
+ let (:params) { {:action=>"addlicense", :term=>"monthly",
54
+ :domain=>"mydomain.com", :test_mode => "false"} }
55
+
56
+ context "on success" do
57
+ before do
58
+ response = blesta_successful_response [{"license"=>"ABCDEFGH12345678",
59
+ "domain"=>"mydomain.com"}]
60
+ stub_faraday_request(params, response)
61
+ end
62
+
63
+ it 'requires a term that is owned, monthly or yearly' do
64
+ Blesta::License::TERM_OPTIONS.each do |t|
65
+ expect { license.add(t, domain) }.to_not raise_error(ArgumentError)
66
+ end
67
+ end
68
+
69
+ it 'raises an error when the term is not valid' do
70
+ error_message = "valid term options are owned, monthly, yearly"
71
+ expect{ license.add('invalid_term', domain) }.
72
+ to raise_error(ArgumentError, error_message)
73
+ end
74
+
75
+ it 'adds a new license' do
76
+ license.add("monthly", :domain => domain )
77
+ faraday_request_stub.verify_stubbed_calls
78
+ end
79
+
80
+ it 'returns a Hash that contains the license id and domain' do
81
+ response = license.add("monthly", :domain => domain)
82
+ response['license'].should == "ABCDEFGH12345678"
83
+ response['domain'].should == "mydomain.com"
84
+ end
85
+ end
86
+
87
+ context 'on error' do
88
+ it 'returns a hash that contains the error code and message' do
89
+ response = blesta_unsuccessful_response "103",
90
+ "License already exists for that domain"
91
+ stub_faraday_request(params, response)
92
+
93
+ response = license.add("monthly", :domain => domain)
94
+ license.should_not be_successful
95
+ response[:error_code].should == "103"
96
+ response[:message].should == "License already exists for that domain"
97
+ end
98
+ end
99
+
100
+ context 'on test mode' do
101
+ it 'sends the test mode parameter' do
102
+ test_mode_params = params.merge({:test_mode => "true"})
103
+ response = blesta_successful_response []
104
+ stub_faraday_request(test_mode_params, response)
105
+
106
+ license.test_mode = true
107
+ license.add("monthly", :domain => domain)
108
+ faraday_request_stub.verify_stubbed_calls
109
+ end
110
+ end
111
+ end
112
+
113
+ describe '#cancel' do
114
+ let (:params) { {:action=>"cancellicense", :license => "ABCDEFGH12345678",
115
+ :test_mode => "false"} }
116
+
117
+ context 'on success' do
118
+ it 'cancels the license' do
119
+ response = blesta_successful_response nil
120
+ stub_faraday_request(params, response)
121
+
122
+ license.cancel 'ABCDEFGH12345678'
123
+ faraday_request_stub.verify_stubbed_calls
124
+ end
125
+
126
+ context 'on test mode' do
127
+ it 'sends the test mode parameter' do
128
+ test_mode_params = params.merge({:test_mode => "true"})
129
+ response = blesta_successful_response nil
130
+ stub_faraday_request(test_mode_params, response)
131
+
132
+ license.test_mode = true
133
+ license.cancel 'ABCDEFGH12345678'
134
+ faraday_request_stub.verify_stubbed_calls
135
+ end
136
+ end
137
+ end
138
+
139
+ context 'on error' do
140
+ it 'returns a hash that contains the error code and message' do
141
+ response = blesta_unsuccessful_response "102", "License does not exist"
142
+ stub_faraday_request(params, response)
143
+
144
+ response = license.cancel 'ABCDEFGH12345678'
145
+ license.should_not be_successful
146
+ response[:error_code].should == "102"
147
+ response[:message].should == "License does not exist"
148
+ end
149
+ end
150
+
151
+ end
152
+
153
+ describe '#suspend' do
154
+ let (:params) { {:action=>"suspendlicense", :license => "ABCDEFGH12345678",
155
+ :test_mode => "false"} }
156
+
157
+ context 'on success' do
158
+ it 'suspends the license' do
159
+ response = blesta_successful_response nil
160
+ stub_faraday_request(params, response)
161
+
162
+ license.suspend 'ABCDEFGH12345678'
163
+ faraday_request_stub.verify_stubbed_calls
164
+ end
165
+ end
166
+
167
+ context 'on error' do
168
+ it 'returns a hash that contains the error code and message' do
169
+ response = blesta_unsuccessful_response "102", "License does not exist"
170
+ stub_faraday_request(params, response)
171
+
172
+ response = license.suspend 'ABCDEFGH12345678'
173
+ license.should_not be_successful
174
+ response[:error_code].should == "102"
175
+ response[:message].should == "License does not exist"
176
+ end
177
+ end
178
+ end
179
+
180
+ describe '#unsuspend' do
181
+ let(:params) { {:action=>"unsuspendlicense", :license => "ABCDEFGH12345678",
182
+ :test_mode => "false"} }
183
+
184
+ context 'on success' do
185
+ it 'unsuspends the license' do
186
+ response = blesta_successful_response nil
187
+ stub_faraday_request(params, response)
188
+
189
+ license.unsuspend 'ABCDEFGH12345678'
190
+ faraday_request_stub.verify_stubbed_calls
191
+ end
192
+ end
193
+
194
+ context 'on error' do
195
+ it 'returns a hash that contains the error code and message' do
196
+ response = blesta_unsuccessful_response "102", "License does not exist"
197
+ stub_faraday_request(params, response)
198
+
199
+ response = license.unsuspend 'ABCDEFGH12345678'
200
+ license.should_not be_successful
201
+ response[:error_code].should == "102"
202
+ response[:message].should == "License does not exist"
203
+ end
204
+ end
205
+ end
206
+
207
+
208
+ describe '#update_domain' do
209
+ let (:params) { {:action=>"updatedomain", :license => 'ABCDEFGH12345678',
210
+ :domain => 'newdomain.com' } }
211
+
212
+ context 'on success' do
213
+ it 'updates the domain for a given license' do
214
+ response = blesta_successful_response nil
215
+ stub_faraday_request(params, response)
216
+
217
+ license.update_domain('ABCDEFGH12345678', :domain => 'newdomain.com')
218
+ faraday_request_stub.verify_stubbed_calls
219
+ end
220
+ end
221
+
222
+ context 'on error' do
223
+ it 'returns a hash that contains the error code and message' do
224
+ response = blesta_unsuccessful_response "103",
225
+ "License already exists for that domain"
226
+ stub_faraday_request(params, response)
227
+
228
+ response = license.update_domain('ABCDEFGH12345678',
229
+ :domain => 'newdomain.com')
230
+ license.should_not be_successful
231
+ response[:error_code].should == "103"
232
+ response[:message].should == "License already exists for that domain"
233
+ end
234
+ end
235
+ end
236
+
237
+ describe "#search" do
238
+ context 'on success' do
239
+ it 'returns a list of licenses that match the given domains' do
240
+ params = {"action" => "searchlicenses", "search" =>
241
+ { "domain" => "mydomain.com" }}
242
+ stub_faraday_request(params, getlicenses_search_response)
243
+
244
+ response = license.search "mydomain.com", :type => 'domain'
245
+ faraday_request_stub.verify_stubbed_calls
246
+ license.should be_successful
247
+ response.should have(2).items
248
+ end
249
+
250
+ it 'returns a list of licenses that match the given licenses id' do
251
+ params = {"action" => "searchlicenses", "search" =>
252
+ { "license" => "ABCDEFGH12345678" }}
253
+ stub_faraday_request(params, getlicenses_search_response)
254
+
255
+ response = license.search "ABCDEFGH12345678", :type => 'license'
256
+ faraday_request_stub.verify_stubbed_calls
257
+ license.should be_successful
258
+ response.should have(2).items
259
+ end
260
+ end
261
+ context 'on error' do
262
+ it 'returns a hash that contains the error code and message' do
263
+ params = {"action" => "searchlicenses", "search" =>
264
+ { "license" => "ABCDEFGH12345678" }}
265
+ response = blesta_unsuccessful_response "100","Authentication failed"
266
+ stub_faraday_request(params, response)
267
+
268
+ response = license.search "ABCDEFGH12345678", :type => 'license'
269
+ license.should_not be_successful
270
+ response[:error_code].should == "100"
271
+ response[:message].should == "Authentication failed"
272
+ end
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blesta::Package do
4
+ let(:faraday_request_stub) { Faraday::Adapter::Test::Stubs.new }
5
+ let(:package) { Blesta::Package.new }
6
+
7
+ before do
8
+ package.instance_variable_get(:@conn).builder.
9
+ swap Faraday::Adapter::NetHttp,
10
+ Faraday::Adapter::Test, faraday_request_stub
11
+ end
12
+
13
+ def stub_faraday_request(params, response)
14
+ query_param = build_query_params params
15
+ faraday_request_stub.get(query_param) { [200, {}, response] }
16
+ end
17
+
18
+ describe '#all' do
19
+ let (:params) { {:action => "getpackages"} }
20
+
21
+ context 'on success' do
22
+ it 'should return a list of all the packages' do
23
+ response = blesta_successful_response [{"name" => "Blesta Monthly",
24
+ "type" => "license","term" => "monthly","price" => "15.95"}]
25
+ stub_faraday_request(params, response)
26
+
27
+ response = package.all
28
+ faraday_request_stub.verify_stubbed_calls
29
+ end
30
+ end
31
+
32
+ context 'on error' do
33
+ it 'returns a hash that contains the error code and message' do
34
+ params = {"action" => "getpackages" }
35
+ response = blesta_unsuccessful_response "100","Authentication failed"
36
+ stub_faraday_request(params, response)
37
+
38
+ response = package.all
39
+ package.should_not be_successful
40
+ response[:error_code].should == "100"
41
+ response[:message].should == "Authentication failed"
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blesta::Base do
4
+ let(:base) { Blesta::Base.new }
5
+ let(:successful_response) { JSON.generate({ :response_text => "OK",
6
+ :response_code => "200" }) }
7
+ let(:bad_response) { JSON.generate({:response_code => "100" }) }
8
+
9
+ describe "#request" do
10
+ it 'includes the blesta UID and password in the params' do
11
+ mock_blesta_request successful_response
12
+ base.request(:get, '/api')['response_text'].should == 'OK'
13
+ end
14
+
15
+ it 'merges any other request parameter' do
16
+ query_param = { :merge => 'true' }
17
+ mock_blesta_request successful_response, query_param
18
+ base.request(:get, '/api', :query => query_param )['response_text'].
19
+ should == 'OK'
20
+ end
21
+ end
22
+
23
+ describe "#successful?" do
24
+ context 'response_code is 200' do
25
+ it 'is true' do
26
+ mock_blesta_request successful_response
27
+ base.request(:get, '/api')
28
+ base.should be_successful
29
+ end
30
+ end
31
+ context 'response_code is different than 200' do
32
+ it 'is false' do
33
+ FakeWeb.clean_registry
34
+ mock_blesta_request bad_response
35
+ base.request(:get, '/api')
36
+ base.should_not be_successful
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "#authentication parameters" do
42
+ it "returns the authentication parameters given the Blesta configuration" do
43
+ parameters = {
44
+ :uid => Blesta.config[:uid],
45
+ :password => Blesta.config[:password]
46
+ }
47
+ base.authentication_params.should == parameters
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blesta::Config do
4
+ let(:config) { Blesta::Config.new }
5
+
6
+ describe '#new' do
7
+ it 'sets up the @config hash' do
8
+ config.config.should == {}
9
+ end
10
+ end
11
+
12
+ describe "#[]" do
13
+ it "behaves like an array" do
14
+ config.config[:something] = 1
15
+ config[:something].should == 1
16
+ end
17
+ end
18
+
19
+ describe "#base_uri" do
20
+ it "sets the base_uri" do
21
+ config.config[:base_uri].should be_nil
22
+ config.base_uri 'url'
23
+ config.config[:base_uri].should == 'url'
24
+ end
25
+ end
26
+
27
+ describe "#uid" do
28
+ it "sets the uid" do
29
+ config.config[:uid].should be_nil
30
+ config.uid '1234'
31
+ config.config[:uid].should == '1234'
32
+ end
33
+ end
34
+
35
+ describe "#password" do
36
+ it "sets the password" do
37
+ config.config[:password].should be_nil
38
+ config.password 'pass'
39
+ config.config[:password].should == 'pass'
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,90 @@
1
+ require 'blesta'
2
+ require 'vcr'
3
+ require 'fakeweb'
4
+ require 'json'
5
+
6
+ def mock_request(meth, path, options = {})
7
+ uri = URI.parse(Blesta.config[:base_uri])
8
+ url = "#{uri.scheme}://#{uri.host}:#{uri.port}#{path}"
9
+ FakeWeb.register_uri(meth, url, {:content_type => 'application/json'}.
10
+ merge(options))
11
+ end
12
+
13
+ RSpec.configure do |c|
14
+ c.before(:each) do
15
+ configure_for_tests
16
+ end
17
+ c.after(:each) do
18
+ Blesta.reset_config
19
+ end
20
+ end
21
+
22
+ def configure_for_tests
23
+ Blesta.config do |c|
24
+ c.uid "12345"
25
+ c.password "test"
26
+ c.base_uri "http://example.com"
27
+ end
28
+ end
29
+
30
+ def blesta_successful_response(data)
31
+ successful_response = {
32
+ "response_code" => 200,
33
+ "response_text" => "OK",
34
+ "data" => data
35
+ }
36
+ JSON.generate(successful_response)
37
+ end
38
+
39
+ def blesta_unsuccessful_response(code, message="error")
40
+ successful_response = {
41
+ "response_code" => code,
42
+ "response_text" => message,
43
+ "data" => nil
44
+ }
45
+ JSON.generate(successful_response)
46
+ end
47
+
48
+ def getlicenses_response
49
+ license_response = {
50
+ "ABCDEFGH12345678" => [{
51
+ "type" => "license",
52
+ "license" => "ABCDEFGH12345678",
53
+ "domain" => "domain.com",
54
+ "term" => "monthly",
55
+ "date_added" => "2009-07- 24",
56
+ "date_suspended" => "0000-00-00",
57
+ "date_renews" => "2009-10- 01",
58
+ }]
59
+ }
60
+ blesta_successful_response license_response
61
+ end
62
+
63
+ def getlicenses_search_response
64
+ search_response = [{
65
+ "id" => "616",
66
+ "domain" => "mydomain.com",
67
+ "license" => "ABCDEFGH01234567",
68
+ "last_callback" => "",
69
+ "term" => "0"
70
+ },{
71
+ "id" => "617",
72
+ "domain" => "test.com",
73
+ "license" => "ABCDEFGH01234568",
74
+ "last_callback" => "",
75
+ "term" => "0"
76
+ }]
77
+ blesta_successful_response search_response
78
+ end
79
+
80
+ def build_query_params(params={})
81
+ base = Blesta::Base.new
82
+ params = params.merge base.authentication_params
83
+ require 'active_support/core_ext'
84
+ "?#{params.to_query}"
85
+ end
86
+
87
+ def mock_blesta_request(body, params = {})
88
+ mock_request(:get, "/api" + build_query_params(params),
89
+ :status => [200, "OK"], :body => body)
90
+ end
metadata ADDED
@@ -0,0 +1,222 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blesta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Mazzi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.7.3
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.10.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.10.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: awesome_print
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.0.2
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.0.2
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.9.2.2
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.9.2.2
110
+ - !ruby/object:Gem::Dependency
111
+ name: vcr
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 2.3.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 2.3.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: fakeweb
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: '1.3'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '1.3'
142
+ - !ruby/object:Gem::Dependency
143
+ name: activesupport
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 3.2.9
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 3.2.9
158
+ description: A Ruby library for working with the Blesta Resellers API
159
+ email:
160
+ - jmazzi@site5.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .rspec
167
+ - .travis.yml
168
+ - Gemfile
169
+ - LICENSE
170
+ - README.md
171
+ - Rakefile
172
+ - blesta.gemspec
173
+ - lib/blesta.rb
174
+ - lib/blesta/credit.rb
175
+ - lib/blesta/license.rb
176
+ - lib/blesta/package.rb
177
+ - lib/blesta/support/base.rb
178
+ - lib/blesta/support/config.rb
179
+ - lib/blesta/support/version.rb
180
+ - spec/blesta/credit_spec.rb
181
+ - spec/blesta/license_spec.rb
182
+ - spec/blesta/package_spec.rb
183
+ - spec/blesta/support/base_spec.rb
184
+ - spec/blesta/support/config_spec.rb
185
+ - spec/spec_helper.rb
186
+ homepage: ''
187
+ licenses: []
188
+ post_install_message:
189
+ rdoc_options: []
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ segments:
199
+ - 0
200
+ hash: 1985824838570944957
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ! '>='
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ segments:
208
+ - 0
209
+ hash: 1985824838570944957
210
+ requirements: []
211
+ rubyforge_project: blesta
212
+ rubygems_version: 1.8.24
213
+ signing_key:
214
+ specification_version: 3
215
+ summary: A Ruby library for working with the Blesta Resellers API
216
+ test_files:
217
+ - spec/blesta/credit_spec.rb
218
+ - spec/blesta/license_spec.rb
219
+ - spec/blesta/package_spec.rb
220
+ - spec/blesta/support/base_spec.rb
221
+ - spec/blesta/support/config_spec.rb
222
+ - spec/spec_helper.rb