Class: SuggestGrid::BaseController
- Inherits:
-
Object
- Object
- SuggestGrid::BaseController
- Defined in:
- lib/suggest_grid/controllers/base_controller.rb
Direct Known Subclasses
ActionController, MetadataController, RecommendationController, SimilarityController, TypeController
Constant Summary collapse
- @@http_client =
FaradayClient.new
- @@global_headers =
{ 'user-agent' => 'SUGGESTGRID' }
Instance Attribute Summary collapse
-
#http_call_back ⇒ Object
Returns the value of attribute http_call_back.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
Instance Method Summary collapse
- #execute_request(request, binary: false) ⇒ Object
-
#initialize(http_client: nil, http_call_back: nil) ⇒ BaseController
constructor
A new instance of BaseController.
- #validate_parameters(args) ⇒ Object
- #validate_response(context) ⇒ Object
Constructor Details
#initialize(http_client: nil, http_call_back: nil) ⇒ BaseController
Returns a new instance of BaseController.
13 14 15 16 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 13 def initialize(http_client: nil, http_call_back: nil) @http_client = http_client ||= @@http_client @http_call_back = http_call_back end |
Instance Attribute Details
#http_call_back ⇒ Object
Returns the value of attribute http_call_back.
5 6 7 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 5 def http_call_back @http_call_back end |
#http_client ⇒ Object
Returns the value of attribute http_client.
5 6 7 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 5 def http_client @http_client end |
Instance Method Details
#execute_request(request, binary: false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 26 def execute_request(request, binary: false) @http_call_back.on_before_request(request) if @http_call_back APIHelper.clean_hash(request.headers) request.headers = @@global_headers.clone.merge(request.headers) response = binary ? @http_client.execute_as_binary(request) : @http_client.execute_as_string(request) context = HttpContext.new(request, response) @http_call_back.on_after_response(context) if @http_call_back return context end |
#validate_parameters(args) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 18 def validate_parameters(args) args.each do |name, value| if value == nil raise ArgumentError.new 'Required parameter #{name} cannot be nil.' end end end |
#validate_response(context) ⇒ Object
40 41 42 43 44 |
# File 'lib/suggest_grid/controllers/base_controller.rb', line 40 def validate_response(context) if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK raise APIException.new 'HTTP Response Not OK', context end end |