Sha256: 00cfa9614962db3363c4e75a390bd5729ea5481b5d4139547ebfd87879363c9b

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'json'
require 'curb'

class Render::Component::Client

  def obtain_component(component, attributes)
    execute_request(component, attributes)
  end

  private

  def execute_request(component, attributes)

    url = "#{default_endpoint}/#{component}"

    response = Curl.get(url) do |curl|
      curl.headers['X-Content'] = apply_default_attributes(attributes)
      curl.connect_timeout = 3
    end

    code = response.status.to_i

    return response.body_str if code == 200

    raise StandardError.new, "Request Error. URL: #{url}, Response Status Code: #{code}"
  end

  def apply_default_attributes(attributes)
    body = JSON.parse(attributes)
    body['base_path'] = default_base_path
    JSON.generate(body)
  end

  def default_base_path
    base_path = Render::Component.configuration.base_path
    raise Render::Component::Error.new, configuration_error_message(__method__, 'base_path') if base_path.nil?
    base_path
  end

  def default_endpoint
    endpoint = Render::Component.configuration.endpoint
    raise Render::Component::Error.new, configuration_error_message(__method__, 'endpoint') if endpoint.nil?
    endpoint
  end

  def configuration_error_message(method, attribute)
    "To use `#{method}` you must add default #{attribute}. e.g. Render::Component.configuration.#{attribute}="
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
render-component-0.3.0 lib/render/component/client.rb