Sha256: 349ebf846eb80de8c1257fa94a595bd774a0dcd2b0a71852bb78784fa5fa20cc

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 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, :required => true, :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

4 entries across 4 versions & 1 rubygems

Version Path
singem-0.2.0 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.1.2 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.1.1 lib/generators/twitter/templates/lib/templates/models/user.rb.erb
singem-0.1.0 lib/generators/twitter/templates/lib/templates/models/user.rb.erb