Class: Palettes::ColourLovers

Inherits:
Object
  • Object
show all
Includes:
Mixin
Defined in:
lib/colourlovers.rb

Instance Attribute Summary

Attributes included from Mixin

#colors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#[], #fill, #names, #raw, #to_hash

Constructor Details

#initialize(palette_id, names = false) ⇒ ColourLovers

Returns a new instance of ColourLovers.



18
19
20
# File 'lib/colourlovers.rb', line 18

def initialize(palette_id, names=false)
  @palette = self.class.get_palette(palette_id, names)
end

Class Method Details

.get_palette(palette_id, names = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/colourlovers.rb', line 46

def self.get_palette(palette_id, names=false)
  #raise ArgumentError unless palette_id.is_a? Integer
  @palette = ColourLovers.grab_single palette_id.to_s
  colors ||= @palette.pop['colors']
  
  if names
    color_names=[]
    colors.each do |c|
      response = JSON.load( Net::HTTP.get( URI.parse "http://www.colourlovers.com/api/color/#{c.to_s}?format=json" ))
      color_names << response.pop['title']
    end
    colors.map! { |c| [color_names.shift, c] }
  else
    colors
  end
end

.search(*keywords) ⇒ Object

Returns { :title => id }



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/colourlovers.rb', line 24

def self.search(*keywords)
  if keywords.respond_to? :assoc
    words = keywords.inject {|result,keyword| result += "+#{keyword}"}
    words.gsub!(' ','+')
  else
    words = keywords.gsub(' ','+')
  end

  palettes = ColourLovers.grab_list(words)
  return nil if palettes.empty?
  titles = []; ids = []; colors = []; creators = []
  palettes.each do |palette|
    titles << palette['title']
    ids << palette['id']
    colors << palette['colors']
    creators << palette['userName']
  end
  composites=[]
  (0..(titles.length-1)).each {|x| composites[x] = {:title => titles[x], :id => ids[x], :colors => colors[x], :creator => creators[x]} }
  composites
end