palettes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << "#{File.dirname(__FILE__)}/../lib/waves"
4
+
5
+ require 'rubygems'
6
+ require 'waves'
7
+
8
+ require 'runtime/server'
9
+
10
+ begin
11
+ Waves::Server.run( :startup => "startup.rb" )
12
+ rescue LoadError => e
13
+ puts e.message
14
+ end
@@ -0,0 +1,78 @@
1
+ # Gems
2
+ require 'rubygems'
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+
7
+ def Kernel.engine; defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'mri'; end
8
+ require 'jruby/openssl/gem_only' if Kernel.engine == 'jruby'
9
+
10
+ # Application Local
11
+ require 'mixin'
12
+
13
+ module Palettes
14
+ class ColourLovers
15
+
16
+ include Palettes::Mixin
17
+
18
+ def initialize(palette_id, names=false)
19
+ @palette = self.class.get_palette(palette_id, names)
20
+ end
21
+
22
+
23
+ # Returns { :title => id }
24
+ def self.search(*keywords)
25
+ if keywords.respond_to? :assoc
26
+ words = keywords.inject {|result,keyword| result += "+#{keyword}"}
27
+ words.gsub!(' ','+')
28
+ else
29
+ words = keywords.gsub(' ','+')
30
+ end
31
+
32
+ palettes = ColourLovers.grab_list(words)
33
+ return nil if palettes.empty?
34
+ titles = []; ids = []; colors = []; creators = []
35
+ palettes.each do |palette|
36
+ titles << palette['title']
37
+ ids << palette['id']
38
+ colors << palette['colors']
39
+ creators << palette['userName']
40
+ end
41
+ composites=[]
42
+ (0..(titles.length-1)).each {|x| composites[x] = {:title => titles[x], :id => ids[x], :colors => colors[x], :creator => creators[x]} }
43
+ composites
44
+ end
45
+
46
+ def self.get_palette(palette_id, names=false)
47
+ #raise ArgumentError unless palette_id.is_a? Integer
48
+ @palette = ColourLovers.grab_single palette_id.to_s
49
+ colors ||= @palette.pop['colors']
50
+
51
+ if names
52
+ color_names=[]
53
+ colors.each do |c|
54
+ response = JSON.load( Net::HTTP.get( URI.parse "http://www.colourlovers.com/api/color/#{c.to_s}?format=json" ))
55
+ color_names << response.pop['title']
56
+ end
57
+ colors.map! { |c| [color_names.shift, c] }
58
+ else
59
+ colors
60
+ end
61
+ end
62
+
63
+ private
64
+ def ColourLovers.grab_list(args)
65
+ if args.is_a? Hash
66
+ raise ArgumentError if args[:keywords].nil?
67
+ params = args.inject {|p,(a,v)| p + "#{a}=#{b}&" } # We will always add a format, so don't fear the trailing '&'
68
+ else
69
+ params = "keywords=#{args}&" if args.is_a? String
70
+ end
71
+ json = JSON.load( Net::HTTP.get( URI.parse 'http://www.colourlovers.com/api/palettes?' + params + 'format=json' ))
72
+ end
73
+
74
+ def ColourLovers.grab_single(palette_id)
75
+ json = JSON.load( Net::HTTP.get( URI.parse "http://www.colourlovers.com/api/palette/#{palette_id}?format=json" ))
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,46 @@
1
+ # Palettes::Mixin
2
+ # Provides basic palette methods.
3
+
4
+ module Palettes
5
+
6
+ module Mixin
7
+ attr_reader :colors
8
+
9
+ # Palette colors may be supplied as an array of values, or an array of name/value pairs:
10
+ # Palette.new [ "#eeeeee", "#666600" ]
11
+ # Palette.new [ ["red", "#ff0000"], ["blue", "#0000ff"] ]
12
+
13
+ def fill(color_array)
14
+ @palette = color_array
15
+ end
16
+
17
+ def colors
18
+ colors = []
19
+ # We could use #map here, but then I'd have to use #compact
20
+ @palette.each { |c| c.respond_to?( :assoc ) ? colors << c.last : colors << c }
21
+ colors
22
+ end
23
+
24
+ def names
25
+ n = []
26
+ # We could use #map here, but then I'd have to use #compact
27
+ @palette.each { |c| c.respond_to?( :assoc ) ? n << c.first : nil }
28
+ n.empty? ? nil : n
29
+ end
30
+
31
+ def [](arg)
32
+ color = arg.is_a?(Integer) ? @palette[arg] : @palette.assoc(arg)
33
+ color.respond_to?(:last) ? color.last : color
34
+ end
35
+
36
+ def to_hash
37
+ Hash[ *@palette.flatten ]
38
+ end
39
+
40
+ def raw
41
+ @palette
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1 @@
1
+ require 'colourlovers'
@@ -0,0 +1,34 @@
1
+ require 'foundations/compact'
2
+ require 'autocode'
3
+
4
+ require 'hoshi'
5
+
6
+ require 'views'
7
+ require 'palettes'
8
+
9
+
10
+ module PaletteApp
11
+ include Waves::Foundations::Compact
12
+
13
+ module Resources
14
+ class Map
15
+
16
+ # Set up our views
17
+ @index = Views::Index.new
18
+ assigns = {}
19
+
20
+ on(:get, true) { @index = Views::Index.new; @index.show }
21
+
22
+ on(:post, []) {
23
+ result = Palettes::ColourLovers.search( query['keywords'] )
24
+ @index ||= Views::Index.new
25
+ if result.nil?
26
+ @index.not_found query['keywords']
27
+ else
28
+ @index.search result, query['keywords']
29
+ end
30
+ }
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,134 @@
1
+ require 'hoshi'
2
+ require 'cassandra'
3
+
4
+ module Views
5
+ class Index < Hoshi::View(:xhtml1)
6
+ def show
7
+ doctype
8
+ html {
9
+ head {
10
+ title "Palettes"
11
+ }
12
+ body(:bgcolor => "#BCBDAC", :style => "font-family: sans-serif") {
13
+ h1 "Palettes"
14
+ h3 "A Simple Interface to Glorious Possibility"
15
+ form(:action => '/', :method => 'post') {
16
+ input(:type => 'text', :name => 'keywords')
17
+ input(:type => 'submit', :value => 'Search')
18
+ }
19
+ }
20
+ }
21
+ render
22
+ end
23
+
24
+ def not_found(keywords)
25
+ doctype
26
+ html {
27
+ head {
28
+ title "Palettes: No palettes found :("
29
+ }
30
+ body(:bgcolor => "#BCBDAC", :style => "font-family: sans-serif") {
31
+ h1 "No palettes found for #{keywords} :("
32
+ }
33
+ }
34
+ render
35
+ end
36
+
37
+ def search(result, keywords)
38
+ doctype
39
+ html {
40
+ head {
41
+ # Begin Cassandra
42
+ @css = Cssy.new
43
+ @css.process {
44
+
45
+ li.palette_box {
46
+ border(:solid)
47
+ margin_bottom('3em')
48
+ background_color("#DBE6EC")
49
+ width('780px')
50
+ height('350px')
51
+ position(:relative)
52
+ z_index('1')
53
+ }
54
+
55
+ td.color_bar {
56
+ vertical_align(:top)
57
+ font_size('xx-small')
58
+ }
59
+
60
+ table.colors {
61
+ width('440px');
62
+ height('330px');
63
+ border(:solid);
64
+ margin('10px');
65
+ position(:relative)
66
+ z_index('2')
67
+ }
68
+
69
+ div.info_box {
70
+ position(:absolute)
71
+ top('0px')
72
+ left('480px')
73
+ z_index('2')
74
+ width("295px")
75
+ }
76
+
77
+ dl.info {
78
+ margin_top("1.5em")
79
+ list_style(:none)
80
+ position(:relative)
81
+ }
82
+
83
+ dd{
84
+ margin_left('1em')
85
+ padding('0')
86
+ }
87
+
88
+ ul.colors { list_style(:none); margin("0"); padding("0") }
89
+ li.colors { margin_left("3px") }
90
+ ol.palettes { list_style(:none); position(:relative) }
91
+ body { position(:relative) }
92
+ a { color(:black); text_decoration(:none) }
93
+ selector("a:hover") { text_decoration(:underline) }
94
+ }
95
+ style @css.to_s, "type"=>"text/css"
96
+ # End Cassandra
97
+
98
+ title "Palette Search: #{keywords}"
99
+ }
100
+ body(:bgcolor => "#BCBDAC", :style => "font-family: sans-serif") {
101
+ h1 "Palettes"
102
+ h3 "Search results for: #{keywords}"
103
+ ol(:class => "palettes") {
104
+ result.each do |r|
105
+ li(:class => "palette_box") {
106
+ table(:class => "colors", 'cellspacing' => '0px' ) { tr {
107
+ r[:colors].each {|color|
108
+ td(:class => 'color_bar', :title => "##{color}",
109
+ :style => " width: #{440/r[:colors].length}px;
110
+ color: ##{color};
111
+ background-color: ##{color}") { raw "##{color}" }
112
+ }#td (loop)
113
+ } }#tr, table.colors
114
+ div(:class => "info_box") {
115
+ h3 { a(:href => "http://colourlovers.com/palette/#{r[:id].to_s}") { raw "#{r[:title].to_s}" } }
116
+ dl(:class => "info") {
117
+ dt { raw "Creator: " }
118
+ dd { raw "#{r[:creator]}"}
119
+ dt { raw "ID: " }
120
+ dd { raw "#{r[:id]}"}
121
+ dt { raw "Colors: " }
122
+ dd { ul(:class => "colors") { r[:colors].each {|color| li(:class=>"colors") { raw "##{color}" } }}}
123
+ }#dl.info
124
+ }#div.info_box
125
+ }#li.palette_box
126
+ end
127
+ }#ol.palettes
128
+ }
129
+ }
130
+ render
131
+ end
132
+
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: palettes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ab5tract
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: waves
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoshi
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: cassandra
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: json
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Palettes is for making masterpieces.
56
+ email: john.haltiwanger@gmail.com
57
+ executables:
58
+ - palettes
59
+ extensions: []
60
+
61
+ extra_rdoc_files: []
62
+
63
+ files:
64
+ - bin/palettes
65
+ - lib/palettes.rb
66
+ - lib/colourlovers.rb
67
+ - lib/mixin.rb
68
+ - lib/waves/startup.rb
69
+ - lib/waves/views.rb
70
+ has_rdoc: false
71
+ homepage: http://github.com/ab5tract/palettes
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.1
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: Palettes accesses the ColourLovers API through a Waves instance.
96
+ test_files: []
97
+