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



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
42
43
44
45
46
# File 'lib/ur/metadata_cache.rb', line 16

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

  begin
    url = URI.parse(url).to_s
    # Get the JSON response from the Metadata Cache
    response = Yajl::Parser.parse(RestClient.get(url))
  rescue RestClient::Exception
    # Raise an invalid response exception if there was
    # a problem with the HTTP request
    raise UR::InvalidResponse
  end

  # Return the response as a parsed JSON object
  response
end