Module: Rest

Included in:
NeoWs
Defined in:
lib/nasa/utils/rest.rb

Overview

CRUD Rest actions to be plug and play into all API Services

Instance Method Summary collapse

Instance Method Details

#delete(url, payload: {}, params: {}) ⇒ NASA::RestResponse, NASA::ExceptionResponse

Parameters:

  • url (String)

    the url to send a request to

  • payload (Hash)

    the JSON payload to send

  • params (Hash)

    the URL params to send

Returns:

  • (NASA::RestResponse)

    if the API call was sucessful

  • (NASA::ExceptionResponse)

    if the API call failed



20
21
22
23
24
# File 'lib/nasa/utils/rest.rb', line 20

%w[get post put delete].each do |action|
  define_method(:"#{action}") do |url, payload: {}, params: {}|
    request(:"#{action}", url, payload, params)
  end
end

#get(url, payload: {}, params: {}) ⇒ NASA::RestResponse, NASA::ExceptionResponse

Parameters:

  • url (String)

    the url to send a request to

  • payload (Hash)

    the JSON payload to send

  • params (Hash)

    the URL params to send

Returns:

  • (NASA::RestResponse)

    if the API call was sucessful

  • (NASA::ExceptionResponse)

    if the API call failed



20
21
22
23
24
# File 'lib/nasa/utils/rest.rb', line 20

%w[get post put delete].each do |action|
  define_method(:"#{action}") do |url, payload: {}, params: {}|
    request(:"#{action}", url, payload, params)
  end
end

#post(url, payload: {}, params: {}) ⇒ NASA::RestResponse, NASA::ExceptionResponse

Parameters:

  • url (String)

    the url to send a request to

  • payload (Hash)

    the JSON payload to send

  • params (Hash)

    the URL params to send

Returns:

  • (NASA::RestResponse)

    if the API call was sucessful

  • (NASA::ExceptionResponse)

    if the API call failed



20
21
22
23
24
# File 'lib/nasa/utils/rest.rb', line 20

%w[get post put delete].each do |action|
  define_method(:"#{action}") do |url, payload: {}, params: {}|
    request(:"#{action}", url, payload, params)
  end
end

#put(url, payload: {}, params: {}) ⇒ NASA::RestResponse, NASA::ExceptionResponse

Parameters:

  • url (String)

    the url to send a request to

  • payload (Hash)

    the JSON payload to send

  • params (Hash)

    the URL params to send

Returns:

  • (NASA::RestResponse)

    if the API call was sucessful

  • (NASA::ExceptionResponse)

    if the API call failed



20
21
22
23
24
# File 'lib/nasa/utils/rest.rb', line 20

%w[get post put delete].each do |action|
  define_method(:"#{action}") do |url, payload: {}, params: {}|
    request(:"#{action}", url, payload, params)
  end
end

#request(action, url, payload, params) ⇒ NASA::RestResponse, NASA::ExceptionResponse

Performs a CRUD action to the requested URL

Parameters:

  • url (String)

    the url to send a request to

  • payload (Hash)

    the JSON payload to send

  • params (Hash)

    the URL params to send

Returns:

  • (NASA::RestResponse)

    if the API call was sucessful

  • (NASA::ExceptionResponse)

    if the API call failed



28
29
30
31
32
33
34
35
# File 'lib/nasa/utils/rest.rb', line 28

def request(action, url, payload, params)
  options = rest_options(action, url, payload, params)
  response = RestClient::Request.execute(options)

  Responses::RestResponse.new(response)
rescue RestClient::ExceptionWithResponse => ex
  Responses::ExceptionResponse.new(ex)
end