class Megam::Assembla

Constants

API_VERSION1
HEADERS
OPTIONS
VERSION

Attributes

text[RW]

text is used to print stuff in the terminal (message, log, info, warn etc.)

Public Class Methods

new() click to toggle source

It is assumed that every API call will use an API_KEY/email. This ensures validity of the person really the same guy on who he claims. 3 levels of options exits

  1. The global OPTIONS as available inside the API (OPTIONS)

  2. The options as passed via the instantiation of API will override global options. The ones that are passed are :email and :api_key and will

be merged into a class variable @options

  1. Upon merge of the options, the api_key, email as available in the @options is deleted.

# File lib/megam/assembla.rb, line 62
def initialize
  @options = OPTIONS
end

Public Instance Methods

get_spaces(access_token) click to toggle source

GET /repos

# File lib/megam/assembla/spaces.rb, line 5
def get_spaces(access_token)
  @options = {:path => "/spaces?grant_type=access_token&access_token=#{access_token}",:body => ""}.merge(@options)
  request(
    :expects  => 200,
    :method   => :get,
    :body     => @options[:body]
  )
end
last_response() click to toggle source
# File lib/megam/assembla.rb, line 51
def last_response
  @last_response
end
request(params,&block) click to toggle source
# File lib/megam/assembla.rb, line 66
def request(params,&block)     
  just_color_debug("#{@options[:path]}")
  start = Time.now
  Megam::Log.debug("START")
  params.each do |pkey, pvalue|
    Megam::Log.debug("> #{pkey}: #{pvalue}")
  end

  begin
    response = connection.request(params, &block)        
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status

    when 401 then Megam::API::Errors::Unauthorized
    when 403 then Megam::API::Errors::Forbidden
    when 404 then Megam::API::Errors::NotFound
    when 408 then Megam::API::Errors::Timeout
    when 422 then Megam::API::Errors::RequestFailed
    when 423 then Megam::API::Errors::Locked
    when /50./ then Megam::API::Errors::RequestFailed
    else Megam::API::Errors::ErrorWithResponse
    end
    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    Megam::Log.debug("#{reerror.response.body}")
    reerror.response.body = Megam::JSONCompat.from_json(reerror.response.body.chomp)
    Megam::Log.debug("RESPONSE ERR: Ruby Object")
    Megam::Log.debug("#{reerror.response.body}")      
    raise(reerror)
  end

  @last_response = response
  Megam::Log.debug("RESPONSE: HTTP Status and Header Data")
  Megam::Log.debug("> HTTP #{response.remote_ip} #{response.status}")

  response.headers.each do |header, value|
    Megam::Log.debug("> #{header}: #{value}")
  end
  Megam::Log.debug("End HTTP Status/Header Data.")

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    Megam::Log.debug("RESPONSE: HTTP Body(JSON)")
    Megam::Log.debug("#{response.body}")

    begin
      response.body = Megam::JSONCompat.from_json(response.body.chomp)
      Megam::Log.debug("RESPONSE: Ruby Object")
      Megam::Log.debug("#{response.body}")
    rescue Exception => jsonerr
      Megam::Log.error(jsonerr)
      raise(jsonerr)
    end
  end
  Megam::Log.debug("END(#{(Time.now - start).to_s}s)")
  # reset (non-persistent) connection
  @connection.reset
  response
end

Private Instance Methods

connection() click to toggle source

Make a lazy connection.

# File lib/megam/assembla.rb, line 135
def connection
  @options[:path] =API_VERSION1+ @options[:path]
  @options[:headers] = HEADERS.merge(@options[:headers])

  Megam::Log.debug("HTTP Request Data:")
  Megam::Log.debug("> HTTP #{@options[:scheme]}://#{@options[:host]}")
  @options.each do |key, value|
    Megam::Log.debug("> #{key}: #{value}")
  end
  Megam::Log.debug("End HTTP Request Data.")
  @connection = Excon.new("#{@options[:scheme]}://#{@options[:host]}",@options)
end
just_color_debug(path) click to toggle source
# File lib/megam/assembla.rb, line 130
def just_color_debug(path)
  text.msg "--> #{text.color("(#{path})", :cyan,:bold)}"
end