Class: Rimu::RimuAPI
- Inherits:
-
Object
show all
- Defined in:
- lib/rimu.rb
Defined Under Namespace
Classes: Orders, RimuArgumentError, RimuRequestError, RimuResponseError, Servers
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ RimuAPI
Returns a new instance of RimuAPI.
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/rimu.rb', line 9
def initialize(args)
@api_url = args[:api_url] if args[:api_url]
@logger = args[:logger]
if args.include?(:api_key)
@api_key = args[:api_key]
else
raise ArgumentError, "The :api_key is required."
end
@read_timeout = args[:read_timeout] if args[:read_timeout]
raise ArgumentError, "The :read_timeout must be an Integer." if ! @read_timeout.nil? && ! @read_timeout.is_a?(Integer)
end
|
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
7
8
9
|
# File 'lib/rimu.rb', line 7
def logger
@logger
end
|
Class Method Details
.has_namespace(*namespaces) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/rimu.rb', line 131
def self.has_namespace(*namespaces)
namespaces.each do |namespace|
define_method(namespace.to_sym) do ||
lookup = instance_variable_get("@#{namespace}")
return lookup if lookup
subclass = self.class.const_get(namespace.to_s.capitalize).new(
:api_key => api_key,
:api_url => api_url,
:logger => logger,
:read_timeout => read_timeout,
)
instance_variable_set("@#{namespace}", subclass)
subclass
end
end
end
|
Instance Method Details
#api_key ⇒ Object
25
26
27
|
# File 'lib/rimu.rb', line 25
def api_key
@api_key
end
|
#api_url ⇒ Object
21
22
23
|
# File 'lib/rimu.rb', line 21
def api_url
@api_url || "https://api.rimuhosting.com"
end
|
#billing_methods ⇒ Object
114
115
116
|
# File 'lib/rimu.rb', line 114
def billing_methods
send_request("/r/billing-methods", "billing_methods")
end
|
#convert_item(response) ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/rimu.rb', line 83
def convert_item(response)
if response.respond_to?(:keys)
response.keys.each do |key|
response[key.downcase] = response[key]
response.delete(key) if key != key.downcase
end
end
OpenStruct.new(response)
end
|
#distributions ⇒ Object
110
111
112
|
# File 'lib/rimu.rb', line 110
def distributions
send_request("/r/distributions", "distro_infos")
end
|
#error?(response) ⇒ Boolean
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/rimu.rb', line 42
def error?(response)
if response.nil?
return true
else
if response.is_a?(Hash) && ! response.empty?
! response.empty? and response[response.keys[0]] and \
response[response.keys[0]].has_key?("response_type") and \
response[response.keys[0]]["response_type"] == "ERROR"
else
return true
end
end
end
|
#error_message(response) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/rimu.rb', line 56
def error_message(response)
if response.nil? || ! response.is_a?(Hash) || (response.is_a?(Hash) && response.empty?)
if response.empty?
" - Error: Response was empty"
else
" - Error: Unknown error occured"
end
else
if response[response.keys[0]].has_key?("human_readable_message")
error = response[response.keys[0]]["human_readable_message"]
" - Error: #{error}"
else
" - Error: Unknown error occured"
end
end
end
|
73
74
75
76
77
78
79
80
81
|
# File 'lib/rimu.rb', line 73
def format_response(response, field)
result = response[response.keys[0]]
if result.is_a?(Hash) and result.has_key?(field)
result = result[field]
end
return result.collect {|item| convert_item(item) } if result.class == Array
return result unless result.respond_to?(:keys)
convert_item(result)
end
|
#prep_data(default_params, params) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/rimu.rb', line 118
def prep_data(default_params, params)
params.keep_if {|k,_| default_params.keys.include? k }
new_params = default_params.merge(params)
new_params.each_pair do |key, val|
if val.is_a?(Hash)
val.keep_if {|_,v| v != nil }
val.keep_if {|k,_| default_params[key].keys.include? k }
end
end
new_params.keep_if {|_,v| (v.is_a?(Hash) && ! v.empty?) || (! v.is_a?(Hash) && v != nil) }
return new_params
end
|
#read_timeout ⇒ Object
29
30
31
|
# File 'lib/rimu.rb', line 29
def read_timeout
@read_timeout || 3600
end
|
#send_request(path, field, method = "GET", data = nil) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/rimu.rb', line 93
def send_request(path, field, method="GET", data=nil)
logger.info "#{method} #{api_url}#{path} body:#{data.inspect}" if logger
if data.nil?
options = {:base_uri => api_url, headers: }
else
options = {:base_uri => api_url, headers: , body: data.to_json, read_timeout: read_timeout}
end
begin
response = HTTParty.send(method.downcase.to_sym, path, options).parsed_response
rescue StandardError => e
raise RimuRequestError, "API Request ERROR: #{e}"
end
raise RimuResponseError, "API Response ERROR: #{error_message(response)}" if error?(response)
logger.info "Response: => #{response}" if logger
format_response(response, field)
end
|
33
34
35
36
37
38
39
40
|
# File 'lib/rimu.rb', line 33
def
{
'Content-Type' =>'application/json',
'Accept' =>'application/json',
'User-Agent' => 'RimuAPI-Ruby',
'Authorization' => "rimuhosting apikey=#{api_key}",
}
end
|