Sha256: 2293b4d270b634e1886e2211db9b7161cee9f7e8cdc239cb4ed9822f5be0cada

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

require 'hyperclient'
require 'pp'

def print_resources(resources)
  resources.each do |name, resource|
    begin
      puts %{Found #{name} at #{resource.url}}
    rescue
      puts %{Found #{name}}
    end
  end
end

def print_attributes(attributes)
  puts "-----------------------------"
  attributes.each do |attribute, value|
    puts %{#{attribute}: #{value}}
  end
end

api = Hyperclient::EntryPoint.new 'http://hal-shop.heroku.com'

puts "Let's inspect the API:"
puts "\n"

puts 'Links from the entry point:'

print_resources(api.links)

puts
puts "Let's see what stats we have:"
print_attributes(api.embedded.stats.attributes)

products = api.links["http://hal-shop.heroku.com/rels/products"].resource

puts 
puts "And what's the inventory of products?"
puts products.attributes['inventory_size']

puts
puts 'What embedded resources does products have?'
products.embedded.products.each do |product|
  puts 'Product:'
  print_attributes(product.attributes)
  puts
end

puts 'And links?'
print_resources(products.links)

puts
puts 'Attributes of the first product?'
print_attributes(products.embedded.products.first.attributes)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hyperclient-0.4.0 examples/hal_shop.rb
hyperclient-0.3.2 examples/hal_shop.rb
hyperclient-0.3.1 examples/hal_shop.rb
hyperclient-0.3.0 examples/hal_shop.rb
hyperclient-0.2.0 examples/hal_shop.rb
hyperclient-0.1.0 examples/hal_shop.rb