Module: ExternalApiService

Defined in:
lib/external_api_service.rb,
lib/external_api_service/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.get_service(url, queries = {}, credentials = {}) ⇒ Object

Make a GET request to external endpoint You must format it as a hash with string key/value => “password”

ExternalApiService.get_service(“sample_endpoint”, “sample_request”)

Parameters:

  • url

    is required

  • query

    is optional. Query must be formatted as Ruby hash.

  • auth

    is optional. If you choose to include it,

Returns:

  • Will transform the JSON to Ruby Hash with symbolized names



17
18
19
20
21
# File 'lib/external_api_service.rb', line 17

def self.get_service(url, queries = {}, credentials = {})
  credentials_formatted_for_auth = credentials.to_a.flatten
  uri = URI_Builder.build_uri(url, queries)
  HTTP_Client.new.get(uri, credentials_formatted_for_auth)
end

.post_service(url, data_to_post, credentials, http_header_params = {}) ⇒ Object

Note:

You have to have the trailing ‘/’ for the path for Ruby to POST properly e.g. sample.com/

Make a JSON POST request to external endpoint You must format it as a hash of strings (both key and value) e.g. => “password” You get several free headers with the Net/HTTP class, but it is there if you want it. Must be a hash

Examples:

Add a subscriber to your Mailchimp Account (Api V3)

auth = {'api_key': '123key'}
optional_header = {}
data_to_post: {'email' => '[email protected]', 'status' => 'subscribed'}
endpoint = 'https://us9.api.mailchimp.com/3.0/lists/123uniquelistID/members'
ExternalApiService.post_service(endpoint, data_to_post, auth, optional_header)

Parameters:

  • url

    is required

  • data_to_post

    is required, since you are a making POST request after all.

  • auth

    is required, as it is not an idempotent action.

  • header_params

    are optional. This sets content-type to “application/json”, so do not include Content-Type.

Returns:

  • Will transform the JSON to Ruby Hash with symbolized names



43
44
45
46
47
48
# File 'lib/external_api_service.rb', line 43

def self.post_service(url, data_to_post, credentials, http_header_params = {})
  credentials_formatted_for_auth = credentials.to_a.flatten
  uri = URI_Builder.build_uri(url)
  http_params = { uri: uri, data: data_to_post, credentials: credentials_formatted_for_auth, header_params: http_header_params }
  HTTP_Client.new.post(http_params)
end