Sha256: e8f5778abf32811f917e780f3ea945335d12930cf064e3218e4603077e31f4d5

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

require 'active_record'
require 'spigot'
require 'net/http'
require 'uri'

ActiveRecord::Base.logger = Spigot.logger
require_relative '../script/active_record'

class Event < ActiveRecord::Base
  include Spigot::Base
end

class ActiveUser < ActiveRecord::Base
  include Spigot::Base
end

Spigot.define do
  resource :active_user do
    id :github_id
    name :name
    login :username
    gravatar_id :token
    options do
      primary_key :username
    end
  end

  resource :event do
    id   :github_id
    type :name
    actor ActiveUser
    options do
      primary_key :id
    end
  end
end

events_url  = 'https://api.github.com/events'

puts 'We can start off by making a request to get the latest github events'
response = Net::HTTP.get_response URI.parse(events_url)
puts "Parse the response:\n `data = JSON.parse(response.body)`"
event_data = JSON.parse(response.body)

puts "\nIt returns a whole bunch of data: "
puts "#{event_data.inspect[0..100]} ... etc, etc, etc\n\n"

puts 'Lets take the first three events and run them through spigot.'
puts 'We only want a few attributes: id, type and the associated user.'
puts Event.spigot.map.to_hash.inspect

puts "We'll assign a primary key in our options to make sure we don't duplicate events"
puts Event.spigot.options.inspect

puts "\nWe can parse all the events with one nice and easy line: `Event.spigot.find_or_create(data)`"
puts Event.spigot.find_or_create(event_data.first(3)).inspect

puts "\nSpigot has used the ActiveUser spigot map to create users for each event"
puts ActiveUser.all.map { |user| "#{user.id}: #{user.username}" }

puts "\nEnjoy!"

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spigot-0.3.3 examples/multi_resource.rb
spigot-0.3.2 examples/multi_resource.rb
spigot-0.3.1 examples/multi_resource.rb
spigot-0.3.0 examples/multi_resource.rb