Class: Colourlovers::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Rather than making laborious calls to ‘Colourlovers::Client.colors` you can create an instance of the Colourlovers::Client and call methods on it. Instance methods will be defined for class methods on their first utilization.

Examples:

cl = Colourlovers::Client.new
cl.colors    #=> [...]


15
16
17
18
19
20
21
22
23
# File 'lib/colourlovers/client.rb', line 15

def method_missing(method, *args, &block)
  super unless self.class.respond_to? method
  self.class.class_eval do
    define_method method do |*args, &block|
      self.class.send(method, *args, &block)
    end
  end
  self.class.send(method, *args, &block)
end

Class Method Details

.color(hex) ⇒ Object

Retrieve color from hex value



88
89
90
# File 'lib/colourlovers/client.rb', line 88

def color(hex)
  Color.find(hex)
end

.colors(options = {}) ⇒ Object

List colors



64
65
66
# File 'lib/colourlovers/client.rb', line 64

def colors(options={})
  Color.all(options)
end

.handle_response(response) ⇒ Object

TODO:

Add individual errors for various HTTP Status codes

Handle responses from HTTParty calls to the COLOURlovers API with some generic response interpretation and manipulation.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/colourlovers/client.rb', line 31

def handle_response(response)
  status = response.code
  
  case status
    when 404 then STDERR.puts "404 - maybe COLOURlovers down? #{$!}"
    when 400..499 then STDERR.puts "400-499 - maybe COLOURlovers down? #{$!}"
    when 500..599 then STDERR.puts "500-599 - maybe COLOURlovers down? #{$!}"
    else response.first
  end

end

.lover(username) ⇒ Object

Retrieve lover from username

See Also:



178
179
180
# File 'lib/colourlovers/client.rb', line 178

def lover(username)
  Lover.find(username)
end

.lovers(options = {}) ⇒ Object

List lovers

See Also:



160
161
162
# File 'lib/colourlovers/client.rb', line 160

def lovers(options={})
  Lover.all(options)
end

.new_colors(options = {}) ⇒ Object

List new colors



76
77
78
# File 'lib/colourlovers/client.rb', line 76

def new_colors(options={})
 Color.new(options)
end

.new_lovers(options = {}) ⇒ Object

List new lovers

See Also:



172
173
174
# File 'lib/colourlovers/client.rb', line 172

def new_lovers(options={})
 Lover.new(options)
end

.new_palettes(options = {}) ⇒ Object

List new palettes

See Also:



108
109
110
# File 'lib/colourlovers/client.rb', line 108

def new_palettes(options={})
 Palette.new(options)
end

.new_patterns(options = {}) ⇒ Object

List new patterns

See Also:



140
141
142
# File 'lib/colourlovers/client.rb', line 140

def new_patterns(options={})
 Pattern.new(options)
end

.palette(paletteID) ⇒ Object

Retrieve palette from paletteID

See Also:



120
121
122
# File 'lib/colourlovers/client.rb', line 120

def palette(paletteID)
  Palette.find(paletteID)
end

.palettes(options = {}) ⇒ Object

List palettes

See Also:



96
97
98
# File 'lib/colourlovers/client.rb', line 96

def palettes(options={})
  Palette.all(options)
end

.pattern(patternID) ⇒ Object

Retrieve pattern from patternID

See Also:



152
153
154
# File 'lib/colourlovers/client.rb', line 152

def pattern(patternID)
  Pattern.find(patternID)
end

.patterns(options = {}) ⇒ Object

List patterns

See Also:



128
129
130
# File 'lib/colourlovers/client.rb', line 128

def patterns(options={})
  Pattern.all(options)
end

.random_colorObject

Retrieve random color



82
83
84
# File 'lib/colourlovers/client.rb', line 82

def random_color
  Color.random
end

.random_paletteObject

Retrieve random palette

See Also:



114
115
116
# File 'lib/colourlovers/client.rb', line 114

def random_palette
  Palette.random
end

.random_patternObject

Retrieve random pattern

See Also:



146
147
148
# File 'lib/colourlovers/client.rb', line 146

def random_pattern
  Pattern.random
end

.top_colors(options = {}) ⇒ Object

List top colors



70
71
72
# File 'lib/colourlovers/client.rb', line 70

def top_colors(options={})
  Color.top(options)
end

.top_lovers(options = {}) ⇒ Object

List top lovers

See Also:



166
167
168
# File 'lib/colourlovers/client.rb', line 166

def top_lovers(options={})
  Lover.top(options)
end

.top_palettes(options = {}) ⇒ Object

List top palettes

See Also:



102
103
104
# File 'lib/colourlovers/client.rb', line 102

def top_palettes(options={})
  Palette.top(options)
end

.top_patterns(options = {}) ⇒ Object

List top patterns

See Also:



134
135
136
# File 'lib/colourlovers/client.rb', line 134

def top_patterns(options={})
  Pattern.top(options)
end