Sha256: 6497bcc69c43b7cf171aecb8ff1947ce2c33c98b78b9a660e610ec8efb2292ae

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'lazy_resource'
require 'benchmark'

class SimpleLogger
  def info(message)
    puts message
  end
end

LazyResource.configure do |config|
  config.site = "https://api.github.com"
  config.logger = SimpleLogger.new
  config.debug = true
end

class User
  include LazyResource::Resource
end

class Repo
  include LazyResource::Resource

  attribute :id,          Fixnum
  attribute :pushed_at,   DateTime
  attribute :owner,       User
  attribute :clone_url,   String
  attribute :name,        String
  attribute :description, String
end

class User
  include LazyResource::Resource

  self.primary_key_name = 'login'

  attribute :id,          Fixnum
  attribute :login,       String
  attribute :name,        String
  attribute :bio,         String
  attribute :company,     String
  attribute :number_of_public_repos, Fixnum, :from => 'public_repos'
  attribute :repos,       [Repo]
end

dhh = User.find('dhh')
dhh.repos.each do |repo|
  puts "#{dhh.name} pushed to #{repo.name} on #{repo.pushed_at.strftime("%D")}"
end

puts "Fetching 10 users serially..."
Benchmark.bm do |x|
  x.report do
    names = []
    10.times do |i|
      u = User.find(i + 1)
      names << u.name
    end
    
    puts names
  end
end

puts "\nFetching 10 users in parallel..."
Benchmark.bm do |x|
  x.report do
    users = []
    10.times do |i|
      users << User.find(i + 1)
    end
    puts users.map { |user| user.name }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lazy_resource-0.5.0 examples/github.rb