class Megam::Scmmanager

Constants

API_REST
AUTH_PATH
HEADERS
OPTIONS
VERSION

Public Class Methods

new(options={}) click to toggle source

It is assumed that every API call will NOT use an API_KEY/email.

# File lib/megam/scmmanager.rb, line 51
def initialize(options={})
  @options = OPTIONS.merge(options)
end

Public Instance Methods

get_accounts(email) click to toggle source
GET /accounts

Yet to be tested

# File lib/megam/scmmanager/accounts.rb, line 5
def get_accounts(email)

  @options = {:path => "/users.json",
    :body => ''}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end
get_repos(username=nil, password=nil) click to toggle source
GET /accounts

Yet to be tested

# File lib/megam/scmmanager/repos.rb, line 5
def get_repos(username=nil, password=nil)

  @options = {:path => "/repositories",
    :body => ''}.merge(@options)

  request(
    :expects  => 200,
    :method   => :get,
    :username => username,
    :password => password,
    :body     => @options[:body]
  )
end
last_response() click to toggle source
# File lib/megam/scmmanager.rb, line 46
def last_response
  @last_response
end
post_accounts(json_hash) click to toggle source

The body content needs to be a json.

# File lib/megam/scmmanager/accounts.rb, line 18
def post_accounts(json_hash)
  @options = {:path => '/users.json',
  :body => json_hash[:json]}.merge(@options)

  request(
    :expects  => 201,
    :method   => :post,
    :body     => @options[:body]
    )
end
request(params, &block) click to toggle source
# File lib/megam/scmmanager.rb, line 55
def request(params, &block)
  start = Time.now
  puts params
  text.msg "#{text.color("START", :cyan, :bold)}"
  username =  params[:username] || ENV['MEGAM_SCMADMIN_USERNAME']
  password =  params[:password]  || ENV['MEGAM_SCMADMIN_PASSWORD']
  raise ArgumentError, "You must specify [:username, :password]" if username.nil? || password.nil?
  text.msg "#{text.color("Got username[#{username}] [*******]", :red, :bold)}"

  begin
    httpcon = connection
    httpcon.use_ssl = false
    httpcon.start do |http|
      request = Net::HTTP::Get.new(@options[:path])
      request.basic_auth username, password
      @response = http.request(request)
    end
  end
  @response
end
text() click to toggle source
# File lib/megam/scmmanager.rb, line 42
def text
  @text ||= Megam::Dumpout.new(STDOUT, STDERR, STDIN)
end

Private Instance Methods

connection() click to toggle source

Make a lazy connection.

# File lib/megam/scmmanager.rb, line 78
def connection
  @options[:path] =API_REST + @options[:path]
  @options[:headers] = HEADERS.merge({
    'X-Megam-Date' =>  Time.now.strftime("%Y-%m-%d %H:%M")
  }).merge(@options[:headers])

  text.info("HTTP Request Data:")
  text.msg("> HTTP #{@options[:scheme]}://#{@options[:host]}")
  @options.each do |key, value|
    text.msg("> #{key}: #{value}")
  end
  text.info("End HTTP Request Data.")
  http = Net::HTTP.new(@options[:host], @options[:port])
  http
end