Class: Kuler

Inherits:
Object
  • Object
show all
Defined in:
lib/kuler.rb,
lib/kuler/theme.rb,
lib/kuler/swatch.rb

Defined Under Namespace

Classes: Swatch, Theme

Constant Summary collapse

VERSION =

:nodoc:

'0.1.0'
BASE_URL =

:nodoc:

"http://kuler-api.adobe.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Kuler

Create a new Kuler object. Accepts a single argument, the api_key.



16
17
18
# File 'lib/kuler.rb', line 16

def initialize( api_key )
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

the key required to access the kuler API



12
13
14
# File 'lib/kuler.rb', line 12

def api_key
  @api_key
end

Instance Method Details

#build_url(args = {}) ⇒ Object

Build the appropriate URL for a request to the Kuler API.

Parameters:

type

the type of API call to make. Options are recent, popular, rating, or random.

limit

the number of themes to return. Valid range is 1 to 100.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kuler.rb', line 27

def build_url( args = {} )
  # default options
  opts = {
    :type  => :recent,
    :limit => 20
  }.merge( args )

  unless [ :recent, :popular, :rating, :random ].include? opts[:type]
    raise ArgumentError, "unknown feed type '#{opts[:type]}'. Valid options are recent, popular, rating, or random"
  end

  unless (1..100).include? opts[:limit]
    raise ArgumentError, "invalid limit: #{opts[:limit]}. Valid options are 1-100"
  end

  options = {
    :key          => self.api_key,

    :itemsPerPage => opts[:limit],
    :listType     => opts[:type],
  }

  get_args = options.
    sort_by {|k,v| k.to_s }.
    map     {|k,v| "#{k}=#{v}" }.
    join( "&" )

  return "#{BASE_URL}/rss/get.cfm?#{get_args}"
end

#fetch_random_themeObject

fetch a single random color theme



58
59
60
61
62
# File 'lib/kuler.rb', line 58

def fetch_random_theme
  url = build_url( :type => :random, :limit => 1 )
  xml = Nokogiri::XML( open(url) ).at( "//kuler:themeItem" )
  return Kuler::Theme.new( xml )
end