class Orchestrate::API::Wrapper

Ruby wrapper for the Orchestrate.io REST API.

The primary entry point is the send_request method, which generates a Request, and returns the Response to the caller. Usage examples for each HTTP request are documented in the Procedural interface module.

Attributes

verbose[RW]

Public Class Methods

new(config_file) click to toggle source

Loads settings from the configuration file and returns the client handle to be used for subsequent requests.

Example config file: "./lib/orch_config.json"

 {
   "base_url":"https://api.orchestrate.io/v0",
   "user":"<user-key-from-orchestrate.io>",
   "verbose":"false"
 }
# File api/lib/orchestrate_api/wrapper.rb, line 29
def initialize(config_file)
  orch_config = ::ActiveSupport::JSON.decode open(config_file).read
  @base_url = orch_config['base_url']
  @user     = orch_config['user']
  @verbose  = orch_config['verbose'] && orch_config['verbose'] == 'true' ? true
                                                                         : false
end

Public Instance Methods

send_request(method, args) click to toggle source

Creates the Request object and sends it via the perform method, which generates and returns the Response object.

# File api/lib/orchestrate_api/wrapper.rb, line 40
def send_request(method, args)
  request = Orchestrate::API::Request.new(method, build_url(method, args), @user) do |r|
    r.data = args[:json] if args[:json]
    r.ref = args[:ref] if args[:ref]
    r.verbose = verbose
  end
  request.perform
end

Private Instance Methods

build_url(method, args) click to toggle source

Builds the URL for each HTTP request to the orchestrate.io api.

# File api/lib/orchestrate_api/wrapper.rb, line 53
def build_url(method, args)
  uri = "#{@base_url}/#{args[:collection]}"
  if args[:key]
    uri << "/#{args[:key]}"
    if args[:ref]
      uri << "/refs/#{args[:ref].gsub(/"/, '')}" if method == :get
    elsif args[:event_type]
      uri << "/events/#{args[:event_type]}"
      unless args[:timestamp].blank?
        if method == :get && args[:timestamp][:start]
          uri << "?start=#{args[:timestamp][:start]}&end=#{args[:timestamp][:end]}"
        elsif method == :put
          uri << "?timestamp=#{args[:timestamp]}"
        end
      end
    elsif args[:kind]
      if method == :get
        uri << "/relations/#{args[:kind]}"
      else
        uri << "/relation/#{args[:kind]}/#{args[:to_collection]}/#{args[:to_key]}"
      end
    end
  end
  uri << args[:path] if args[:path]
  uri
end