Class: Beget::API
- Inherits:
-
Object
- Object
- Beget::API
- Defined in:
- lib/beget_api.rb
Overview
Beget API wrapper
Constant Summary collapse
- API_URI =
'https://api.beget.com/api'
Instance Method Summary collapse
-
#initialize(login, password) ⇒ API
constructor
A new instance of API.
- #method_missing(method) ⇒ Object
- #request(section, method, **data) ⇒ Object
- #respond_to_missing? ⇒ Boolean
Constructor Details
#initialize(login, password) ⇒ API
Returns a new instance of API.
12 13 14 15 |
# File 'lib/beget_api.rb', line 12 def initialize(login, password) @login = login @password = password end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Method Details
#request(section, method, **data) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/beget_api.rb', line 25 def request(section, method, **data) query = { login: @login, passwd: @password, output_format: 'json' } unless data.empty? query.merge!( { input_format: 'json', input_data: JSON.generate(data) } ) end uri = "#{API_URI}/#{section}/#{method}?#{URI.encode_www_form(query)}" response = Net::HTTP.get_response(URI(uri)) raise HTTPError, response.code unless response.is_a?(Net::HTTPSuccess) response_data = JSON.parse(response.body) if response_data['status'] == 'error' raise RequestError.new( response_data['error_code'], response_data['error_text'] ) end raise AnswerError, response_data['answer']['errors'] if response_data['answer']['status'] == 'error' response_data['answer']['result'] end |
#respond_to_missing? ⇒ Boolean
21 22 23 |
# File 'lib/beget_api.rb', line 21 def respond_to_missing? true end |