Sha256: 86225aa5db00f90140f60d5ef87ad3dae4fe65b39784966b65e0afcdde62a716

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module <%= name.camelize %>
  class User
    include DataMapper::Resource
    class  UserCreationError < StandardError; end
    storage_names[:default] = '<%= name.camelize %>_users'

    property :id, Serial
    property :twitter_id, Integer, :nullable => false, :unique => true
    property :name, String
    property :token, String
    property :secret, String

    property :url, String, :length => 512
    property :avatar, String, :length => 512, :default => 'http://static.twitter.com/images/default_profile_normal.png'

    timestamps :at

    def access_token
      ::OAuth::AccessToken.new(::<%= name.camelize %>::OAuth.consumer, token, secret)
    end

    def self.create_twitter_user(twitter_id)
      content = Curl::Easy.perform("http://twitter.com/users/show/#{twitter_id}.json") do |curl|
        curl.timeout = 30
      end
      user_info = JSON.parse(content.body_str)
      raise UserCreationError.new("Unable to find '#{twitter_id}'") if(user_info['error'] == 'Not found')
      result = unless user_info['error']
        self.first_or_create({:twitter_id => user_info['id']},{
                              :name       => user_info['name'],
                              :avatar     => user_info['profile_image_url'],
                              :url        => 'http://twitter.com/'+user_info['screen_name']})
      end
    rescue JSON::ParserError
      raise UserCreationError.new("Unable to find '#{twitter_id}'")
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
atmos-singem-0.0.5 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.7 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.6 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.5 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.3 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.4 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.0.2 lib/generators/twitter/templates/lib/templates/models/user.rb.erb