Class: EasyTranslate::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, http_options = {}) ⇒ Request

Returns a new instance of Request.



10
11
12
13
# File 'lib/easy_translate/request.rb', line 10

def initialize(options = {}, http_options = {})
  @options = options
  @http_options = http_options
end

Instance Attribute Details

#http_optionsObject

Returns the value of attribute http_options.



8
9
10
# File 'lib/easy_translate/request.rb', line 8

def http_options
  @http_options
end

Instance Method Details

#bodyString

Body, blank by default

Returns:

  • (String)

    The body for this request



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

def body
  ''
end

#paramsHash

The base params for a request

Returns:

  • (Hash)

    a hash of the base parameters for any request



29
30
31
32
33
34
# File 'lib/easy_translate/request.rb', line 29

def params
  params = {}
  params[:key] = EasyTranslate.api_key if EasyTranslate.api_key
  params[:prettyPrint] = 'false' # eliminate unnecessary overhead
  params
end

#pathString

The path for the request

Returns:

  • (String)

    The path for this request

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/easy_translate/request.rb', line 23

def path
  raise NotImplementedError.new('path is not implemented')
end

#perform_rawString

Perform the given request

Returns:

  • (String)

    The response String



38
39
40
41
42
43
44
# File 'lib/easy_translate/request.rb', line 38

def perform_raw
  request = Net::HTTP::Post.new(uri.request_uri, { 'Content-Type' => 'application/json' })
  request.body = body
  response = http.request(request)
  raise_exception(response) unless response.code == '200'
  response.body
end