Class: HappySeed::Generators::ApiGenerator

Inherits:
HappySeedGenerator show all
Defined in:
lib/generators/happy_seed/api/api_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fingerprintObject



8
9
10
# File 'lib/generators/happy_seed/api/api_generator.rb', line 8

def self.fingerprint
  gem_available? 'apitome'
end

Instance Method Details

#install_apiObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/happy_seed/api/api_generator.rb', line 12

def install_api
  return if already_installed

  gem 'apitome'
  gem_group :development, :test do
    gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'faker'
    gem 'rspec_api_documentation'
  end

  generate 'rspec:install'

  require_generator DeviseGenerator

  Bundler.with_clean_env do
    run 'bundle install --without production'
  end

  generate 'model UserToken user:belongs_to access_token:string'

  directory '.'

  route "scope module: :api, defaults: {format: :json} do
    %w(v1).each do |version|
namespace version.to_sym do
  resources :users, only: %w(create update show) do
    collection do
      post :forgot_password
      put :reset_password
    end
  end
  resource :user_token, path: :token, only: %w(create destroy)
end
    end
  end\n"

  inject_into_class 'app/models/user.rb', 'User' do
    '  has_many :user_tokens, dependent: :destroy'
  end

  gsub_file 'app/models/user_token.rb', /belongs_to :user\n/, "belongs_to :user, required: true

  validates :access_token, presence: true, uniqueness: {case_sensitive: false}

  before_validation :set_access_token

  private

  def set_access_token
    self.access_token ||= loop do
random_string = SecureRandom.hex(4).downcase
break random_string if self.class.where(access_token: random_string).empty?
    end
  end\n"

  append_to_file 'spec/rails_helper.rb', "\nRspecApiDocumentation.configure do |config|
  config.format = :json
  config.docs_dir = Pathname('docs/api')

  config.request_headers_to_include = %w(Authorization)
  config.response_headers_to_include = %w()
end"

  insert_into_file 'spec/factories/users.rb', before: /end\s*\z/ do
    "\n  factory :user_with_token, parent: :user do
    after :build do |user, evaluator|
user.user_tokens.build
    end
  end\n"
  end

  rake 'db:migrate:reset', env: 'test'
  rake 'docs:generate', env: 'test'
end