├── .gitignore
├── .rspec
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
├── assets
│ ├── images
│ │ └── .keep
│ ├── javascripts
│ │ └── application.js
│ └── stylesheets
│ │ └── application.css
├── controllers
│ ├── api
│ │ ├── products_controller.rb
│ │ ├── sessions_controller.rb
│ │ └── users_controller.rb
│ ├── application_controller.rb
│ └── concerns
│ │ └── .keep
├── decorators
│ ├── product_decorator.rb
│ └── user_decorator.rb
├── helpers
│ └── application_helper.rb
├── mailers
│ └── .keep
├── models
│ ├── .keep
│ ├── auth_token.rb
│ ├── concerns
│ │ └── .keep
│ ├── product.rb
│ └── user.rb
└── views
│ ├── application
│ ├── create.json.erb
│ ├── errors.json.erb
│ ├── exception.json.erb
│ ├── index.json.erb
│ └── show.json.erb
│ └── layouts
│ └── application.html.erb
├── bin
├── bundle
├── rails
├── rake
└── setup
├── config.ru
├── config
├── application.rb
├── boot.rb
├── database.yml
├── environment.rb
├── environments
│ ├── development.rb
│ ├── production.rb
│ └── test.rb
├── initializers
│ ├── backtrace_silencers.rb
│ ├── cookies_serializer.rb
│ ├── filter_parameter_logging.rb
│ ├── generators.rb
│ ├── inflections.rb
│ ├── mime_types.rb
│ ├── session_store.rb
│ └── wrap_parameters.rb
├── locales
│ └── en.yml
├── routes.rb
└── secrets.yml
├── db
├── migrate
│ ├── 20150925082536_create_products.rb
│ ├── 20151204094329_create_users.rb
│ └── 20151204132543_create_auth_tokens.rb
├── schema.rb
└── seeds.rb
├── lib
├── assets
│ └── .keep
├── session.rb
└── tasks
│ └── .keep
├── log
└── .keep
├── mlsdev-logo.png
├── public
├── 404.html
├── 422.html
├── 500.html
├── favicon.ico
└── robots.txt
├── spec
├── controllers
│ ├── api
│ │ ├── products_controller_spec.rb
│ │ ├── sessions_controller_spec.rb
│ │ └── users_controller_spec.rb
│ └── application_controller_spec.rb
├── decorators
│ ├── product_decorator_spec.rb
│ └── user_decorator_spec.rb
├── lib
│ └── session_spec.rb
├── models
│ ├── auth_token_spec.rb
│ ├── product_spec.rb
│ └── user_spec.rb
├── rails_helper.rb
└── spec_helper.rb
└── vendor
└── assets
├── javascripts
└── .keep
└── stylesheets
└── .keep
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2 | #
3 | # If you find yourself ignoring temporary files generated by your text editor
4 | # or operating system, you probably want to add a global ignore instead:
5 | # git config --global core.excludesfile '~/.gitignore_global'
6 |
7 | # Ignore bundler config.
8 | /.bundle
9 |
10 | # Ignore all logfiles and tempfiles.
11 | /log/*
12 | !/log/.keep
13 | /tmp
14 |
--------------------------------------------------------------------------------
/.rspec:
--------------------------------------------------------------------------------
1 | --color
2 | --require spec_helper
3 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gem 'rails'
4 | gem 'pg'
5 | gem 'jquery-rails'
6 | gem 'bcrypt'
7 | gem 'draper'
8 | gem 'kaminari'
9 | gem 'pg_search'
10 | gem 'email_validator'
11 |
12 | group :development, :test do
13 | gem 'rspec-rails'
14 | gem 'faker'
15 | end
16 |
17 | group :test do
18 | gem 'shoulda-matchers'
19 | gem 'rspec-activemodel-mocks'
20 | gem 'rspec-its'
21 | end
22 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | GEM
2 | remote: https://rubygems.org/
3 | specs:
4 | actionmailer (4.2.5)
5 | actionpack (= 4.2.5)
6 | actionview (= 4.2.5)
7 | activejob (= 4.2.5)
8 | mail (~> 2.5, >= 2.5.4)
9 | rails-dom-testing (~> 1.0, >= 1.0.5)
10 | actionpack (4.2.5)
11 | actionview (= 4.2.5)
12 | activesupport (= 4.2.5)
13 | rack (~> 1.6)
14 | rack-test (~> 0.6.2)
15 | rails-dom-testing (~> 1.0, >= 1.0.5)
16 | rails-html-sanitizer (~> 1.0, >= 1.0.2)
17 | actionview (4.2.5)
18 | activesupport (= 4.2.5)
19 | builder (~> 3.1)
20 | erubis (~> 2.7.0)
21 | rails-dom-testing (~> 1.0, >= 1.0.5)
22 | rails-html-sanitizer (~> 1.0, >= 1.0.2)
23 | activejob (4.2.5)
24 | activesupport (= 4.2.5)
25 | globalid (>= 0.3.0)
26 | activemodel (4.2.5)
27 | activesupport (= 4.2.5)
28 | builder (~> 3.1)
29 | activerecord (4.2.5)
30 | activemodel (= 4.2.5)
31 | activesupport (= 4.2.5)
32 | arel (~> 6.0)
33 | activesupport (4.2.5)
34 | i18n (~> 0.7)
35 | json (~> 1.7, >= 1.7.7)
36 | minitest (~> 5.1)
37 | thread_safe (~> 0.3, >= 0.3.4)
38 | tzinfo (~> 1.1)
39 | arel (6.0.3)
40 | bcrypt (3.1.10)
41 | builder (3.2.2)
42 | diff-lcs (1.2.5)
43 | draper (2.1.0)
44 | actionpack (>= 3.0)
45 | activemodel (>= 3.0)
46 | activesupport (>= 3.0)
47 | request_store (~> 1.0)
48 | email_validator (1.6.0)
49 | activemodel
50 | erubis (2.7.0)
51 | faker (1.5.0)
52 | i18n (~> 0.5)
53 | globalid (0.3.6)
54 | activesupport (>= 4.1.0)
55 | i18n (0.7.0)
56 | jquery-rails (4.0.5)
57 | rails-dom-testing (~> 1.0)
58 | railties (>= 4.2.0)
59 | thor (>= 0.14, < 2.0)
60 | json (1.8.3)
61 | kaminari (0.16.3)
62 | actionpack (>= 3.0.0)
63 | activesupport (>= 3.0.0)
64 | loofah (2.0.3)
65 | nokogiri (>= 1.5.9)
66 | mail (2.6.3)
67 | mime-types (>= 1.16, < 3)
68 | mime-types (2.99)
69 | mini_portile (0.6.2)
70 | minitest (5.8.3)
71 | nokogiri (1.6.6.4)
72 | mini_portile (~> 0.6.0)
73 | pg (0.18.4)
74 | pg_search (1.0.5)
75 | activerecord (>= 3.1)
76 | activesupport (>= 3.1)
77 | arel
78 | rack (1.6.4)
79 | rack-test (0.6.3)
80 | rack (>= 1.0)
81 | rails (4.2.5)
82 | actionmailer (= 4.2.5)
83 | actionpack (= 4.2.5)
84 | actionview (= 4.2.5)
85 | activejob (= 4.2.5)
86 | activemodel (= 4.2.5)
87 | activerecord (= 4.2.5)
88 | activesupport (= 4.2.5)
89 | bundler (>= 1.3.0, < 2.0)
90 | railties (= 4.2.5)
91 | sprockets-rails
92 | rails-deprecated_sanitizer (1.0.3)
93 | activesupport (>= 4.2.0.alpha)
94 | rails-dom-testing (1.0.7)
95 | activesupport (>= 4.2.0.beta, < 5.0)
96 | nokogiri (~> 1.6.0)
97 | rails-deprecated_sanitizer (>= 1.0.1)
98 | rails-html-sanitizer (1.0.2)
99 | loofah (~> 2.0)
100 | railties (4.2.5)
101 | actionpack (= 4.2.5)
102 | activesupport (= 4.2.5)
103 | rake (>= 0.8.7)
104 | thor (>= 0.18.1, < 2.0)
105 | rake (10.4.2)
106 | request_store (1.2.1)
107 | rspec-activemodel-mocks (1.0.2)
108 | activemodel (>= 3.0)
109 | activesupport (>= 3.0)
110 | rspec-mocks (>= 2.99, < 4.0)
111 | rspec-core (3.4.1)
112 | rspec-support (~> 3.4.0)
113 | rspec-expectations (3.4.0)
114 | diff-lcs (>= 1.2.0, < 2.0)
115 | rspec-support (~> 3.4.0)
116 | rspec-its (1.2.0)
117 | rspec-core (>= 3.0.0)
118 | rspec-expectations (>= 3.0.0)
119 | rspec-mocks (3.4.0)
120 | diff-lcs (>= 1.2.0, < 2.0)
121 | rspec-support (~> 3.4.0)
122 | rspec-rails (3.4.0)
123 | actionpack (>= 3.0, < 4.3)
124 | activesupport (>= 3.0, < 4.3)
125 | railties (>= 3.0, < 4.3)
126 | rspec-core (~> 3.4.0)
127 | rspec-expectations (~> 3.4.0)
128 | rspec-mocks (~> 3.4.0)
129 | rspec-support (~> 3.4.0)
130 | rspec-support (3.4.1)
131 | shoulda-matchers (3.0.1)
132 | activesupport (>= 4.0.0)
133 | sprockets (3.4.0)
134 | rack (> 1, < 3)
135 | sprockets-rails (2.3.3)
136 | actionpack (>= 3.0)
137 | activesupport (>= 3.0)
138 | sprockets (>= 2.8, < 4.0)
139 | thor (0.19.1)
140 | thread_safe (0.3.5)
141 | tzinfo (1.2.2)
142 | thread_safe (~> 0.1)
143 |
144 | PLATFORMS
145 | ruby
146 |
147 | DEPENDENCIES
148 | bcrypt
149 | draper
150 | email_validator
151 | faker
152 | jquery-rails
153 | kaminari
154 | pg
155 | pg_search
156 | rails
157 | rspec-activemodel-mocks
158 | rspec-its
159 | rspec-rails
160 | shoulda-matchers
161 |
162 | BUNDLED WITH
163 | 1.10.6
164 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # E-commerce API
2 |
3 | This is an E-commerce website API project done with Ruby on Rails. It is aimed at beginners and will be supplemented each time a new tutorial part appears on our blog.
4 |
5 | The tutorial consists of the following parts so far:
6 |
7 | 1. [Creating products][part 1]
8 | 2. [Product search with Full Text Search in PostgreSQL][part 2]
9 | 3. [Registration and Authorization][part 3]
10 | 4. To be continued...
11 |
12 | ----
13 |
14 | ### Technologies
15 |
16 | * Ruby >= 2.2.3;
17 | * Ruby on Rails >= 4.2.4;
18 | * PostgreSQL;
19 |
20 | ### Development Environment Setup
21 |
22 | * Mac OS X
23 | * Install RVM + ruby;
24 | * brew install postgresql;
25 | * Set up config/secrets.yml, config/database.yml;
26 | * rake db:create;
27 | * rake db:migrate;
28 | * rails server;
29 |
30 | ## License
31 |
32 | This work is licensed under a MIT License.
33 |
34 | ## About MLSDev
35 |
36 | [
][mlsdev]
37 |
38 | E-commerce API are maintained by MLSDev, Inc. We specialize in providing all-in-one solution in mobile and web development. Our team follows Lean principles and works according to agile methodologies to deliver the best results reducing the budget for development and its timeline.
39 |
40 | Find out more [here][mlsdev] and don't hesitate to [contact us][contact]!
41 |
42 | [mlsdev]: http://mlsdev.com
43 | [contact]: http://mlsdev.com/contact_us
44 | [part 1]: http://mlsdev.com/blog/42
45 | [part 2]: http://mlsdev.com/blog/43
46 | [part 3]: http://mlsdev.com/blog/44
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | # Add your own tasks in files placed in lib/tasks ending in .rake,
2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3 |
4 | require File.expand_path('../config/application', __FILE__)
5 |
6 | Rails.application.load_tasks
7 |
--------------------------------------------------------------------------------
/app/assets/images/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/app/assets/images/.keep
--------------------------------------------------------------------------------
/app/assets/javascripts/application.js:
--------------------------------------------------------------------------------
1 | // This is a manifest file that'll be compiled into application.js, which will include all the files
2 | // listed below.
3 | //
4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5 | // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6 | //
7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8 | // compiled file.
9 | //
10 | // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11 | // about supported directives.
12 | //
13 | //= require jquery
14 | //= require jquery_ujs
15 | //= require_tree .
16 |
--------------------------------------------------------------------------------
/app/assets/stylesheets/application.css:
--------------------------------------------------------------------------------
1 | /*
2 | * This is a manifest file that'll be compiled into application.css, which will include all the files
3 | * listed below.
4 | *
5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6 | * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7 | *
8 | * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9 | * compiled file so the styles you add here take precedence over styles defined in any styles
10 | * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11 | * file per style scope.
12 | *
13 | *= require_tree .
14 | *= require_self
15 | */
16 |
--------------------------------------------------------------------------------
/app/controllers/api/products_controller.rb:
--------------------------------------------------------------------------------
1 | class Api::ProductsController < ApplicationController
2 | skip_before_action :authenticate
3 |
4 | private
5 | def collection
6 | @products ||= Product.search_by(params)
7 | end
8 |
9 | def resource
10 | @product ||= Product.find params[:id]
11 | end
12 | end
--------------------------------------------------------------------------------
/app/controllers/api/sessions_controller.rb:
--------------------------------------------------------------------------------
1 | class Api::SessionsController < ApplicationController
2 | skip_before_action :authenticate, only: [:create]
3 |
4 | private
5 | def build_resource
6 | @session = Session.new resource_params
7 | end
8 |
9 | def resource
10 | @session ||= Session.new user: current_user
11 | end
12 |
13 | def resource_params
14 | params.require(:session).permit(:email, :password)
15 | end
16 | end
--------------------------------------------------------------------------------
/app/controllers/api/users_controller.rb:
--------------------------------------------------------------------------------
1 | class Api::UsersController < ApplicationController
2 | skip_before_action :authenticate, only: [:create]
3 |
4 | private
5 | def build_resource
6 | @user = User.new resource_params
7 | end
8 |
9 | def resource
10 | @user
11 | end
12 |
13 | def resource_params
14 | params.require(:user).permit(:name, :email, :password, :password_confirmation)
15 | end
16 | end
--------------------------------------------------------------------------------
/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | # Prevent CSRF attacks by raising an exception.
3 | # For APIs, you may want to use :null_session instead.
4 | protect_from_forgery with: :exception
5 |
6 | helper_method :resource, :collection
7 |
8 | skip_before_action :verify_authenticity_token, if: :json_request?
9 |
10 | before_action :authenticate
11 |
12 | attr_reader :current_user
13 |
14 | rescue_from ActiveRecord::RecordNotFound do |exception|
15 | @exception = exception
16 |
17 | render :exception
18 | end
19 |
20 | rescue_from ActiveRecord::RecordInvalid, ActiveModel::StrictValidationFailed do
21 | render :errors, status: :unprocessable_entity
22 | end
23 |
24 | def create
25 | build_resource
26 |
27 | resource.save!
28 | end
29 |
30 | def update
31 | resource.update! resource_params
32 | end
33 |
34 | def destroy
35 | resource.destroy!
36 |
37 | head :ok
38 | end
39 |
40 | private
41 | def authenticate
42 | authenticate_or_request_with_http_token do |token, options|
43 | @current_user = User.joins(:auth_token).find_by(auth_tokens: { value: token })
44 | end
45 | end
46 |
47 | def json_request?
48 | request.format.json?
49 | end
50 | end
51 |
--------------------------------------------------------------------------------
/app/controllers/concerns/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/app/controllers/concerns/.keep
--------------------------------------------------------------------------------
/app/decorators/product_decorator.rb:
--------------------------------------------------------------------------------
1 | class ProductDecorator < Draper::Decorator
2 | delegate_all
3 |
4 | def as_json *args
5 | {
6 | id: id,
7 | name: name,
8 | description: description,
9 | price: price
10 | }
11 | end
12 | end
--------------------------------------------------------------------------------
/app/decorators/user_decorator.rb:
--------------------------------------------------------------------------------
1 | class UserDecorator < Draper::Decorator
2 | delegate_all
3 |
4 | def as_json *args
5 | {
6 | name: name,
7 | email: email
8 | }
9 | end
10 | end
--------------------------------------------------------------------------------
/app/helpers/application_helper.rb:
--------------------------------------------------------------------------------
1 | module ApplicationHelper
2 | end
3 |
--------------------------------------------------------------------------------
/app/mailers/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/app/mailers/.keep
--------------------------------------------------------------------------------
/app/models/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/app/models/.keep
--------------------------------------------------------------------------------
/app/models/auth_token.rb:
--------------------------------------------------------------------------------
1 | class AuthToken < ActiveRecord::Base
2 | belongs_to :user
3 |
4 | validates :value, presence: true
5 | end
--------------------------------------------------------------------------------
/app/models/concerns/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/app/models/concerns/.keep
--------------------------------------------------------------------------------
/app/models/product.rb:
--------------------------------------------------------------------------------
1 | class Product < ActiveRecord::Base
2 | include PgSearch
3 |
4 | pg_search_scope :search,
5 | against: {
6 | name: :A,
7 | description: :B
8 | },
9 | using: {
10 | tsearch: { dictionary: :english }
11 | }
12 |
13 | class << self
14 | def search_by params = {}
15 | params = params.try(:symbolize_keys) || {}
16 |
17 | collection = page(params[:page])
18 |
19 | if params[:term].present?
20 | collection = collection.where('name ILIKE ?', "#{ params[:term] }%")
21 | end
22 |
23 | if params[:name].present?
24 | collection = collection.search(params[:name])
25 | end
26 |
27 | collection
28 | end
29 | end
30 | end
31 |
--------------------------------------------------------------------------------
/app/models/user.rb:
--------------------------------------------------------------------------------
1 | class User < ActiveRecord::Base
2 | has_secure_password
3 |
4 | validates :name, presence: true
5 |
6 | validates :email, presence: true, uniqueness: true, email: true
7 |
8 | has_one :auth_token, dependent: :destroy
9 | end
10 |
--------------------------------------------------------------------------------
/app/views/application/create.json.erb:
--------------------------------------------------------------------------------
1 | <%= sanitize resource.decorate.to_json %>
--------------------------------------------------------------------------------
/app/views/application/errors.json.erb:
--------------------------------------------------------------------------------
1 | <%= sanitize({ errors: resource.errors }.to_json) %>
2 |
--------------------------------------------------------------------------------
/app/views/application/exception.json.erb:
--------------------------------------------------------------------------------
1 | <%= sanitize({ errors: { @exception.class.name => [@exception.to_s] } }.to_json) %>
2 |
--------------------------------------------------------------------------------
/app/views/application/index.json.erb:
--------------------------------------------------------------------------------
1 | <%= sanitize collection.decorate.to_json %>
--------------------------------------------------------------------------------
/app/views/application/show.json.erb:
--------------------------------------------------------------------------------
1 | <%= sanitize resource.decorate.to_json %>
--------------------------------------------------------------------------------
/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Shop
5 | <%= stylesheet_link_tag 'application', media: 'all' %>
6 | <%= javascript_include_tag 'application' %>
7 | <%= csrf_meta_tags %>
8 |
9 |
10 |
11 | <%= yield %>
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/bin/bundle:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3 | load Gem.bin_path('bundler', 'bundle')
4 |
--------------------------------------------------------------------------------
/bin/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | APP_PATH = File.expand_path('../../config/application', __FILE__)
3 | require_relative '../config/boot'
4 | require 'rails/commands'
5 |
--------------------------------------------------------------------------------
/bin/rake:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | require_relative '../config/boot'
3 | require 'rake'
4 | Rake.application.run
5 |
--------------------------------------------------------------------------------
/bin/setup:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | require 'pathname'
3 |
4 | # path to your application root.
5 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6 |
7 | Dir.chdir APP_ROOT do
8 | # This script is a starting point to setup your application.
9 | # Add necessary setup steps to this file:
10 |
11 | puts "== Installing dependencies =="
12 | system "gem install bundler --conservative"
13 | system "bundle check || bundle install"
14 |
15 | # puts "\n== Copying sample files =="
16 | # unless File.exist?("config/database.yml")
17 | # system "cp config/database.yml.sample config/database.yml"
18 | # end
19 |
20 | puts "\n== Preparing database =="
21 | system "bin/rake db:setup"
22 |
23 | puts "\n== Removing old logs and tempfiles =="
24 | system "rm -f log/*"
25 | system "rm -rf tmp/cache"
26 |
27 | puts "\n== Restarting application server =="
28 | system "touch tmp/restart.txt"
29 | end
30 |
--------------------------------------------------------------------------------
/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Rails.application
5 |
--------------------------------------------------------------------------------
/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | require "rails"
4 | # Pick the frameworks you want:
5 | require "active_model/railtie"
6 | require "active_job/railtie"
7 | require "active_record/railtie"
8 | require "action_controller/railtie"
9 | require "action_mailer/railtie"
10 | require "action_view/railtie"
11 | # require "sprockets/railtie"
12 | # require "rails/test_unit/railtie"
13 |
14 | # Require the gems listed in Gemfile, including any gems
15 | # you've limited to :test, :development, or :production.
16 | Bundler.require(*Rails.groups)
17 |
18 | module Shop
19 | class Application < Rails::Application
20 | # Settings in config/environments/* take precedence over those specified here.
21 | # Application configuration should go into files in config/initializers
22 | # -- all .rb files in that directory are automatically loaded.
23 |
24 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26 | # config.time_zone = 'Central Time (US & Canada)'
27 |
28 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30 | # config.i18n.default_locale = :de
31 |
32 | # Do not swallow errors in after_commit/after_rollback callbacks.
33 | config.active_record.raise_in_transactional_callbacks = true
34 |
35 | config.eager_load_paths << config.root.join('lib').to_s
36 | end
37 | end
38 |
--------------------------------------------------------------------------------
/config/boot.rb:
--------------------------------------------------------------------------------
1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
2 |
3 | require 'bundler/setup' # Set up gems listed in the Gemfile.
4 |
--------------------------------------------------------------------------------
/config/database.yml:
--------------------------------------------------------------------------------
1 | # PostgreSQL. Versions 8.2 and up are supported.
2 | #
3 | # Install the pg driver:
4 | # gem install pg
5 | # On OS X with Homebrew:
6 | # gem install pg -- --with-pg-config=/usr/local/bin/pg_config
7 | # On OS X with MacPorts:
8 | # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
9 | # On Windows:
10 | # gem install pg
11 | # Choose the win32 build.
12 | # Install PostgreSQL and put its /bin directory on your path.
13 | #
14 | # Configure Using Gemfile
15 | # gem 'pg'
16 | #
17 | default: &default
18 | adapter: postgresql
19 | encoding: unicode
20 | # For details on connection pooling, see rails configuration guide
21 | # http://guides.rubyonrails.org/configuring.html#database-pooling
22 | pool: 5
23 |
24 | development:
25 | <<: *default
26 | database: Shop_development
27 |
28 | # The specified database role being used to connect to postgres.
29 | # To create additional roles in postgres see `$ createuser --help`.
30 | # When left blank, postgres will use the default role. This is
31 | # the same name as the operating system user that initialized the database.
32 | #username: Shop
33 |
34 | # The password associated with the postgres role (username).
35 | #password:
36 |
37 | # Connect on a TCP socket. Omitted by default since the client uses a
38 | # domain socket that doesn't need configuration. Windows does not have
39 | # domain sockets, so uncomment these lines.
40 | #host: localhost
41 |
42 | # The TCP port the server listens on. Defaults to 5432.
43 | # If your server runs on a different port number, change accordingly.
44 | #port: 5432
45 |
46 | # Schema search path. The server defaults to $user,public
47 | #schema_search_path: myapp,sharedapp,public
48 |
49 | # Minimum log levels, in increasing order:
50 | # debug5, debug4, debug3, debug2, debug1,
51 | # log, notice, warning, error, fatal, and panic
52 | # Defaults to warning.
53 | #min_messages: notice
54 |
55 | # Warning: The database defined as "test" will be erased and
56 | # re-generated from your development database when you run "rake".
57 | # Do not set this db to the same as development or production.
58 | test:
59 | <<: *default
60 | database: Shop_test
61 |
62 | # As with config/secrets.yml, you never want to store sensitive information,
63 | # like your database password, in your source code. If your source code is
64 | # ever seen by anyone, they now have access to your database.
65 | #
66 | # Instead, provide the password as a unix environment variable when you boot
67 | # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
68 | # for a full rundown on how to provide these environment variables in a
69 | # production deployment.
70 | #
71 | # On Heroku and other platform providers, you may have a full connection URL
72 | # available as an environment variable. For example:
73 | #
74 | # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
75 | #
76 | # You can use this database configuration with:
77 | #
78 | # production:
79 | # url: <%= ENV['DATABASE_URL'] %>
80 | #
81 | production:
82 | <<: *default
83 | database: Shop_production
84 | username: Shop
85 | password: <%= ENV['SHOP_DATABASE_PASSWORD'] %>
86 |
--------------------------------------------------------------------------------
/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the Rails application.
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the Rails application.
5 | Rails.application.initialize!
6 |
--------------------------------------------------------------------------------
/config/environments/development.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # In the development environment your application's code is reloaded on
5 | # every request. This slows down response time but is perfect for development
6 | # since you don't have to restart the web server when you make code changes.
7 | config.cache_classes = false
8 |
9 | # Do not eager load code on boot.
10 | config.eager_load = false
11 |
12 | # Show full error reports and disable caching.
13 | config.consider_all_requests_local = true
14 | config.action_controller.perform_caching = false
15 |
16 | # Don't care if the mailer can't send.
17 | config.action_mailer.raise_delivery_errors = false
18 |
19 | # Print deprecation notices to the Rails logger.
20 | config.active_support.deprecation = :log
21 |
22 | # Raise an error on page load if there are pending migrations.
23 | config.active_record.migration_error = :page_load
24 |
25 |
26 | # Raises error for missing translations
27 | # config.action_view.raise_on_missing_translations = true
28 | end
29 |
--------------------------------------------------------------------------------
/config/environments/production.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # Code is not reloaded between requests.
5 | config.cache_classes = true
6 |
7 | # Eager load code on boot. This eager loads most of Rails and
8 | # your application in memory, allowing both threaded web servers
9 | # and those relying on copy on write to perform better.
10 | # Rake tasks automatically ignore this option for performance.
11 | config.eager_load = true
12 |
13 | # Full error reports are disabled and caching is turned on.
14 | config.consider_all_requests_local = false
15 | config.action_controller.perform_caching = true
16 |
17 | # Enable Rack::Cache to put a simple HTTP cache in front of your application
18 | # Add `rack-cache` to your Gemfile before enabling this.
19 | # For large-scale production use, consider using a caching reverse proxy like
20 | # NGINX, varnish or squid.
21 | # config.action_dispatch.rack_cache = true
22 |
23 | # Disable serving static files from the `/public` folder by default since
24 | # Apache or NGINX already handles this.
25 | config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
26 |
27 |
28 | # Specifies the header that your server uses for sending files.
29 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
30 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
31 |
32 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
33 | # config.force_ssl = true
34 |
35 | # Use the lowest log level to ensure availability of diagnostic information
36 | # when problems arise.
37 | config.log_level = :debug
38 |
39 | # Prepend all log lines with the following tags.
40 | # config.log_tags = [ :subdomain, :uuid ]
41 |
42 | # Use a different logger for distributed setups.
43 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
44 |
45 | # Use a different cache store in production.
46 | # config.cache_store = :mem_cache_store
47 |
48 | # Enable serving of images, stylesheets, and JavaScripts from an asset server.
49 | # config.action_controller.asset_host = 'http://assets.example.com'
50 |
51 | # Ignore bad email addresses and do not raise email delivery errors.
52 | # Set this to true and configure the email server for immediate delivery to raise delivery errors.
53 | # config.action_mailer.raise_delivery_errors = false
54 |
55 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
56 | # the I18n.default_locale when a translation cannot be found).
57 | config.i18n.fallbacks = true
58 |
59 | # Send deprecation notices to registered listeners.
60 | config.active_support.deprecation = :notify
61 |
62 | # Use default logging formatter so that PID and timestamp are not suppressed.
63 | config.log_formatter = ::Logger::Formatter.new
64 |
65 | # Do not dump schema after migrations.
66 | config.active_record.dump_schema_after_migration = false
67 | end
68 |
--------------------------------------------------------------------------------
/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 |
10 | # Do not eager load code on boot. This avoids loading your whole application
11 | # just for the purpose of running a single test. If you are using a tool that
12 | # preloads Rails for running tests, you may have to set it to true.
13 | config.eager_load = false
14 |
15 | # Configure static file server for tests with Cache-Control for performance.
16 | config.serve_static_files = true
17 | config.static_cache_control = 'public, max-age=3600'
18 |
19 | # Show full error reports and disable caching.
20 | config.consider_all_requests_local = true
21 | config.action_controller.perform_caching = false
22 |
23 | # Raise exceptions instead of rendering exception templates.
24 | config.action_dispatch.show_exceptions = false
25 |
26 | # Disable request forgery protection in test environment.
27 | config.action_controller.allow_forgery_protection = false
28 |
29 | # Tell Action Mailer not to deliver emails to the real world.
30 | # The :test delivery method accumulates sent emails in the
31 | # ActionMailer::Base.deliveries array.
32 | config.action_mailer.delivery_method = :test
33 |
34 | # Randomize the order test cases are executed.
35 | config.active_support.test_order = :random
36 |
37 | # Print deprecation notices to the stderr.
38 | config.active_support.deprecation = :stderr
39 |
40 | # Raises error for missing translations
41 | # config.action_view.raise_on_missing_translations = true
42 | end
43 |
--------------------------------------------------------------------------------
/config/initializers/backtrace_silencers.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5 |
6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7 | # Rails.backtrace_cleaner.remove_silencers!
8 |
--------------------------------------------------------------------------------
/config/initializers/cookies_serializer.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Rails.application.config.action_dispatch.cookies_serializer = :json
4 |
--------------------------------------------------------------------------------
/config/initializers/filter_parameter_logging.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Configure sensitive parameters which will be filtered from the log file.
4 | Rails.application.config.filter_parameters += [:password]
5 |
--------------------------------------------------------------------------------
/config/initializers/generators.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | config.generators do |g|
3 | g.orm :active_record, :migration => true
4 | g.test_framework :rspec, :fixtures => false
5 | g.helper false
6 | g.decorator false
7 | g.controller :assets => false
8 | end
9 | end
--------------------------------------------------------------------------------
/config/initializers/inflections.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new inflection rules using the following format. Inflections
4 | # are locale specific, and you may define rules for as many different
5 | # locales as you wish. All of these examples are active by default:
6 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
7 | # inflect.plural /^(ox)$/i, '\1en'
8 | # inflect.singular /^(ox)en/i, '\1'
9 | # inflect.irregular 'person', 'people'
10 | # inflect.uncountable %w( fish sheep )
11 | # end
12 |
13 | # These inflection rules are supported but not enabled by default:
14 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
15 | # inflect.acronym 'RESTful'
16 | # end
17 |
--------------------------------------------------------------------------------
/config/initializers/mime_types.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new mime types for use in respond_to blocks:
4 | # Mime::Type.register "text/richtext", :rtf
5 |
--------------------------------------------------------------------------------
/config/initializers/session_store.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Rails.application.config.session_store :cookie_store, key: '_Shop_session'
4 |
--------------------------------------------------------------------------------
/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActiveSupport.on_load(:action_controller) do
8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9 | end
10 |
11 | # To enable root element in JSON for ActiveRecord objects.
12 | # ActiveSupport.on_load(:active_record) do
13 | # self.include_root_in_json = true
14 | # end
15 |
--------------------------------------------------------------------------------
/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Files in the config/locales directory are used for internationalization
2 | # and are automatically loaded by Rails. If you want to use locales other
3 | # than English, add the necessary files in this directory.
4 | #
5 | # To use the locales, use `I18n.t`:
6 | #
7 | # I18n.t 'hello'
8 | #
9 | # In views, this is aliased to just `t`:
10 | #
11 | # <%= t('hello') %>
12 | #
13 | # To use a different locale, set it with `I18n.locale`:
14 | #
15 | # I18n.locale = :es
16 | #
17 | # This would use the information in config/locales/es.yml.
18 | #
19 | # To learn more, please read the Rails Internationalization guide
20 | # available at http://guides.rubyonrails.org/i18n.html.
21 |
22 | en:
23 | hello: "Hello world"
24 |
--------------------------------------------------------------------------------
/config/routes.rb:
--------------------------------------------------------------------------------
1 | Rails.application.routes.draw do
2 | namespace :api do
3 | resources :products, only: [:index, :show]
4 |
5 | resource :user, only: [:create]
6 |
7 | resource :session, only: [:create, :destroy]
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/config/secrets.yml:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key is used for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 |
6 | # Make sure the secret is at least 30 characters and all random,
7 | # no regular words or you'll be exposed to dictionary attacks.
8 | # You can use `rake secret` to generate a secure secret key.
9 |
10 | # Make sure the secrets in this file are kept private
11 | # if you're sharing your code publicly.
12 |
13 | development:
14 | secret_key_base: 5fb2bedc569be872bcabd16ce56021a700a1c6b2057e096855e362de5579224aa68ed729974b535796ba9358bc2e93dbf0258d7a98a81f7bb8a6f309a0fa2dc1
15 |
16 | test:
17 | secret_key_base: e4308681274149346203feda805308fee3d531a2fe397876d2221adb5faf8a66057aa2e58eb5edeb3a8ded9f7e6f7fafe013b5481bf92c2a56e3f3d2c78e8cf4
18 |
19 | # Do not keep production secrets in the repository,
20 | # instead read values from the environment.
21 | production:
22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
23 |
--------------------------------------------------------------------------------
/db/migrate/20150925082536_create_products.rb:
--------------------------------------------------------------------------------
1 | class CreateProducts < ActiveRecord::Migration
2 | def change
3 | create_table :products do |t|
4 | t.string :name
5 | t.integer :price
6 | t.text :description
7 |
8 | t.timestamps null: false
9 | end
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/db/migrate/20151204094329_create_users.rb:
--------------------------------------------------------------------------------
1 | class CreateUsers < ActiveRecord::Migration
2 | def change
3 | create_table :users do |t|
4 | t.string :name
5 | t.string :email
6 | t.string :password_digest
7 |
8 | t.timestamps null: false
9 | end
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/db/migrate/20151204132543_create_auth_tokens.rb:
--------------------------------------------------------------------------------
1 | class CreateAuthTokens < ActiveRecord::Migration
2 | def change
3 | create_table :auth_tokens do |t|
4 | t.string :value
5 | t.references :user, index: true, foreign_key: true
6 |
7 | t.timestamps null: false
8 | end
9 | end
10 | end
11 |
--------------------------------------------------------------------------------
/db/schema.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 | # This file is auto-generated from the current state of the database. Instead
3 | # of editing this file, please use the migrations feature of Active Record to
4 | # incrementally modify your database, and then regenerate this schema definition.
5 | #
6 | # Note that this schema.rb definition is the authoritative source for your
7 | # database schema. If you need to create the application database on another
8 | # system, you should be using db:schema:load, not running all the migrations
9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10 | # you'll amass, the slower it'll run and the greater likelihood for issues).
11 | #
12 | # It's strongly recommended that you check this file into your version control system.
13 |
14 | ActiveRecord::Schema.define(version: 20151204132543) do
15 |
16 | # These are extensions that must be enabled in order to support this database
17 | enable_extension "plpgsql"
18 |
19 | create_table "auth_tokens", force: :cascade do |t|
20 | t.string "value"
21 | t.integer "user_id"
22 | t.datetime "created_at", null: false
23 | t.datetime "updated_at", null: false
24 | end
25 |
26 | add_index "auth_tokens", ["user_id"], name: "index_auth_tokens_on_user_id", using: :btree
27 |
28 | create_table "products", force: :cascade do |t|
29 | t.string "name"
30 | t.integer "price"
31 | t.text "description"
32 | t.datetime "created_at", null: false
33 | t.datetime "updated_at", null: false
34 | end
35 |
36 | create_table "users", force: :cascade do |t|
37 | t.string "name"
38 | t.string "email"
39 | t.string "password_digest"
40 | t.datetime "created_at", null: false
41 | t.datetime "updated_at", null: false
42 | end
43 |
44 | add_foreign_key "auth_tokens", "users"
45 | end
46 |
--------------------------------------------------------------------------------
/db/seeds.rb:
--------------------------------------------------------------------------------
1 | 1000.times do
2 | Product.create \
3 | name: Faker::Commerce.product_name,
4 | price: Faker::Number.between(1, 150),
5 | description: Faker::Commerce.department
6 | end
--------------------------------------------------------------------------------
/lib/assets/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/lib/assets/.keep
--------------------------------------------------------------------------------
/lib/session.rb:
--------------------------------------------------------------------------------
1 | class Session
2 | include ActiveModel::Validations
3 |
4 | attr_reader :email, :password
5 |
6 | def initialize params
7 | params = params.try(:symbolize_keys) || {}
8 |
9 | @user = params[:user]
10 |
11 | @email = params[:email]
12 |
13 | @password = params[:password]
14 | end
15 |
16 | validate do |model|
17 | if user
18 | model.errors.add :password, 'is invalid' unless user.authenticate password
19 | else
20 | model.errors.add :email, 'not found'
21 | end
22 | end
23 |
24 | def save!
25 | raise ActiveModel::StrictValidationFailed unless valid?
26 |
27 | user.create_auth_token value: SecureRandom.uuid
28 | end
29 |
30 | def destroy!
31 | user.auth_token.destroy!
32 | end
33 |
34 | def auth_token
35 | user.try(:auth_token).try(:value)
36 | end
37 |
38 | def as_json *args
39 | { auth_token: auth_token }
40 | end
41 |
42 | def decorate
43 | self
44 | end
45 |
46 | private
47 | def user
48 | @user ||= User.find_by email: email
49 | end
50 | end
51 |
--------------------------------------------------------------------------------
/lib/tasks/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/lib/tasks/.keep
--------------------------------------------------------------------------------
/log/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/log/.keep
--------------------------------------------------------------------------------
/mlsdev-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/mlsdev-logo.png
--------------------------------------------------------------------------------
/public/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The page you were looking for doesn't exist (404)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The page you were looking for doesn't exist.
62 |
You may have mistyped the address or the page may have moved.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/public/422.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The change you wanted was rejected (422)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The change you wanted was rejected.
62 |
Maybe you tried to change something you didn't have access to.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/public/500.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | We're sorry, but something went wrong (500)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
We're sorry, but something went wrong.
62 |
63 |
If you are the application owner check the logs for more information.
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/public/favicon.ico
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2 | #
3 | # To ban all spiders from the entire site uncomment the next two lines:
4 | # User-agent: *
5 | # Disallow: /
6 |
--------------------------------------------------------------------------------
/spec/controllers/api/products_controller_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe Api::ProductsController, type: :controller do
4 | it { should route(:get, '/api/products').to(action: :index) }
5 |
6 | it { should route(:get, '/api/products/1').to(action: :show, id: 1) }
7 |
8 | describe '#index.json' do
9 | before { get :index, format: :json }
10 |
11 | it { should render_template :index }
12 | end
13 |
14 | describe '#show.json' do
15 | before { get :show, id: 1, format: :json }
16 |
17 | it { should render_template :show }
18 | end
19 |
20 | describe '#collection' do
21 | before { expect(subject).to receive(:params).and_return(:params) }
22 |
23 | before { expect(Product).to receive(:search_by).with(:params) }
24 |
25 | it { expect { subject.send :collection }.to_not raise_error }
26 | end
27 |
28 | describe '#resource' do
29 | before { expect(subject).to receive(:params).and_return({ id: 1 }) }
30 |
31 | before { expect(Product).to receive(:find).with(1) }
32 |
33 | it { expect { subject.send :resource }.to_not raise_error }
34 | end
35 | end
36 |
--------------------------------------------------------------------------------
/spec/controllers/api/sessions_controller_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe Api::SessionsController, type: :controller do
4 | it { should route(:post, 'api/session').to(action: :create) }
5 |
6 | it { should route(:delete, 'api/session').to(action: :destroy) }
7 |
8 | describe '#create.json' do
9 | let(:session) { double }
10 |
11 | before { expect(Session).to receive(:new).with(email: 'test@test.com', password: 'test').and_return(session) }
12 |
13 | before { expect(session).to receive(:save!) }
14 |
15 | before { post :create, session: { email: 'test@test.com', password: 'test' }, format: :json }
16 |
17 | it { should render_template :create }
18 | end
19 |
20 | describe '#destroy.json' do
21 | let(:session) { double }
22 |
23 | let(:user) { stub_model User }
24 |
25 | before { expect(subject).to receive(:authenticate) }
26 |
27 | before { expect(subject).to receive(:current_user).and_return(user) }
28 |
29 | before { expect(Session).to receive(:new).with(user: user).and_return(session) }
30 |
31 | before { expect(session).to receive(:destroy!) }
32 |
33 | before { delete :destroy, format: :json }
34 |
35 | it { should respond_with :ok }
36 | end
37 | end
--------------------------------------------------------------------------------
/spec/controllers/api/users_controller_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe Api::UsersController, type: :controller do
4 | it { should route(:post, 'api/user').to(action: :create) }
5 |
6 | describe '#create.json' do
7 | let(:params) do
8 | {
9 | name: 'Test name',
10 | email: 'test@test.com',
11 | password: '12345678',
12 | password_confirmation: '12345678'
13 | }
14 | end
15 |
16 | let(:user) { stub_model User }
17 |
18 | before { expect(User).to receive(:new).with(params).and_return(user) }
19 |
20 | before { expect(user).to receive(:save!) }
21 |
22 | before { post :create, user: params, format: :json }
23 |
24 | it { should render_template :create }
25 | end
26 | end
27 |
--------------------------------------------------------------------------------
/spec/controllers/application_controller_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe ApplicationController, type: :controller do
4 | describe '#authenticate' do
5 | before { expect(subject).to receive(:authenticate_or_request_with_http_token).and_yield('token', nil) }
6 |
7 | before do
8 | #
9 | # User.joins(:auth_token).find_by(auth_tokens: { value: 'token' })
10 | #
11 | expect(User).to receive(:joins).with(:auth_token) do
12 | double.tap do |a|
13 | expect(a).to receive(:find_by).with(auth_tokens: { value: 'token' })
14 | end
15 | end
16 | end
17 |
18 | it { expect { subject.send :authenticate }.to_not raise_error }
19 | end
20 |
21 | describe '#json_request?' do
22 | before do
23 | #
24 | # subject.request.format.json? -> true
25 | #
26 | expect(subject).to receive(:request) do
27 | double.tap do |a|
28 | expect(a).to receive(:format) do
29 | double.tap do |b|
30 | expect(b).to receive(:json?).and_return(true)
31 | end
32 | end
33 | end
34 | end
35 | end
36 |
37 | its(:json_request?) { should eq true }
38 | end
39 | end
--------------------------------------------------------------------------------
/spec/decorators/product_decorator_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | describe ProductDecorator do
4 | describe '#as_json' do
5 | let(:product) { stub_model Product, id: 1, name: 'apple', price: 10.0, description: 'green' }
6 |
7 | subject { product.decorate.as_json }
8 |
9 | its([:id]) { should eq 1 }
10 |
11 | its([:name]) { should eq 'apple' }
12 |
13 | its([:price]) { should eq 10.0 }
14 |
15 | its([:description]) { should eq 'green' }
16 | end
17 | end
--------------------------------------------------------------------------------
/spec/decorators/user_decorator_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe UserDecorator do
4 | describe '#as_json' do
5 | let(:user) { stub_model User, name: 'Test name', email: 'test@test.com' }
6 |
7 | subject { user.decorate.as_json }
8 |
9 | its([:name]) { should eq 'Test name' }
10 |
11 | its([:email]) { should eq 'test@test.com' }
12 | end
13 | end
--------------------------------------------------------------------------------
/spec/lib/session_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe Session, type: :lib do
4 | it { should be_a ActiveModel::Validations }
5 |
6 | let(:session) { Session.new email: 'test@test.com', password: '12345678' }
7 |
8 | let(:user) { stub_model User }
9 |
10 | subject { session }
11 |
12 | its(:email) { should eq 'test@test.com' }
13 |
14 | its(:password) { should eq '12345678' }
15 |
16 | its(:decorate) { should eq subject }
17 |
18 | describe '#user' do
19 | before { expect(User).to receive(:find_by).with(email: 'test@test.com') }
20 |
21 | it { expect { subject.send :user }.to_not raise_error }
22 | end
23 |
24 | context 'validations' do
25 | subject { session.errors }
26 |
27 | context do
28 | before { expect(session).to receive(:user) }
29 |
30 | before { session.valid? }
31 |
32 | its([:email]) { should eq ['not found'] }
33 | end
34 |
35 | context do
36 | before { expect(session).to receive(:user).twice.and_return(user) }
37 |
38 | before { expect(user).to receive(:authenticate).with('12345678').and_return(false) }
39 |
40 | before { session.valid? }
41 |
42 | its([:password]) { should eq ['is invalid'] }
43 | end
44 | end
45 |
46 | describe '#save!' do
47 | context do
48 | before { expect(subject).to receive(:valid?).and_return(false) }
49 |
50 | it { expect { subject.save! }.to raise_error(ActiveModel::StrictValidationFailed) }
51 | end
52 |
53 | context do
54 | before { expect(subject).to receive(:user).and_return(user) }
55 |
56 | before { expect(subject).to receive(:valid?).and_return(true) }
57 |
58 | before { expect(SecureRandom).to receive(:uuid).and_return('XXXX-YYYY-ZZZZ') }
59 |
60 | before { expect(user).to receive(:create_auth_token).with(value: 'XXXX-YYYY-ZZZZ') }
61 |
62 | it { expect { subject.save! }.to_not raise_error }
63 | end
64 | end
65 |
66 | describe '#destroy!' do
67 | before do
68 | #
69 | # subject.user.auth_token.destroy!
70 | #
71 | expect(subject).to receive(:user) do
72 | double.tap do |a|
73 | expect(a).to receive(:auth_token) do
74 | double.tap do |b|
75 | expect(b).to receive(:destroy!)
76 | end
77 | end
78 | end
79 | end
80 | end
81 |
82 | it { expect { subject.destroy! }.to_not raise_error }
83 | end
84 |
85 | describe '#auth_token' do
86 | context do
87 | before { expect(subject).to receive(:user) }
88 |
89 | its(:auth_token) { should eq nil }
90 | end
91 |
92 | context do
93 | let(:auth_token) { stub_model AuthToken, value: 'XXXX-YYYY-ZZZZ' }
94 |
95 | let(:user) { stub_model User, auth_token: auth_token }
96 |
97 | before { expect(subject).to receive(:user).and_return(user) }
98 |
99 | its(:auth_token) { should eq 'XXXX-YYYY-ZZZZ' }
100 | end
101 | end
102 |
103 | describe '#as_json' do
104 | before { expect(subject).to receive(:auth_token).and_return('XXXX-YYYY-ZZZZ') }
105 |
106 | its(:as_json) { should eq auth_token: 'XXXX-YYYY-ZZZZ' }
107 | end
108 | end
--------------------------------------------------------------------------------
/spec/models/auth_token_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe AuthToken, type: :model do
4 | it { should belong_to :user }
5 |
6 | it { should validate_presence_of :value }
7 | end
8 |
--------------------------------------------------------------------------------
/spec/models/product_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe Product, type: :model do
4 | it { should be_a PgSearch }
5 |
6 | describe '.search_by' do
7 | let(:relation) { double }
8 |
9 | before { expect(Product).to receive(:page).with(1).and_return(relation) }
10 |
11 | context do
12 | it { expect { Product.search_by 'page' => 1 }.to_not raise_error }
13 | end
14 |
15 | context do
16 | before { expect(relation).to receive(:where).with('name ILIKE ?', 'abc%') }
17 |
18 | it { expect { Product.search_by 'page' => 1, 'term' => 'abc' }.to_not raise_error }
19 | end
20 |
21 | context do
22 | before { expect(relation).to receive(:search).with('word') }
23 |
24 | it { expect { Product.search_by 'page' => 1, 'name' => 'word' }.to_not raise_error }
25 | end
26 | end
27 | end
--------------------------------------------------------------------------------
/spec/models/user_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | RSpec.describe User, type: :model do
4 | it { should have_secure_password }
5 |
6 | it { should validate_presence_of :name }
7 |
8 | it { should validate_presence_of :email }
9 |
10 | it { should validate_uniqueness_of :email }
11 |
12 | it { should_not allow_value('test').for(:email) }
13 |
14 | it { should allow_value('test@test.com').for(:email) }
15 |
16 | it { should have_one(:auth_token).dependent(:destroy) }
17 | end
18 |
--------------------------------------------------------------------------------
/spec/rails_helper.rb:
--------------------------------------------------------------------------------
1 | # This file is copied to spec/ when you run 'rails generate rspec:install'
2 | ENV['RAILS_ENV'] ||= 'test'
3 | require File.expand_path('../../config/environment', __FILE__)
4 | # Prevent database truncation if the environment is production
5 | abort("The Rails environment is running in production mode!") if Rails.env.production?
6 | require 'spec_helper'
7 | require 'rspec/rails'
8 | # Add additional requires below this line. Rails is not loaded until this point!
9 |
10 | # Requires supporting ruby files with custom matchers and macros, etc, in
11 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
12 | # run as spec files by default. This means that files in spec/support that end
13 | # in _spec.rb will both be required and run as specs, causing the specs to be
14 | # run twice. It is recommended that you do not name files matching this glob to
15 | # end with _spec.rb. You can configure this pattern with the --pattern
16 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
17 | #
18 | # The following line is provided for convenience purposes. It has the downside
19 | # of increasing the boot-up time by auto-requiring all files in the support
20 | # directory. Alternatively, in the individual `*_spec.rb` files, manually
21 | # require only the support files necessary.
22 | #
23 | # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
24 |
25 | # Checks for pending migrations before tests are run.
26 | # If you are not using ActiveRecord, you can remove this line.
27 | ActiveRecord::Migration.maintain_test_schema!
28 |
29 | RSpec.configure do |config|
30 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
31 | config.fixture_path = "#{::Rails.root}/spec/fixtures"
32 |
33 | # If you're not using ActiveRecord, or you'd prefer not to run each of your
34 | # examples within a transaction, remove the following line or assign false
35 | # instead of true.
36 | config.use_transactional_fixtures = true
37 |
38 | # RSpec Rails can automatically mix in different behaviours to your tests
39 | # based on their file location, for example enabling you to call `get` and
40 | # `post` in specs under `spec/controllers`.
41 | #
42 | # You can disable this behaviour by removing the line below, and instead
43 | # explicitly tag your specs with their type, e.g.:
44 | #
45 | # RSpec.describe UsersController, :type => :controller do
46 | # # ...
47 | # end
48 | #
49 | # The different available types are documented in the features, such as in
50 | # https://relishapp.com/rspec/rspec-rails/docs
51 | config.infer_spec_type_from_file_location!
52 | end
53 |
54 | Shoulda::Matchers.configure do |config|
55 | config.integrate do |with|
56 | with.test_framework :rspec
57 |
58 | with.library :rails
59 | end
60 | end
61 |
62 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | # This file was generated by the `rails generate rspec:install` command. Conventionally, all
2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3 | # The generated `.rspec` file contains `--require spec_helper` which will cause
4 | # this file to always be loaded, without a need to explicitly require it in any
5 | # files.
6 | #
7 | # Given that it is always loaded, you are encouraged to keep this file as
8 | # light-weight as possible. Requiring heavyweight dependencies from this file
9 | # will add to the boot time of your test suite on EVERY test run, even for an
10 | # individual file that may not need all of that loaded. Instead, consider making
11 | # a separate helper file that requires the additional dependencies and performs
12 | # the additional setup, and require it from the spec files that actually need
13 | # it.
14 | #
15 | # The `.rspec` file also contains a few flags that are not defaults but that
16 | # users commonly want.
17 | #
18 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19 | RSpec.configure do |config|
20 | # rspec-expectations config goes here. You can use an alternate
21 | # assertion/expectation library such as wrong or the stdlib/minitest
22 | # assertions if you prefer.
23 | config.expect_with :rspec do |expectations|
24 | # This option will default to `true` in RSpec 4. It makes the `description`
25 | # and `failure_message` of custom matchers include text for helper methods
26 | # defined using `chain`, e.g.:
27 | # be_bigger_than(2).and_smaller_than(4).description
28 | # # => "be bigger than 2 and smaller than 4"
29 | # ...rather than:
30 | # # => "be bigger than 2"
31 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32 | end
33 |
34 | # rspec-mocks config goes here. You can use an alternate test double
35 | # library (such as bogus or mocha) by changing the `mock_with` option here.
36 | config.mock_with :rspec do |mocks|
37 | # Prevents you from mocking or stubbing a method that does not exist on
38 | # a real object. This is generally recommended, and will default to
39 | # `true` in RSpec 4.
40 | mocks.verify_partial_doubles = true
41 | end
42 |
43 | # The settings below are suggested to provide a good initial experience
44 | # with RSpec, but feel free to customize to your heart's content.
45 | =begin
46 | # These two settings work together to allow you to limit a spec run
47 | # to individual examples or groups you care about by tagging them with
48 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49 | # get run.
50 | config.filter_run :focus
51 | config.run_all_when_everything_filtered = true
52 |
53 | # Allows RSpec to persist some state between runs in order to support
54 | # the `--only-failures` and `--next-failure` CLI options. We recommend
55 | # you configure your source control system to ignore this file.
56 | config.example_status_persistence_file_path = "spec/examples.txt"
57 |
58 | # Limits the available syntax to the non-monkey patched syntax that is
59 | # recommended. For more details, see:
60 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61 | # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63 | config.disable_monkey_patching!
64 |
65 | # Many RSpec users commonly either run the entire suite or an individual
66 | # file, and it's useful to allow more verbose output when running an
67 | # individual spec file.
68 | if config.files_to_run.one?
69 | # Use the documentation formatter for detailed output,
70 | # unless a formatter has already been configured
71 | # (e.g. via a command-line flag).
72 | config.default_formatter = 'doc'
73 | end
74 |
75 | # Print the 10 slowest examples and example groups at the
76 | # end of the spec run, to help surface which specs are running
77 | # particularly slow.
78 | config.profile_examples = 10
79 |
80 | # Run specs in random order to surface order dependencies. If you find an
81 | # order dependency and want to debug it, you can fix the order by providing
82 | # the seed, which is printed after each run.
83 | # --seed 1234
84 | config.order = :random
85 |
86 | # Seed global randomization in this process using the `--seed` CLI option.
87 | # Setting this allows you to use `--seed` to deterministically reproduce
88 | # test failures related to randomization by passing the same `--seed` value
89 | # as the one that triggered the failure.
90 | Kernel.srand config.seed
91 | =end
92 | end
93 |
--------------------------------------------------------------------------------
/vendor/assets/javascripts/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/vendor/assets/javascripts/.keep
--------------------------------------------------------------------------------
/vendor/assets/stylesheets/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MLSDev/E-commerce-API/495daa1687ce0e95b916e010e51c54d80190e218/vendor/assets/stylesheets/.keep
--------------------------------------------------------------------------------