Sha256: 8d3f961120bb3b9fc69751474ba43573f759dc70a7370e43fdb1ae406fa27c17

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

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

ActiveRecord::Base.logger = Spigot.logger
ActiveRecord::Base.establish_connection({
  :adapter => "sqlite3",
  :database => ':memory:'
})

ActiveRecord::Schema.define do
  self.verbose = false
  create_table :users, :force => true do |t|
    t.integer :github_id
    t.string  :username
    t.string  :image_url
    t.string  :profile_url
  end
end

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

Spigot.configure do |config|
  config.translations = {'user' => {
    'id'         => 'github_id',
    'login'      => 'username',
    'avatar_url' => 'image_url',
    'url'        => 'profile_url',
    'spigot' => {
      'primary_key' => 'username'
    }
  }}
end

puts "Making a request to an external API (Github)"
response = Net::HTTP.get_response URI.parse("https://api.github.com/users/mwerner")
puts "Parse the response: `data = JSON.parse(response.body)`"
data = JSON.parse(response.body)

puts "\nReceived a whole bunch of data: "
puts "#{data.inspect[0..100]}... etc, etc, etc (#{data.keys.length} more keys received)"

puts "\nWe don't want to use all of it. We can define a map on Spigot:"
puts User.spigot(:github).map.inspect
puts "Each key is an attribute received from the API, and the corresponding value is our column name."

puts "\nWe define our primary key in the spigot `User` options, so we know how to check if the record already exists:"
puts User.spigot(:github).options.inspect


puts "\nWe can create a new user with one nice and easy line: `User.find_or_create_by_api(:github, data)`"
puts User.find_or_create_by_api(:github, data).inspect

puts "\nEnjoy!"

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spigot-0.1.0 examples/active_record.rb
spigot-0.0.1 examples/active_record.rb