dune-api 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.gitmodules +3 -0
- data/.rspec +2 -0
- data/.travis.yml +19 -0
- data/CHANGELOG.md +48 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +601 -0
- data/LICENSE.txt +22 -0
- data/README.md +19 -0
- data/README.md.backup +61 -0
- data/Rakefile +2 -0
- data/apiary.apib +1769 -0
- data/app/constraints/dune/api/api_constraint.rb +17 -0
- data/app/controllers/dune/api/base_controller.rb +68 -0
- data/app/controllers/dune/api/v1/channels/members_controller.rb +33 -0
- data/app/controllers/dune/api/v1/channels_controller.rb +73 -0
- data/app/controllers/dune/api/v1/investments_controller.rb +70 -0
- data/app/controllers/dune/api/v1/press_assets_controller.rb +38 -0
- data/app/controllers/dune/api/v1/projects_controller.rb +76 -0
- data/app/controllers/dune/api/v1/rewards_controller.rb +9 -0
- data/app/controllers/dune/api/v1/sessions_controller.rb +27 -0
- data/app/controllers/dune/api/v1/tags_controller.rb +37 -0
- data/app/controllers/dune/api/v1/users_controller.rb +26 -0
- data/app/models/dune/api/access_token.rb +24 -0
- data/app/models/dune/api/investment.rb +14 -0
- data/app/models/dune/api/project.rb +23 -0
- data/app/models/dune/api/user_concern.rb +11 -0
- data/app/serializers/channel_member_serializer.rb +7 -0
- data/app/serializers/channel_serializer.rb +34 -0
- data/app/serializers/dune/api/investment_serializer.rb +44 -0
- data/app/serializers/dune/api/project_serializer.rb +86 -0
- data/app/serializers/press_asset_serializer.rb +11 -0
- data/app/serializers/reward_serializer.rb +13 -0
- data/app/serializers/tag_serializer.rb +10 -0
- data/app/serializers/user_serializer.rb +46 -0
- data/bin/rails +8 -0
- data/config/initializers/mime_types.rb +1 -0
- data/config/routes.rb +42 -0
- data/db/migrate/20140624141405_create_dune_api_access_tokens.rb +11 -0
- data/dune-api.gemspec +28 -0
- data/lib/dune/api.rb +12 -0
- data/lib/dune/api/engine.rb +11 -0
- data/lib/dune/api/paginated_controller.rb +19 -0
- data/lib/dune/api/version.rb +5 -0
- data/spec/constraints/neighborly/api/api_constraint_spec.rb +50 -0
- data/spec/controllers/neighborly/api/v1/channels/members_controller_spec.rb +82 -0
- data/spec/controllers/neighborly/api/v1/channels_controller_spec.rb +188 -0
- data/spec/controllers/neighborly/api/v1/investments_controller_spec.rb +178 -0
- data/spec/controllers/neighborly/api/v1/press_assets_controller_spec.rb +129 -0
- data/spec/controllers/neighborly/api/v1/projects_controller_spec.rb +317 -0
- data/spec/controllers/neighborly/api/v1/rewards_controller_spec.rb +28 -0
- data/spec/controllers/neighborly/api/v1/sessions_controller_spec.rb +67 -0
- data/spec/controllers/neighborly/api/v1/tags_controller_spec.rb +143 -0
- data/spec/controllers/neighborly/api/v1/users_controller_spec.rb +43 -0
- data/spec/factories.rb +78 -0
- data/spec/fixtures/image.png +0 -0
- data/spec/models/neighborly/api/investment_spec.rb +33 -0
- data/spec/models/neighborly/api/user_concern_spec.rb +33 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/shared_examples.rb +96 -0
- metadata +219 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
class ApiConstraint
|
3
|
+
attr_reader :revision
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@revision = options.fetch(:revision)
|
7
|
+
@default = options[:default]
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(request)
|
11
|
+
@default || request
|
12
|
+
.headers
|
13
|
+
.fetch(:accept)
|
14
|
+
.include?("revision=#{revision}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
class BaseController < ActionController::Metal
|
3
|
+
|
4
|
+
MODULES = [
|
5
|
+
AbstractController::Rendering,
|
6
|
+
ActionController::Redirecting,
|
7
|
+
ActionView::Rendering, # This is needed because of respond_with
|
8
|
+
ActionController::Rendering,
|
9
|
+
ActionController::Renderers::All,
|
10
|
+
ActionController::ConditionalGet,
|
11
|
+
ActionController::MimeResponds,
|
12
|
+
ActionController::ImplicitRender,
|
13
|
+
ActionController::StrongParameters,
|
14
|
+
ActionController::ForceSSL,
|
15
|
+
ActionController::HttpAuthentication::Token::ControllerMethods,
|
16
|
+
ActionController::Serialization,
|
17
|
+
ActionController::Instrumentation,
|
18
|
+
ActionController::ParamsWrapper,
|
19
|
+
ActionController::Rescue,
|
20
|
+
HasScope,
|
21
|
+
Pundit,
|
22
|
+
Dune::Api::Engine.routes.url_helpers,
|
23
|
+
Rails.application.routes.url_helpers,
|
24
|
+
Pundit,
|
25
|
+
|
26
|
+
#ActionController::Helpers,
|
27
|
+
#ActionController::UrlFor,
|
28
|
+
#ActionController::RackDelegation,
|
29
|
+
#AbstractController::Callbacks,
|
30
|
+
]
|
31
|
+
|
32
|
+
MODULES.each do |mod|
|
33
|
+
include mod
|
34
|
+
end
|
35
|
+
|
36
|
+
respond_to :json
|
37
|
+
before_action :check_authorization!
|
38
|
+
|
39
|
+
rescue_from Pundit::NotAuthorizedError, with: :handle_forbidden
|
40
|
+
|
41
|
+
def handle_forbidden
|
42
|
+
head :forbidden
|
43
|
+
end
|
44
|
+
|
45
|
+
def access_token
|
46
|
+
@access_token
|
47
|
+
end
|
48
|
+
|
49
|
+
def current_user
|
50
|
+
@current_user ||= access_token.user
|
51
|
+
end
|
52
|
+
|
53
|
+
def require_admin!
|
54
|
+
handle_unauthorized unless current_user.admin?
|
55
|
+
end
|
56
|
+
|
57
|
+
def check_authorization!
|
58
|
+
authenticate_or_request_with_http_token do |token, options|
|
59
|
+
@access_token = AccessToken.find_by(code: token)
|
60
|
+
end
|
61
|
+
@access_token.is_a?(AccessToken) or handle_unauthorized
|
62
|
+
end
|
63
|
+
|
64
|
+
def handle_unauthorized
|
65
|
+
head :unauthorized
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class Channels::MembersController < BaseController
|
4
|
+
before_action :require_admin!
|
5
|
+
|
6
|
+
def index
|
7
|
+
respond_with parent.members, root: 'users'
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
channel_member = parent.channel_members.build(
|
12
|
+
user_id: params[:channel_member].try(:[], :user_id)
|
13
|
+
)
|
14
|
+
if channel_member.save
|
15
|
+
render json: channel_member.user, status: :created
|
16
|
+
else
|
17
|
+
respond_with channel_member
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
parent.channel_members.find_by(user_id: params[:id]).delete
|
23
|
+
head :no_content
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def parent
|
29
|
+
@channel ||= Channel.find(params[:channel_id])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class ChannelsController < BaseController
|
4
|
+
include PaginatedController
|
5
|
+
before_action :require_admin!, except: %i(index show update)
|
6
|
+
|
7
|
+
has_scope :pg_search, as: :query
|
8
|
+
|
9
|
+
def index
|
10
|
+
respond_with_pagination collection
|
11
|
+
end
|
12
|
+
|
13
|
+
def show
|
14
|
+
respond_with Channel.find(params[:id])
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
channel = ::Channel.new(permitted_params)
|
19
|
+
authorize channel
|
20
|
+
channel.save
|
21
|
+
respond_with channel
|
22
|
+
end
|
23
|
+
|
24
|
+
def update
|
25
|
+
@channel = Channel.find(params[:id])
|
26
|
+
authorize @channel
|
27
|
+
respond_with Channel.update(params[:id], permitted_params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy
|
31
|
+
channel = Channel.find(params[:id])
|
32
|
+
authorize channel
|
33
|
+
|
34
|
+
channel.delete
|
35
|
+
head :no_content
|
36
|
+
end
|
37
|
+
|
38
|
+
[:push_to_draft, :push_to_online].each do |name|
|
39
|
+
define_method name do
|
40
|
+
channel = Channel.find(params[:id])
|
41
|
+
authorize channel
|
42
|
+
|
43
|
+
channel.send("#{name.to_s}!")
|
44
|
+
head :no_content
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def permitted_params
|
51
|
+
params.permit(policy(@channel || Channel).permitted_attributes(params))[:channel]
|
52
|
+
end
|
53
|
+
|
54
|
+
def collection
|
55
|
+
@collection ||= begin
|
56
|
+
authorized_scope = policy_scope(Channel)
|
57
|
+
apply_scopes(
|
58
|
+
scoped_by_state(authorized_scope)
|
59
|
+
).order('created_at desc').all
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def scoped_by_state(scope)
|
64
|
+
state_scopes = params.slice(*Channel.state_names).keys
|
65
|
+
if state_scopes.any?
|
66
|
+
scope.with_state(state_scopes)
|
67
|
+
else
|
68
|
+
scope
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class InvestmentsController < BaseController
|
4
|
+
include PaginatedController
|
5
|
+
|
6
|
+
before_action :require_admin!
|
7
|
+
|
8
|
+
has_scope :pg_search, as: :query
|
9
|
+
has_scope :by_project_id, as: :project_id
|
10
|
+
has_scope :between_values,
|
11
|
+
using: %i(initial final),
|
12
|
+
type: :hash
|
13
|
+
|
14
|
+
def index
|
15
|
+
respond_with_pagination collection
|
16
|
+
end
|
17
|
+
|
18
|
+
def show
|
19
|
+
respond_with Dune::Api::Investment.find(params[:id])
|
20
|
+
end
|
21
|
+
|
22
|
+
def update
|
23
|
+
@investment = ::Investment.find(params[:id])
|
24
|
+
authorize @investment
|
25
|
+
respond_with ::Investment.update(params[:id], permitted_params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy
|
29
|
+
investment = ::Investment.find(params[:id])
|
30
|
+
authorize investment
|
31
|
+
|
32
|
+
investment.push_to_trash!
|
33
|
+
head :no_content
|
34
|
+
end
|
35
|
+
|
36
|
+
[:confirm, :pendent, :refund, :hide, :cancel].each do |name|
|
37
|
+
define_method name do
|
38
|
+
investment = ::Investment.find(params[:id])
|
39
|
+
authorize investment
|
40
|
+
|
41
|
+
investment.send("#{name.to_s}!")
|
42
|
+
head :no_content
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def permitted_params
|
49
|
+
params.permit(policy(@investment || ::Investment).permitted_attributes)[:investment]
|
50
|
+
end
|
51
|
+
|
52
|
+
def collection
|
53
|
+
@collection ||= begin
|
54
|
+
apply_scopes(
|
55
|
+
scoped_by_state(Dune::Api::Investment)
|
56
|
+
).order('created_at desc').all
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def scoped_by_state(scope)
|
61
|
+
state_scopes = params.slice(*Investment.state_names).keys
|
62
|
+
if state_scopes.any?
|
63
|
+
scope.with_state(state_scopes)
|
64
|
+
else
|
65
|
+
scope
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class PressAssetsController < BaseController
|
4
|
+
before_action :require_admin!, except: %i(index show)
|
5
|
+
|
6
|
+
include PaginatedController
|
7
|
+
|
8
|
+
def index
|
9
|
+
respond_with_pagination apply_scopes(PressAsset).all
|
10
|
+
end
|
11
|
+
|
12
|
+
def create
|
13
|
+
respond_with PressAsset.create(permited_params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def update
|
17
|
+
respond_with PressAsset.update(params[:id], permited_params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def show
|
21
|
+
respond_with PressAsset.find(params[:id])
|
22
|
+
end
|
23
|
+
|
24
|
+
def destroy
|
25
|
+
respond_with PressAsset.destroy(params[:id])
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def permited_params
|
31
|
+
params.permit(
|
32
|
+
{ press_asset:
|
33
|
+
PressAsset.attribute_names.map(&:to_sym) - [:created_at, :updated_at]
|
34
|
+
})[:press_asset]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class ProjectsController < Dune::Api::BaseController
|
4
|
+
include PaginatedController
|
5
|
+
|
6
|
+
has_scope :order_by
|
7
|
+
has_scope :pg_search, as: :query
|
8
|
+
has_scope :between_created_at,
|
9
|
+
:between_expires_at,
|
10
|
+
:between_online_date,
|
11
|
+
using: %i(starts_at ends_at),
|
12
|
+
type: :hash
|
13
|
+
|
14
|
+
def index
|
15
|
+
respond_with_pagination collection
|
16
|
+
end
|
17
|
+
|
18
|
+
def show
|
19
|
+
project = ::Project.find(params[:id])
|
20
|
+
authorize project
|
21
|
+
respond_with project, serializer: Dune::Api::ProjectSerializer
|
22
|
+
end
|
23
|
+
|
24
|
+
def update
|
25
|
+
@project = Project.find(params[:id])
|
26
|
+
authorize @project
|
27
|
+
respond_with Project.update(params[:id], permitted_params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy
|
31
|
+
project = Project.find(params[:id])
|
32
|
+
authorize project
|
33
|
+
|
34
|
+
project.push_to_trash!
|
35
|
+
head :no_content
|
36
|
+
end
|
37
|
+
|
38
|
+
[:approve, :launch, :reject, :push_to_draft].each do |name|
|
39
|
+
define_method name do
|
40
|
+
project = Project.find(params[:id])
|
41
|
+
authorize project
|
42
|
+
|
43
|
+
project.send("#{name.to_s}!")
|
44
|
+
head :no_content
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def permitted_params
|
51
|
+
params.permit(policy(@project || Project).permitted_attributes)[:project]
|
52
|
+
end
|
53
|
+
|
54
|
+
def collection
|
55
|
+
@collection ||= begin
|
56
|
+
if ActiveRecord::ConnectionAdapters::Column.
|
57
|
+
value_to_boolean(params[:manageable])
|
58
|
+
authorized_scope = policy_scope(Dune::Api::Project)
|
59
|
+
else
|
60
|
+
authorized_scope = Dune::Api::Project.visible
|
61
|
+
end
|
62
|
+
apply_scopes(scoped_by_state(authorized_scope)).without_state('deleted')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def scoped_by_state(scope)
|
67
|
+
state_scopes = params.slice(*Project.state_names).keys
|
68
|
+
if state_scopes.any?
|
69
|
+
scope.with_state(state_scopes)
|
70
|
+
else
|
71
|
+
scope
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class SessionsController < BaseController
|
4
|
+
skip_before_action :check_authorization!, only: :create
|
5
|
+
|
6
|
+
def create
|
7
|
+
user = User.find_by(email: params.fetch(:email))
|
8
|
+
if user && user.valid_password?(params.fetch(:password))
|
9
|
+
render status: :created, json: {
|
10
|
+
access_token: user.get_access_token,
|
11
|
+
user_id: user.id
|
12
|
+
}
|
13
|
+
else
|
14
|
+
render status: :unauthorized, json: {}
|
15
|
+
end
|
16
|
+
rescue KeyError
|
17
|
+
render status: :bad_request, json: {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy
|
21
|
+
access_token.try(:expire!)
|
22
|
+
|
23
|
+
render status: :ok, json: {}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Dune::Api
|
2
|
+
module V1
|
3
|
+
class TagsController < BaseController
|
4
|
+
before_action :require_admin!, except: %i(index show)
|
5
|
+
|
6
|
+
include PaginatedController
|
7
|
+
|
8
|
+
has_scope :popular, type: :boolean
|
9
|
+
|
10
|
+
def index
|
11
|
+
respond_with_pagination apply_scopes(Tag).all
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
respond_with Tag.create(permited_params)
|
16
|
+
end
|
17
|
+
|
18
|
+
def update
|
19
|
+
respond_with Tag.update(params[:id], permited_params)
|
20
|
+
end
|
21
|
+
|
22
|
+
def show
|
23
|
+
respond_with Tag.find(params[:id])
|
24
|
+
end
|
25
|
+
|
26
|
+
def destroy
|
27
|
+
respond_with Tag.destroy(params[:id])
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def permited_params
|
33
|
+
params.permit({ tag: [ :name, :visible ] })[:tag]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|