Class: Codestrap::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/codestrap/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# 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

Instance Method Details

#cacheString

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

Returns:

  • (String)


23
24
25
26
27
28
29
# 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_contentString

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

Returns:

  • (String)


34
35
36
37
38
39
40
# 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_objectString

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

Returns:

  • (String)


45
46
47
48
49
50
51
# 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) ⇒ Hash|nil

Sets capability URL. Returns JSON capability document as hash.

Parameters:

  • uri (URI) (defaults to: nil)

    Capability URI

Returns:

  • (Hash|nil)

    JSON document as a Hash



66
67
68
69
70
71
72
73
74
75
# 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

#digestString

Return MD5 hexdigest digest of the capability URL

Returns:

  • (String)


56
57
58
# File 'lib/codestrap/client.rb', line 56

def digest
  @digest ||= (Digest::MD5.hexdigest @url)
end

#getobjects(objects = nil) ⇒ Object



126
127
128
129
130
# 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) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/codestrap/client.rb', line 151

def getstrap(project)
  return nil unless @capability
  path     = File.join(cache_content, project)

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

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

  # Make files
  ['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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/codestrap/client.rb', line 132

def getstub(codestrap)
  return nil unless @capability
  path     = File.join(cache_content, codestrap + '.erb')

  # Get metadata
   =  codestrap
  if not  or .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

#objecturlObject



121
122
123
124
# File 'lib/codestrap/client.rb', line 121

def objecturl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['objects']}"
end

#straplistObject



105
106
107
108
109
# 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) ⇒ Object



99
100
101
102
103
# File 'lib/codestrap/client.rb', line 99

def (project)
  return nil unless @capability
  response = Net::HTTP.get_response URI strapmetadataurl + '?name=' + project
  JSON.parse(response.body)[project]
end

#strapmetadataurlObject



111
112
113
114
# File 'lib/codestrap/client.rb', line 111

def strapmetadataurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['strap']['metadata']}"
end

#strapprojecturlObject



116
117
118
119
# File 'lib/codestrap/client.rb', line 116

def strapprojecturl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['strap']['project']}"
end

#stubfileurlObject



94
95
96
97
# File 'lib/codestrap/client.rb', line 94

def stubfileurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['stub']['file']}"
end

#stublistObject



88
89
90
91
92
# 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) ⇒ Object



77
78
79
80
81
# File 'lib/codestrap/client.rb', line 77

def (file)
  return nil unless @capability
  response = Net::HTTP.get_response URI stubmetadataurl + '?name=' + file
  JSON.parse(response.body)
end

#stubmetadataurlObject



83
84
85
86
# File 'lib/codestrap/client.rb', line 83

def stubmetadataurl
  return nil unless @capability
  "#{@scheme}://#{@host}:#{@port}#{@capability['urls']['stub']['metadata']}"
end