Class: Richcss::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/richcss/part.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/richcss/part.rb', line 8

def name
  @name
end

Class Method Details

.fetch(part_name, version) ⇒ Object

Fetch url and download the part



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/richcss/part.rb', line 40

def self.fetch(part_name, version)
  puts "Fetching part #{part_name}"

  begin
    resp = RestClient.get "http://www.cssparts.com/api/part/#{part_name}"
    if resp.code == 200
      body = JSON.parse(resp.to_str)
      homepage = body["homepage"]
      homepage.slice! "https:\/\/github.com\/"
      homepage = homepage.split("\/")
      repo_owner = homepage[0]
      repo_name = homepage[1]
      jsonResponse = JSON.parse(Net::HTTP.get(URI("https://api.github.com/repos/#{repo_owner}/#{repo_name}/releases/tags/#{body["version"]}")))
      downloadLink = jsonResponse["zipball_url"]
      install(part_name, body["version"], downloadLink)
    else
      puts "Error: Part #{name} cannot be found."
    end
  rescue RestClient::ExceptionWithResponse => e
    puts e.response
  end
end

.get_or_create_partfileObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/richcss/part.rb', line 15

def self.get_or_create_partfile()
  if !Dir.exists?('parts')
    FileUtils.mkdir_p('parts')
  end

  parts = Hash.new
  Dir.chdir('parts') do
    part_file = "Partfile"

    begin
      File.open(part_file, "r") do |f|
        f.each_line do |line|
        part, version = line.split(" ")
          parts[part] = version
        end
      end
    rescue
      File.new(part_file, "w")
    end
  end

  return parts
end

.install(part_name, version, resource) ⇒ Object

Install this part



64
65
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
# File 'lib/richcss/part.rb', line 64

def self.install(part_name, version, resource)
  uri = URI.parse(resource)

  http_object = Net::HTTP.new(uri.host, uri.port)
  http_object.use_ssl = true if uri.scheme == 'https'
  begin
    http_object.start do |http|
      request = Net::HTTP::Get.new uri.request_uri
      http.read_timeout = 500
      http.request request do |response|
        case response
        when Net::HTTPRedirection then
          location = response['location']
          install(part_name, version, location)
          return
        else
          puts "Installing part..."

          if !Dir.exists?('parts')
            FileUtils.mkdir_p('parts')
          end

          Dir.chdir('parts') do
            if Dir.exists?(part_name) 
              FileUtils.remove_dir(part_name)
            end
            Zip::Archive.open_buffer(response.body) do |ar|
               #save the directory name for rename later
               oldDirName = ar.get_name(0)

               ar.each do |zf|
                  if zf.directory?
                     FileUtils.mkdir_p(zf.name)
                  else
                     dirname = File.dirname(zf.name)
                     FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
                     open(zf.name, 'wb') do |f|
                        f << zf.read
                     end
                  end
               end

               FileUtils.mv oldDirName, part_name
            end
          end
        end
      end
    end

    RestClient.post "http://www.cssparts.com/api/part/downloaded", :name => part_name, :version => version
  rescue Exception => e
    puts e
  end
end

.resolve_dependencies(part_name, version, installed = {}) ⇒ Object

placeholder for latest version is blank



11
12
13
# File 'lib/richcss/part.rb', line 11

def self.resolve_dependencies(part_name, version, installed={})
  dep_list = Richcss::Resolver.start(part_name, version, installed)
end