ivapi 1.0.4 → 1.1.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.
@@ -3,42 +3,48 @@ require 'ivapi/version'
3
3
 
4
4
  module Ivapi
5
5
  module Configuration
6
- VALID_OPTIONS_KEYS = [
7
- :adapter,
8
- :faraday_config_block,
9
- :username,
10
- :password,
11
- :user_agent,
12
- :server_id
13
- ].freeze
14
-
15
- DEFAULT_ADAPTER = Faraday.default_adapter
16
- DEFAULT_USER_AGENT = "Ivapi Ruby Gem #{Ivapi::VERSION}".freeze
17
-
18
- attr_accessor(*VALID_OPTIONS_KEYS)
19
-
20
- def self.extended(base)
21
- base.reset
6
+
7
+ attr_accessor :server_id, :user_agent, :connection_options,
8
+ :web_endpoint, :api_endpoint
9
+ attr_writer :username, :password
10
+
11
+ def self.keys
12
+ @keys ||= [
13
+ :api_endpoint,
14
+ :server_id,
15
+ :username,
16
+ :middleware,
17
+ :password,
18
+ :user_agent,
19
+ :connection_options
20
+ ]
22
21
  end
23
22
 
24
23
  def configure
25
24
  yield self
26
25
  end
27
26
 
28
- def options
29
- VALID_OPTIONS_KEYS.reduce({}) { |a, e| a.merge!(e => send(e)) }
27
+ # Reset configuration options to default values
28
+ def reset!
29
+ Ivapi::Configuration.keys.each do |key|
30
+ instance_variable_set(:"@#{key}", Ivapi::Default.options[key])
31
+ end
32
+ self
30
33
  end
34
+ alias setup reset!
31
35
 
32
- def faraday_config(&block)
33
- @faraday_config_block = block
36
+ def configure
37
+ yield self
34
38
  end
35
39
 
36
- def reset
37
- self.adapter = DEFAULT_ADAPTER
38
- self.username = nil
39
- self.password = nil
40
- self.server_id = nil
41
- self.user_agent = DEFAULT_USER_AGENT
40
+ private
41
+
42
+ def options
43
+ Hash[
44
+ Ivapi::Configuration.keys.map do |key|
45
+ [key, instance_variable_get(:"@#{key}")]
46
+ end
47
+ ]
42
48
  end
43
49
  end
44
50
  end
@@ -0,0 +1,86 @@
1
+ require 'faraday_middleware'
2
+ require 'ivapi/response/raise_error'
3
+ require 'ivapi/response/rename_keys'
4
+ require 'ivapi/version'
5
+
6
+ module Ivapi
7
+
8
+ # Default configuration options for {Client}
9
+ module Default
10
+ # Default API endpoint
11
+ API_ENDPOINT = 'https://api.iv.lt'.freeze
12
+
13
+ # Default User Agent header string
14
+ USER_AGENT = "Ivapi ruby gem v#{Ivapi::VERSION}".freeze
15
+
16
+ # In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder
17
+ RACK_BUILDER_CLASS = if defined?(Faraday::RackBuilder)
18
+ Faraday::RackBuilder
19
+ else
20
+ Faraday::Builder
21
+ end
22
+
23
+ # Default Faraday middleware stack
24
+ MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
25
+ builder.request :json
26
+ builder.use Ivapi::Response::RaiseError
27
+ builder.use FaradayMiddleware::FollowRedirects
28
+ builder.use FaradayMiddleware::Mashify
29
+ builder.use Ivapi::Response::RenameKeys
30
+ builder.use FaradayMiddleware::ParseJson
31
+ builder.adapter Faraday.default_adapter
32
+ end
33
+
34
+ class << self
35
+
36
+ # Configuration options
37
+ # @return [Hash]
38
+ def options
39
+ Hash[Ivapi::Configuration.keys.map { |key| [key, send(key)] }]
40
+ end
41
+
42
+ # Default GitHub username for Basic Auth from ENV
43
+ # @return [String]
44
+ def username
45
+ ENV['IVAPI_USERNAME']
46
+ end
47
+
48
+ def server_id
49
+ nil
50
+ end
51
+
52
+ # Default middleware stack for Faraday::Connection
53
+ # from {MIDDLEWARE}
54
+ # @return [String]
55
+ def middleware
56
+ MIDDLEWARE
57
+ end
58
+
59
+ # Default GitHub password for Basic Auth from ENV
60
+ # @return [String]
61
+ def password
62
+ ENV['IVAPI_PASSWORD']
63
+ end
64
+
65
+ def connection_options
66
+ {
67
+ headers: {
68
+ user_agent: user_agent
69
+ }
70
+ }
71
+ end
72
+
73
+ # Default User-Agent header string from ENV or {USER_AGENT}
74
+ # @return [String]
75
+ def user_agent
76
+ ENV['IVAPI_USER_AGENT'] || USER_AGENT
77
+ end
78
+
79
+ # Default API endpoint from ENV or {API_ENDPOINT}
80
+ # @return [String]
81
+ def api_endpoint
82
+ ENV['IVAPI_API_ENDPOINT'] || API_ENDPOINT
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,6 +1,24 @@
1
1
  module Ivapi
