Class: Odlifier::License

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ License

Returns a new instance of License.



7
8
9
10
11
# 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

Class Method Details

.define(id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
# 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