Class: Yarr::Command::URLEvaluate

Inherits:
Base
  • Object
show all
Extended by:
Concern::ASTDigger
Defined in:
lib/yarr/command/url_evaluate.rb

Overview

evaluates the content fetched from a url on an online evaluator service like carc.in

Instance Attribute Summary

Attributes inherited from Base

#ast

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern::ASTDigger

digger

Constructor Details

#initialize(ast, fetch_service = Typhoeus, evaluator_service = EvaluatorService.new) ⇒ URLEvaluate

Returns a new instance of URLEvaluate.

Parameters:

  • ast (Yarr::AST)

    user input

  • fetch_service (#get) (defaults to: Typhoeus)

    A web service that can get

  • evaluator_service (Yarr::EvaluatorService) (defaults to: EvaluatorService.new)

    the evaluator


19
20
21
22
23
24
25
26
# File 'lib/yarr/command/url_evaluate.rb', line 19

def initialize(ast,
               fetch_service = Typhoeus,
               evaluator_service = EvaluatorService.new)
  super(ast)

  @fetch_service = fetch_service
  @evaluator_service = evaluator_service
end

Class Method Details

.match?(ast) ⇒ Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/yarr/command/url_evaluate.rb', line 12

def self.match?(ast)
  ast.key? :url_evaluate
end

Instance Method Details

#handleObject


28
29
30
31
32
33
34
35
36
37
# File 'lib/yarr/command/url_evaluate.rb', line 28

def handle
  user_content = @fetch_service.get(url)
  response_code = user_content.response_code
  unless response_code == 200
    return "Request returned response code #{response_code}"
  end

  evaluator_request = EvaluatorService::Request.new(user_content.body)
  @evaluator_service.request(evaluator_request).output
end