2
-
3
2
  class Error < StandardError
3
+ # Returns the appropriate Ivapi::Error sublcass based
4
+ # on status and response message.
5
+ #
6
+ # response - The Hash of HTTP response.
7
+ #
8
+ # Returns the Ivapi::Error.
9
+ def self.from_response(response)
10
+ status = response[:status].to_i
11
+ body = response[:body].to_s
12
+ headers = response[:response_headers]
13
+
14
+ if klass = case status
15
+ when 400 then Ivapi::BadRequest
16
+ when 401 then Ivapi::Unauthorized
17
+ when 403 then Ivapi::Forbidden
18
+ end
19
+ klass.new(response)
20
+ end
21
+ end
4
22
  end
5
23
 
6
24
  # Raised when iv.lt returns a 400 HTTP status code
@@ -11,25 +29,4 @@ module Ivapi
11
29
 
12
30
  # Raised when iv.lt returns a 403 HTTP status code
13
31
  class Forbidden < Error; end
14
-
15
- # Raised when iv.lt returns a 404 HTTP status code
16
- class NotFound < Error; end
17
-
18
- # Raised when iv.lt returns a 406 HTTP status code
19
- class NotAcceptable < Error; end
20
-
21
- # Raised when iv.lt returns a 422 HTTP status code
22
- class UnprocessableEntity < Error; end
23
-
24
- # Raised when iv.lt returns a 500 HTTP status code
25
- class InternalServerError < Error; end
26
-
27
- # Raised when iv.lt returns a 501 HTTP status code
28
- class NotImplemented < Error; end
29
-
30
- # Raised when iv.lt returns a 502 HTTP status code
31
- class BadGateway < Error; end
32
-
33
- # Raised when iv.lt returns a 503 HTTP status code
34
- class ServiceUnavailable < Error; end
35
32
  end
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+ require 'ivapi/error'
3
+
4
+ module Ivapi
5
+ # Faraday response middleware
6
+ module Response
7
+
8
+ # This class raises an exception based HTTP status codes returned
9
+ # by the API.
10
+ class RaiseError < Faraday::Response::Middleware
11
+
12
+ private
13
+
14
+ def on_complete(response)
15
+ if error = Ivapi::Error.from_response(response)
16
+ raise error
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,38 @@
1
+ require 'faraday'
2
+
3
+ module Ivapi
4
+ # Faraday response middleware
5
+ module Response
6
+ class RenameKeys < Faraday::Response::Middleware
7
+ UNUSED_KEYS = [
8
+ 'ac_', 'us_', 'bo_', 'or_', 'se_', 'ta_', 'in_'
9
+ ].freeze
10
+
11
+ private
12
+
13
+ def on_complete(response)
14
+ response[:body] = rename_keys(response[:body])
15
+ end
16
+
17
+ def rename_keys(input)
18
+ if input.is_a?(Hash)
19
+ new = {}
20
+ input.map do |key, value|
21
+ if value.is_a?(Hash)
22
+ value = rename_keys(value)
23
+ elsif value.is_a?(Array)
24
+ value = value.map { |v| rename_keys(v) }
25
+ end
26
+
27
+ new[key.gsub(Regexp.union(UNUSED_KEYS), '')] = value
28
+ end
29
+ new
30
+ elsif input.is_a?(Array)
31
+ input.map { |value| rename_keys(value) }
32
+ else
33
+ input
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Ivapi
2
- VERSION = '1.0.4'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -1,47 +1,81 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Ivapi::Client::Account do
4
-
5
- before do
6
- @client = Ivapi::Client.new(username: 'foo', password: 'bar')
4
+ before(:each) do
5
+ Ivapi.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ end
7
9
  end
