class Odlifier::License

Public Class Methods

define(id) click to toggle source
# File lib/odlifier.rb, line 13
def self.define(id)
  response = self.get("http://licenses.opendefinition.org/licenses/#{id}.json")
  unless response.header.code == "404"
    License.new(response.parsed_response)
  else
    # Try and get an ID with a version number
    candidates = gather_candidiates(id)
    if candidates.count == 0
      raise ArgumentError, "License with the id '#{id}' cannot be found"
    else
      License.new(candidates.last) # Assume the latest version
    end
  end
end
gather_candidiates(id) click to toggle source
# File lib/odlifier.rb, line 28
def self.gather_candidiates(id)
  candidates = []
  response = self.get("http://licenses.opendefinition.org/licenses/groups/all.json")
  response.parsed_response.each do |key, val|
    candidates << val if license_id_matches(id, key)
  end
  candidates
end
license_id_matches(id, key) click to toggle source
# File lib/odlifier.rb, line 37
def self.license_id_matches(id, key)
  if id.match /[0-9]+\.[0-9]+$/
    key.match /#{id}/i
  else
    key.match /#{id}-[0-9]+\.[0-9]+/i
  end
end
new(hash) click to toggle source
# File lib/odlifier.rb, line 7
def initialize(hash)
  hash.each do |key, val|
    self.class.send(:define_method, key.to_sym) { val }
  end
end