Class: Plagscan::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/plagscan/request.rb

Overview

PlagScan HTTP request service

Constant Summary collapse

DEFAULT_REQUEST_OPTIONS =
{
  method: :get,
  body: nil,
  headers: nil,
  ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER,
  ssl_ca_file: nil
}.freeze

Class Method Summary collapse

Class Method Details

.api_url(path = '') ⇒ Object



17
18
19
# File 'lib/plagscan/request.rb', line 17

def api_url(path = '')
  Plagscan.api_base + path
end

.json_request(path, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plagscan/request.rb', line 32

def json_request(path, options = {})
  response = Plagscan::Request.request(path, options)

  unless response.is_a?(options[:expected_result] || Net::HTTPSuccess)
    raise Plagscan::HTTPError, "Invalid http response: #{response.code} - #{response.body}"
  end

  JSON.parse response.body, symbolize_names: true
rescue JSON::ParserError
  raise Plagscan::JsonParseError, "PlagScan response parse error: #{response.body}"
end

.request(path, options = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/plagscan/request.rb', line 25

def request(path, options = {})
  options = DEFAULT_REQUEST_OPTIONS.merge(options)
  http = create_http(options)
  req = create_request(path, options)
  http.start { http.request(req) }
end

.user_agentObject



21
22
23
# File 'lib/plagscan/request.rb', line 21

def user_agent
  "PlagScan-Ruby/#{Plagscan::VERSION}"
end