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
45
46
47
48
49
50
# File 'lib/easy_translate/request.rb', line 38

def perform_raw
  # Construct the request
  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field('X-HTTP-Method-Override', 'GET')
  request.body = body
  # Fire and return
  response = http.request(request)
  unless response.code == '200'
    err = JSON.parse(response.body)['error']['errors'].first['message']
    raise EasyTranslateException.new(err)
  end
  response.body
end