Class: Megam::Assembla

Inherits:
Object
  • Object
show all
Defined in:
lib/megam/assembla.rb,
lib/megam/assembla/spaces.rb,
lib/megam/assembla/version.rb

Constant Summary collapse

HEADERS =
{
  'Accept' => 'application/json',
  'Accept-Encoding' => 'gzip',
  'User-Agent' => "megam-assembla/#{Megam::Assembla::VERSION}",
  'X-Ruby-Version' => RUBY_VERSION,
  'X-Ruby-Platform' => RUBY_PLATFORM
}
OPTIONS =
{
  :headers => {},
  :host => 'api.assembla.com',
  :nonblock => false,
  :scheme => 'https'
}
API_VERSION1 =
"/v1"
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssembla

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.



62
63
64
# File 'lib/megam/assembla.rb', line 62

def initialize
  @options = OPTIONS
end

Instance Attribute Details

#textObject

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



28
29
30
# File 'lib/megam/assembla.rb', line 28

def text
  @text
end

Instance Method Details

#get_spaces(access_token) ⇒ Object

GET /repos



5
6
7
8
9
10
11
12
# 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_responseObject



51
52
53
# File 'lib/megam/assembla.rb', line 51

def last_response
  @last_response
end

#request(params, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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