8
10
 
9
- it 'returns account information' do
10
- stub_command('account_info').to_return(json_response('account_info.json'))
11
+ after(:each) do
12
+ Ivapi.reset!
13
+ end
11
14
 
12
- account_info = @client.account_info
15
+ describe 'account information' do
16
+ before(:each) do
17
+ stub_command('account_info')
18
+ .to_return(json_response('account_info.json'))
19
+ @info = Ivapi.account.information
20
+ end
13
21
 
14
- expect(account_info.ac_name).to eq('Name Surname')
22
+ it 'should return account name' do
23
+ expect(@info.name).to eq('Name Surname')
24
+ end
15
25
  end
16
26
 
17
- it 'returns account orders' do
18
- stub_command('account_orders')
19
- .to_return(json_response('account_orders.json'))
27
+ describe 'account orders' do
28
+ before(:each) do
29
+ stub_command('account_orders')
30
+ .to_return(json_response('account_orders.json'))
31
+ @account_orders = Ivapi.account.orders
32
+ @order = @account_orders.first
33
+ end
20
34
 
21
- account_orders = @client.account_orders
35
+ it 'should return correct orders count' do
36
+ expect(@account_orders.count).to eq(3)
37
+ end
22
38
 
23
- expect(account_orders.count).to eq(3)
24
- expect(account_orders.first.or_cost).to eq('11.11')
39
+ it 'should return order cost' do
40
+ expect(@order.cost).to eq('11.11')
41
+ end
25
42
  end
26
43
 
27
- it 'returns account services' do
28
- stub_command('account_services')
29
- .to_return(json_response('account_services.json'))
44
+ describe 'account services' do
45
+ before(:each) do
46
+ stub_command('account_services')
47
+ .to_return(json_response('account_services.json'))
48
+ @account_services = Ivapi.account.services
49
+ @service = @account_services.first
50
+ end
30
51
 
31
- account_services = @client.account_services
52
+ it 'should return correct services count' do
53
+ expect(@account_services.count).to eq(3)
54
+ end
32
55
 
33
- expect(account_services.count).to eq(3)
34
- expect(account_services.first.se_description)
35
- .to eq('Adreso metinis mokestis (example.com)')
56
+ it 'should return service description' do
57
+ expect(@service.description)
58
+ .to eq('Adreso metinis mokestis (example.com)')
59
+ end
36
60
  end
37
61
 
38
- it 'returns account bonuses' do
39
- stub_command('account_bonuses', { count: 10 })
40
- .to_return(json_response('account_bonuses.json'))
62
+ it 'returns account credits'
63
+
64
+ describe 'account bonuses' do
65
+ before(:each) do
66
+ stub_command('account_bonuses', count: 10)
67
+ .to_return(json_response('account_bonuses.json'))
68
+ @account_bonuses = Ivapi.account.bonuses
69
+ @bonus = @account_bonuses[2]
70
+ end
41
71
 
42
- account_bonuses = @client.account_bonuses
72
+ it 'should return correct bonuses count' do
73
+ expect(@account_bonuses.count).to eq(3)
74
+ end
43
75
 
44
- expect(account_bonuses[2].bo_description)
45
- .to eq('SMS +370.61234569 (example)')
76
+ it 'should return bonus description' do
77
+ expect(@bonus.description)
78
+ .to eq('SMS +370.61234569 (example)')
79
+ end
46
80
  end
47
81
  end
@@ -1,94 +1,167 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Ivapi::Client::Server do
4
+ before(:each) do
5
+ Ivapi.configure do |config|
6
+ config.username = 'foo'
7
+ config.password = 'bar'
8
+ config.server_id = 3
9
+ end
10
+ end
4
11
 
5
- before do
6
- @client = Ivapi::Client.new(username: 'foo', password: 'bar', server_id: 3)
12
+ after(:each) do
13
+ Ivapi.reset!
7
14
  end
8
15
 
9
- it 'should return server information' do
10
- stub_command('server_info', { id: 3 })
11
- .to_return(json_response('server_info.json'))
12
- server_info = @client.server_info
13
- expect(server_info.se_domain).to eq('server.example.com')
14
- expect(server_info.se_info.in_node).to eq('Robinija')
16
+ describe 'server information' do
17
+ before(:each) do
18
+ stub_command('server_info', id: 3)
19
+ .to_return(json_response('server_info.json'))
20
+ @info = Ivapi.server.information
21
+ end
22
+
23
+ it 'should response with alias method' do
24
+ expect(Ivapi.server.info.domain).to eq('server.example.com')
25
+ end
26
+
27
+ it 'should return domain name' do
28
+ expect(@info.domain).to eq('server.example.com')
29
+ end
30
+
31
+ it 'should return node name' do
32
+ expect(@info.info.node).to eq('Robinija')
33
+ end
15
34
  end
