Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browsing_history

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browsing_history

  • 0.0.4
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

BrowsingHistory

BrowsingHistory is recording browsing history simply. it backed by Redis.

Compatibility

The gem has been build and tested Rails 4.2 and Ruby 2.2.3

Usage

At first, you include 2 moudles.

class User < ActiveRecord::Base
  include BrowsingHistory::Browser
end

class Article < ActiveRecord::Base
  include BrowsingHistory::Historizable
end

Then, you can access browsing_histories method on a user instance to add a article to the user's browsing history.

# create a article
article = Article.new(title: 'welcome to browsing_history')

# add the article to user's browsing history
user.browsing_histories << article

# access user's browsing histories of Article class
user.browsing_histories.recent(Article)
# => [#<Article id: 1, title: "welcome to browsing_history", created_at: "2016-04-29 10:13:15", updated_at: "2016-04-29 10:13:15">]

For example,
if you want add a articls to browsing histoy when a user watch the article page, you should add the method on article_controller#show.

# app/controllers/articles_controller
# GET /articles/:id
def show
  @user.browsing_histories << @article
end

How it works

  • recording historizable instances to browser's history on redis
  • getting past historizable instances on browser's browsing history with like activerecord options(limit, between, at)
  • clering browser's browsing history with like activerecord options(limit, between, at)
  • recording above history on some kind of storages(redis, activerecord, mongoid...) by same interface and user creat own interface a storage.

Description Elements

There are 4 elements working on browsing_history.

  • BrowsingHistory::Browser(Module)
    It provide access methods to browsing_history for a browser class included it.
    example: User, Crawler

  • BrowsingHistory::Historizable(Module)
    The class included it can be recorded on browsing_history
    example: Article, Review

  • BrowsingHistory::History(class)
    It's class based on ActiveModel. It record historizable instance on browser's browsing_history and exec some operations for them like add, where, count, clear.

  • BrowsingHistory::Storage(Module)
    It provide abstruct interface of some kind of storages like redis, activerecord, mongoid. It is included BrowsingHistory::History and allow it to access above storages by same methods.

Storages

There is 1 storage interface

  • BrowsingHistory::Storages::Redis
    It's interface of redis for BrowsingHistory::Storage. it depends on redis, redis-objects, redis-namespace.

Getting Started

1. install gem

add a follow sentence to Gemfile

# Gemfile
gem 'browsing_history'

2. Configure

if you want choose storage to save history or modify namespace which is key-prefix on redis, add configuration to environments or your own config file. but now, you can use :redis only as storage.

# config/environments/{your environment} or create config/initializers/browsing_history.rb

# It's default configuration
BrowsingHistory.configure do |config|
  config.storage_types = %i(redis active_record)
  config.storage_type  = :redis
  config.namespace     = 'browsing_history'
end

3. Include modules

you should include only 2 moudles.

# example models
class User < ActiveRecord::Base
  include BrowsingHistory::Browser
end

class Article < ActiveRecord::Base
  include BrowsingHistory::Historizable
end

4. Access browsing history

BrowsingHistory::Browser#browsing_histories

you can access to browsing_history by browsing_histories method. this method is provided BrowsingHistory::Browser and return BrowsingHistory::Browser::Assosiation

BrowsingHistory::Browser::Assosiation is provided following 4 methods.

  1. recent
  2. previous
  3. add
  4. count

and you can give following 2 argumetns for all of the methods.

  • Target class included historizable or their instance you want to get from history like Article, article, Review, review,w
# when browser is user, historizable is Articles
user     = User.first
articles = user.browsing_histories.recent(Article) 

# you can give instance instead of class, both return same result.
user     = User.first
article  = Article.first
articles = user.browsing_histories.recent(article) 

# when you want mautipul historizable such as article and review
user     = User.first
articles = user.browsing_histories.recent(article) 
reviews  = user.browsing_histories.recent(Review) 
  • Range option like activerecord for instance limit, between, at, all....
# when you want to get recent 20(default) articles on browsing_history
articles = user.browsing_histories.recent(Article) 

# when you want to get recent 100 articles on browsing_history
articles_100 = user.browsing_histories.recent(Article, limit: 100) 
#### BrowsingHistory::Browser::Assosiation methods
# recent
# get user's browsing histories in order form the new one
articles     = user.browsing_histories.recent(Article) 
articles_100 = user.browsing_histories.recent(Article, limit: 100) 

# previous
# getting articles that user browsed between today to 1 day ago and in order form the new one
articles = user.browsing_histories.previous(Article, Time.zone.now..1.day.ago)

# add
# add the article to user's browsing history
user.browsing_histories << article

# count

# count user's browsing history
number_of_histories = user.browsing_histories.count(Article) 
BrowsingHistory::History

you can access browsing_history using also BrowsingHistory::History not only BrowsingHistory::Browser::Assosiation

BrowsingHistory::History is provided following 4 methods.

  1. create
  2. create!
  3. where
  4. clear
  5. count

it's based ActiveModel and its method format is similar ActiveRecord.

user    = User.create(name: 'browser')
article = Article.create(title: 'welcome to browsing_history')

# add the article to user's browsing_history
BrowsingHistories::History.create(browser: user, historizable: article)

it is able to conbine some options to access browsing_history so more flexibly than BrowsingHistory::Browser::Assosiation.

BrowsingHistories::History.where(
  user: user, 
  historizable: review,
  between: 1.day.ago..2.days.ago,
  limit: 50
)
BrowsingHistory::History methods
user = User.first

# where
# get user's browsing histories in order form the new one ≒ user.browsing_history.recent
articls     = BrowsingHistories::History.where(user: user, historizable: Article) # default all
articls_100 = BrowsingHistories::History.where(user: user, historizable: Article, limit: 100)

# get the article 5th from the newest
article     = Article.find(100)
articls_5th = BrowsingHistories::History.where(user: user, historizable: Article, at: 5)

# getting articles that user browsed between 1 day ago to 2 days ago and in order form the new one ≒ user.browsing_history.previous
articles = BrowsingHistories::History.where(user: user, historizable: review, between: 1.day.ago..2.days.ago)

# conbining some options
# getting articles that user browsed between 1 day ago to 2 days ago and it's limit 50 in order from the new one
BrowsingHistories::History.where(
  user: user, 
  historizable: review,
  between: 1.day.ago..2.days.ago,
  limit: 50
)

# count
# count user's browsing history ≒ user.browsing_history.count
articls = BrowsingHistories::History.count(user: user, historizable: Article)
number_of_histories = BrowsingHistories::History.count(user: user, historizable: Article) 

# clear
# clear the article from user's browsing history
article = Article.first
BrowsingHistory::History.clear(browseer: user, historizable: article)

# clear all user's browsing history
BrowsingHistory::History.clear(browseer: user, historizable: Article)
# or
article = Article.first
BrowsingHistory::History.clear(browseer: user, historizable: article, all: true)

Advanced

FAQs

Package last updated on 30 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc