Class: UR::MetadataCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ur/metadata_cache.rb

Overview

Responsible for retrieving metadata and populating one or more UR::Product objects

Constant Summary collapse

METADATA_PRODUCT_URL =
'http://metadata.ur.se/products'

Class Method Summary collapse

Class Method Details

.find(id) ⇒ Object

Retrieve one or more products



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ur/metadata_cache.rb', line 15

def self.find(id)
  if id.instance_of?(Array)
    valid_ids = []
    
    id.each do |id|
      if id.to_s.match(/^1\d{5}$/)
        valid_ids << id 
      else
        raise UR::InvalidProductID
      end
    end
    
    url = METADATA_PRODUCT_URL + ".json?ur_product_ids=#{valid_ids.join(',')}"
  else
    raise UR::InvalidProductID if !id.to_s.match(/^1\d{5}$/)
    url = METADATA_PRODUCT_URL + "/#{id}.json"
  end
  
  # Get the JSON response from the Metadata Cache
  response = RestClient.get(url)
  
  # Make sure that we got a valid response
  raise UR::InvalidResponse if response.code != 200
  
  # Return the response as a parsed JSON object
  JSON.parse(response.body)
end