16
35
 
17
- it 'should return server tasks' do
18
- stub_command('server_tasks', { id: 3, count: 1 })
19
- .to_return(json_response('server_tasks.json'))
20
- server_tasks = @client.server_tasks(1)
21
- expect(server_tasks.first.ta_params.domain).to eq('server.example.com')
36
+ describe 'server tasks' do
37
+ before(:each) do
38
+ stub_command('server_tasks', id: 3, count: 1)
39
+ .to_return(json_response('server_tasks.json'))
40
+ @tasks = Ivapi.server.tasks(1)
41
+ @task = @tasks.first
42
+ end
43
+
44
+ it 'should return params domain name' do
45
+ expect(@task.params.domain).to eq('server.example.com')
46
+ end
47
+ end
48
+
49
+ describe 'server graphs' do
50
+ before(:each) do
51
+ stub_command('server_graphs', id: 3, width: 1000, ip: '12.23.34.45')
52
+ .to_return(json_response('server_graphs.json'))
53
+ @graphs = Ivapi.server.graphs(1000, '12.23.34.45')
54
+ end
55
+
56
+ it 'should return cpu weekly' do
57
+ expect(@graphs.cpu_weekly).to eq('//stats.serveriai.lt/graph.php?Ds252x+6Lek1o0SV2+u99fqhNaiflbCcb6QuGzlRJ9yy2R1VycHOc6baz3zRB6Am1RJcniVrpCjj+A47DMwkyfQ==')
58
+ end
22
59
  end
23
60
 
24
61
  it 'should return server tasks with specified options' do
25
- stub_command('server_tasks', { id: 3, count: 1, task_id: 1 })
62
+ stub_command('server_tasks', id: 3, count: 1, task_id: 1)
26
63
  .to_return(json_response('server_tasks.json'))
27
- server_tasks = @client.server_tasks(1, { task_id: 1 })
28
- expect(server_tasks.first.ta_params.domain).to eq('server.example.com')
64
+ server_tasks = Ivapi.client.server(3).tasks(1, { task_id: 1 })
65
+ expect(server_tasks.first.params.domain).to eq('server.example.com')
29
66
  end
30
67
 
31
- it 'should return server graphs' do
32
- stub_command('server_graphs', { id: 3, width: 1000, ip: '12.23.34.45' })
33
- .to_return(json_response('server_graphs.json'))
34
- server_graphs = @client.server_graphs(1000, '12.23.34.45')
35
- expect(server_graphs.cpu_weekly).to eq('//stats.serveriai.lt/graph.php?Ds252x+6Lek1o0SV2+u99fqhNaiflbCcb6QuGzlRJ9yy2R1VycHOc6baz3zRB6Am1RJcniVrpCjj+A47DMwkyfQ==')
36
- end
68
+ describe 'server os' do
69
+ before(:each) do
70
+ stub_command('server_os', id: 3)
71
+ .to_return(json_response('server_os.json'))
72
+ @os = Ivapi.server.os['debian-6.0-x86_64']
73
+ end
37
74
 
38
- it 'should return server os' do
39
- stub_command('server_os', { id: 3 })
40
- .to_return(json_response('server_os.json'))
41
- server_os = @client.server_os
42
- expect(server_os['debian-6.0-x86_64'].title).to eq('Debian 6')
75
+ it 'should return cpu weekly' do
76
+ expect(@os.title).to eq('Debian 6')
77
+ end
43
78
  end
44
79
 
45
- it 'should reboot server' do
46
- stub_command('server_reboot', { id: 3 })
47
- .to_return(json_response('server_reboot.json'))
48
- server_reboot = @client.server_reboot
49
- expect(server_reboot.task_id).to eq('11')
50
- end
80
+ describe 'server reboot' do
81
+ before(:each) do
82
+ stub_command('server_reboot', id: 3)
83
+ .to_return(json_response('server_reboot.json'))
84
+ @reboot_response = Ivapi.server.reboot
85
+ end
51
86
 
