Sha256: 95a19d2aec53cc9836ffbe389840f4deb56ba21f66f46485ea6f1378efcd2029

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'httparty'

module Maestrano
  module Connec
    
    class Client
      include ::HTTParty
      headers 'Accept' => 'application/vnd.api+json'
      headers 'Content-Type' => 'application/vnd.api+json'
      format :json
      
      attr_reader :group_id
      
      def initialize(group_id)
        @group_id = group_id
        self.class.base_uri("#{Maestrano.param('connec.host')}#{Maestrano.param('connec.base_path')}")
      end
      
      # Return the default options which includes
      # maestrano authentication
      def default_options
        {
          basic_auth: { 
            username: Maestrano.param('api.id'), 
            password: Maestrano.param('api.key')
          } 
        }
      end
      
      # Return the right path scoped using the customer
      # group id
      def scoped_path(relative_path)
        clean_path = relative_path.gsub(/^\/+/, "").gsub(/\/+$/, "")
        "/#{@group_id}/#{clean_path}"
      end
      
      # E.g: client.get('/organizations')
      # E.g: client.get('/organizations/123')
      def get(relative_path, options = {})
        self.class.get(self.scoped_path(relative_path),default_options.merge(options))
      end
      
      # E.g: client.post('/organizations', { organizations: { name: 'DoeCorp Inc.' } })
      def post(relative_path, body, options = {})
        self.class.post(self.scoped_path(relative_path),
          default_options.merge(body: body.to_json).merge(options)
        )
      end
      
      # E.g for collection: 
      # => client.put('/organizations/123', { organizations: { name: 'DoeCorp Inc.' } })
      # E.g for singular resource: 
      # => client.put('/company', { company: { name: 'DoeCorp Inc.' } })
      def put(relative_path, body, options = {})
        self.class.put(self.scoped_path(relative_path),
          default_options.merge(body: body.to_json).merge(options)
        )
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
maestrano-0.11.0 lib/maestrano/connec/client.rb
maestrano-0.10.0 lib/maestrano/connec/client.rb