Sha256: 1f750a02b59427d81c8154404e2e2156d7ca00f50bb2a54dd8b1e85b5575c370

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

require "rest-client"

module SauceWhisk
  module RestRequestBuilder

    def get(resource_to_fetch=nil)
      resource_url = fully_qualified_resource
      resource_url << "/#{resource_to_fetch}" if resource_to_fetch
      RestClient::Request.execute({:method => :get, :url => resource_url}.merge auth_details)
    end

    def put(resource_id, body={})
      url = "#{fully_qualified_resource}/#{resource_id}"
      length = body.length
      headers = {"Content-Length" => length}
      req_params = {
          :method => :put,
          :url => url,
          :payload => body,
          :content_type => "application/json",
          :headers => headers
      }
      RestClient::Request.execute(req_params.merge auth_details)
    end

    def delete(resource_id)
      resource_to_delete = fully_qualified_resource << "/#{resource_id}"
      RestClient::Request.execute({:method => :delete, :url => resource_to_delete}.merge auth_details)
    end

    def post(opts)
      payload = (opts[:payload].to_json)
      resource_id = opts[:resource] || nil

      url = fully_qualified_resource
      url << "/#{resource_id}" if resource_id

      length = payload.length
      headers = {"Content-Length" => length}
      req_params = {
          :method => :post,
          :url => url,
          :content_type => "application/json",
          :headers => headers
      }

      req_params.merge!({:payload => payload}) unless payload.nil?

      RestClient::Request.execute(req_params.merge auth_details)

    end

    def auth_details
      {:user => SauceWhisk.username, :password => SauceWhisk.password}
    end

    def fully_qualified_resource
      return (respond_to? :resource) ? "#{SauceWhisk.base_url}/#{resource}" : SauceWhisk.base_url
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sauce_whisk-0.0.13 lib/sauce_whisk/rest_request_builder.rb
sauce_whisk-0.0.12 lib/sauce_whisk/rest_request_builder.rb
sauce_whisk-0.0.11 lib/sauce_whisk/rest_request_builder.rb
sauce_whisk-0.0.10 lib/sauce_whisk/rest_request_builder.rb