52
- it 'should recreate server' do
53
- stub_command('server_recreate', { id: 3, os: 'debian-6.0-x86_64' })
54
- .to_return(json_response('server_recreate.json'))
55
- server_recreate = @client.server_recreate('debian-6.0-x86_64')
56
- expect(server_recreate.task_id).to eq('12')
87
+ it 'should return task id' do
88
+ expect(@reboot_response.task_id).to eq('11')
89
+ end
57
90
  end
58
91
 
59
- it 'should reset server password' do
60
- stub_command('server_reset_password', { id: 3 })
61
- .to_return(json_response('server_reset_password.json'))
62
- server_reset_password = @client.server_reset_password
63
- expect(server_reset_password.task_id).to eq('13')
92
+ describe 'server recreate server' do
93
+ before(:each) do
94
+ stub_command('server_recreate', id: 3, os: 'debian-6.0-x86_64')
95
+ .to_return(json_response('server_recreate.json'))
96
+ @recreate_response = Ivapi.server.recreate('debian-6.0-x86_64')
97
+ end
98
+
99
+ it 'should return task id' do
100
+ expect(@recreate_response.task_id).to eq('12')
101
+ end
64
102
  end
65
103
 
66
- it 'should flush server iptables' do
67
- stub_command('server_flush_iptables', { id: 3 })
68
- .to_return(json_response('server_flush_iptables.json'))
69
- server_flush_iptables = @client.server_flush_iptables
70
- expect(server_flush_iptables.task_id).to eq('16')
104
+ describe 'reset server password' do
105
+ before(:each) do
106
+ stub_command('server_reset_password', id: 3)
107
+ .to_return(json_response('server_reset_password.json'))
108
+ @reset_pwd_response = Ivapi.server.reset_password
109
+ end
110
+
111
+ it 'should return task id' do
112
+ expect(@reset_pwd_response.task_id).to eq('13')
113
+ end
71
114
  end
72
115
 
73
- it 'should change server firewall settings' do
74
- stub_command('server_firewall', { id: 3 })
75
- .to_return(json_response('server_firewall.json'))
76
- server_firewall = @client.server_firewall
77
- expect(server_firewall.pps).to eq('1000')
116
+ describe 'flush server iptables' do
117
+ before(:each) do
118
+ stub_command('server_flush_iptables', id: 3)
119
+ .to_return(json_response('server_flush_iptables.json'))
120
+ @flush_response = Ivapi.server.flush_iptables
121
+ end
122
+
123
+ it 'should return task id' do
124
+ expect(@flush_response.task_id).to eq('16')
125
+ end
78
126
  end
79
127
 
80
- it 'should change server plan' do
81
- stub_command('server_change', { id: 3 })
82
- .to_return(json_response('server_change.json'))
83
- server_change = @client.server_change
84
- expect(server_change.task_id).to eq('14')
128
+ describe 'change server firewall settings' do
129
+ before(:each) do
130
+ stub_command('server_firewall', id: 3)
131
+ .to_return(json_response('server_firewall.json'))
132
+ @firewall = Ivapi.server.firewall
133
+ end
134
+
135
+ it 'should return correct pps' do
136
+ expect(@firewall.pps).to eq('1000')
137
+ end
85
138
  end
86
139
 
87
- it 'should change server hostname' do
88
- stub_command('server_domain', { id: 3, domain: 'example.com' })
89
- .to_return(json_response('server_domain.json'))
90
- server_domain = @client.server_domain('example.com')
91
- expect(server_domain.task_id).to eq('15')
140
+ describe 'change server plan' do
141
+ before(:each) do
142
+ stub_command('server_change', id: 3)
143
+ .to_return(json_response('server_change.json'))
144
+ @plan_response = Ivapi.server.change
145
+ end
146
+
147
+ it 'should return correct pps' do
148
+ expect(@plan_response.task_id).to eq('14')
149
+ end
92
150
  end
93
151
 
152
+ describe 'change server hostname' do
153
+ before(:each) do
154
+ stub_command('server_domain', id: 3, domain: 'example.com')
155
+ .to_return(json_response('server_domain.json'))
156
+ @hostname_response = Ivapi.server.domain('example.com')
157
+ end
158
+
159
+ it 'should return task id' do
160
+ expect(@hostname_response.task_id).to eq('15')
161
+ end
162
+
163
+ it 'should response with alias method' do
164
+ expect(Ivapi.server.hostname('example.com').task_id).to eq('15')
165
+ end
166
+ end
94
167
  end