class Codestrap::Client

Public Class Methods

new(url) click to toggle source
# File lib/codestrap/client.rb, line 11
def initialize(url)
  uri     = URI url
  @url    = url
  @scheme = uri.scheme
  @host   = uri.host
  @port   = uri.port
  self.capability uri
end

Public Instance Methods

cache() click to toggle source

Cache directory path. Will be created if directory doesn't exist

@return [String]

# File lib/codestrap/client.rb, line 23
def cache
  @cache ||= begin
    cache = File.join(Dir.tmpdir, Etc.getlogin, digest, 'codestrap')
    FileUtils.mkpath cache unless File.exist? cache
    @cache = cache
  end
end
cache_content() click to toggle source

Content cache directory path. Will be created if directory doesn't exists

@return [String]

# File lib/codestrap/client.rb, line 34
def cache_content
  @cache_content ||= begin
    cache_content = File.join(cache, 'content')
    FileUtils.mkpath cache_content unless File.exist? cache_content
    @cache_content = cache_content
  end
end
cache_object() click to toggle source

Object cache directory path. Will be created if directory doesn't exists

@return [String]

# File lib/codestrap/client.rb, line 45
def cache_object
  @cache_object ||= begin
    cache_object = File.join(cache, 'objects')
    FileUtils.mkpath cache_object unless File.exist? cache_object
    @cache_object = cache_object
  end
end
capability(uri=nil) click to toggle source

Sets capability URL. Returns JSON capability document as hash.

@param [URI] uri

Capability URI

@return [Hash|nil]

JSON document as a Hash
# File lib/codestrap/client.rb, line 66
def capability(uri=nil)
  return @capability unless uri
  begin
    response    = Net::HTTP.get_response URI uri
    @capability = JSON.parse(response.body)
    return @capability
  rescue Errno::ECONNREFUSED
    nil
  end
end
digest() click to toggle source

Return MD5 hexdigest digest of the capability URL

@return [String]

# File lib/codestrap/client.rb, line 56
def digest
  @digest ||= (Digest::MD5.hexdigest @url)
end
getobjects(objects=nil) click to toggle source
# File lib/codestrap/client.rb, line 126
def getobjects(objects=nil)
  return {} unless @capability
  response = Net::HTTP.get_response URI objecturl
  JSON.parse(response.body)
end
getstrap(project) click to toggle source
# File lib/codestrap/client.rb, line 151
def getstrap(project)
  return nil unless @capability
  path     = File.join(cache_content, project)

  # Get metadata
  metadata = strapmetadata project
  if not metadata or metadata.empty?
    return nil
  end

  # Make directories
  metadata['files'].each do |file|
    next unless file['ftype'].eql?('directory')
    FileUtils.mkdir_p File.join(path, file['file'])
  end

  # Make files
  metadata['files'].each do |file|
    next unless file['ftype'].eql?('file')
    response = Net::HTTP.get_response URI strapprojecturl + "/#{project}/" + file['file']
    File.open(File.join(path, file['file']), 'w') do |fh|
      fh.write response.body
    end
  end

  path
end
getstub(codestrap) click to toggle source
# File lib/codestrap/client.rb, line 132
def getstub(codestrap)
  return nil unless @capability
  path     = File.join(cache_content, codestrap + '.erb')

  # Get metadata
  metadata = stubmetadata codestrap
  if not metadata or metadata.empty?
    return nil
  end

  # create file
  response = Net::HTTP.get_response URI stubfileurl + "/#{codestrap}"
  File.open(path, 'w') do |file|
    file.write response.body
  end

  path
end
objecturl() click to toggle source
# File lib/codestrap/client.rb, line 121
def objecturl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['objects']}"
end
straplist() click to toggle source
# File lib/codestrap/client.rb, line 105
def straplist
  return nil unless @capability
  response = Net::HTTP.get_response URI strapmetadataurl
  JSON.parse(response.body).keys
end
strapmetadata(project) click to toggle source
# File lib/codestrap/client.rb, line 99
def strapmetadata(project)
  return nil unless @capability
  response = Net::HTTP.get_response URI strapmetadataurl + '?name=' + project
  JSON.parse(response.body)[project]
end
strapmetadataurl() click to toggle source
# File lib/codestrap/client.rb, line 111
def strapmetadataurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['strap']['metadata']}"
end
strapprojecturl() click to toggle source
# File lib/codestrap/client.rb, line 116
def strapprojecturl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['strap']['project']}"
end
stubfileurl() click to toggle source
# File lib/codestrap/client.rb, line 94
def stubfileurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['stub']['file']}"
end
stublist() click to toggle source
# File lib/codestrap/client.rb, line 88
def stublist
  return nil unless @capability
  response = Net::HTTP.get_response URI stubmetadataurl
  JSON.parse(response.body).keys
end
stubmetadata(file) click to toggle source
# File lib/codestrap/client.rb, line 77
def stubmetadata(file)
  return nil unless @capability
  response = Net::HTTP.get_response URI stubmetadataurl + '?name=' + file
  JSON.parse(response.body)
end
stubmetadataurl() click to toggle source
# File lib/codestrap/client.rb, line 83
def stubmetadataurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['stub']['metadata']}"
end