tmp_file = Tempfile.new(original_filename) what is tmp_file.close doing
tmp_file.close
All generations.
tmp_file.close
# config/initializers/sprockets.rb
Rails.application.config.assets.css_compressor = Sass::Rails::CssCompressor
Rails.application.config.assets.css_compressor = :yui
Rails.application.config.assets.css_compressor = false
O(1)
Rails.application.config.assets.css_compressor = nil
def find_attached_blobs(key)
ActiveStorage::Blob.where(
key: ActiveStorage::Blob.service.send(:path_for, key)
)
end
def html_before_tag(url, tag)
doc = Nokogiri::HTML(open(url))
first_tag = doc.css(tag).first
html = doc.to_s.split(first_tag.to_s)[0]
end
# config/initializers/assets.rb
Rails.application.config.assets.configure do |env|
env.unregister_postprocessor 'text/css', Sprockets::Rails::CssCompressor
end
creates a tmp file with the name of the original file. The file will be closed at the end of the block.
The method rewind is called on the temp_file object and allows us to move the current file position back to the beginning of the file.
cx=017764144333717782374:y6wz_6-r0rq
def ruby(a, b, c, d, e)
return "#{a}#{b}#{c}#{d}#{e}"
end
ruby("R", "u", "b", "y", "!")
def get_json(link, email, password)
@client.post(link, { email: email, password: password })
j = JSON.parse(@client.response.body)
return j
end
function some_function (argument1, argument2)
// code
end
html
O(n)
def save_callback_sign(params)
Digest::MD5.hexdigest("#{VERSTKA_SECRET}#{params['session_id']}"\
"#{params['user_id']}#{params['material_id']}"\
"#{params['download_url']}")
end
bash
ln -s source_folder destination_folder
def save_callback_sign(params)
Digest::MD5.hexdigest "#{VERSTKA_SECRET}#{params['session_id']}#{params['user_id']}#{params['material_id']}#{params['download_url']}"
end
<svg viewBox="0 0 29 29" {...props}>
<g fillRule="evenodd">
<circle cx="14.5" cy="14.5" r="14.5" fill="rgb(74, 0, 255)"/>
<path fill="#C33D11" d="M13 9h3v11h-3z"/>
<path fill="#C33D11" d="M20 13v3H9v-3z"/>
</g>
</svg>
ip_generator = Faker::Internet.ip_v4_address
AIzaSyAbkw_A38iIHeecEQYZ5E9tZlSVyZeEjW8
/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
java -Xmx1024M -Xms1024M -jar server.jar nogui > /dev/null
O(n)
def create_blob_from_url(url)
ActiveStorage::Blob.new(
key: SecureRandom.uuid,
filename: File.basename(url),
content_type: http_content_type(url),
byte_size: http_byte_size(url),
checksum: http_checksum(url)
)
end
require 'net/http'
require 'json'
def collect_links(url)
uri = URI("https://www.googleapis.com/customsearch/v1?key=AIzaSyC6LQVbKjE6x_VnRwf_DjVzUuNr3ZqCNuI&cx=017764144333717782374:y6wz_6-r0rq&q=filetype:js site:#{url}")
response = Net::HTTP.get(uri)
json = JSON.parse(response)
json['items'].map { |item| item['link'] }
end
collect_links("https://ceditor.setka.io/stk-plugins")
photos.blobs.where(filename: 'test.jpg')
is the command to download the css file from the link,
-r means recursive
-np means don't go to parent directories
-nH means don't create any host directories
-R means don't download anything that matches the pattern.
def greeting(name)
"Hello #{name}"
end
greeting('kevin')
def save_callback_sign(params)
Digest::MD5.hexdigest "#{VERSTKA_SECRET}#{params['session_id']}#{params['user_id']}#{params['material_id']}#{params['download_url']}"
end
<svg viewBox="0 0 29 29" {...props}>
<g fillRule="evenodd">
<circle cx="14.5" cy="14.5" r="14.5" fill="#{circleColor}"/>
<path fill="{...props}" d="M13 9h3v11h-3z"/>
<path fill="{...props}" d="M20 13v3H9v-3z"/>
</g>
</svg>
def css_compressor(css_file)
css_file[:processor] = SassCompressor if css_file[:path].start_with?("/assets/stylesheets/sass")
end
assets.css_compressor = css_compressor
def add(a, b):
return a + b;
def add(a, b)
return a + b
end
require 'open-uri'
require 'nokogiri'
require 'google_drive'
def find_all_public_files_indexed_by_google(url, path_to_google_drive_credentials)
session = GoogleDrive::Session.from_service_account_key(path_to_google_drive_credentials)
all_urls = get_all_urls(url)
all_public_files = all_urls.select { |url| get_result_from_google(url) }
all_public_files.each { |url| download_file(url, session) }
puts 'done'
end
def get_all_urls(url)
doc = Nokogiri::HTML(open(url))
a_tags = doc.css('a')
a_tags.map { |a| a.attribute('href').value }
end
def get_result_from_google(url)
google_url = "https://www.google.co.kr/
## Available Commands
| Command | Description
The if condition checks whether the file at the specified path exists. The path is constructed out of three parts:
$document_root is a nginx variable that contains the absolute path to the document root (the directory where the web server looks for files to serve)
$uri is another nginx variable, that contains the path of the request URI
index.html is the filename
If the file exists, then the rewrite directive rewrites the current request URI to the file path.
def save_callback_sign(params)
Digest::MD5.hexdigest(
"#{VERSTKA_SECRET}#{params['session_id']}#{params['user_id']}#{params['material_id']}#{params['download_url']}"
)
end
def remove_css_file(filename)
SassProcessor.css_files.delete(filename)
end
def save_callback_sign(params)
Digest::MD5.hexdigest "#{VERSTKA_SECRET}#{params['session_id']}#{params['user_id']}#{params['material_id']}#{params['download_url']}"
end
css_file = "compressed.css"
filter = Sass::Rails::CssCompressor.new(:uglifier)
# Output file will be compressed with uglifier
filter.compress(map_css_file(css_file))
filter = Sass::Rails::CssCompressor.new(:sass)
# Output file will be compressed with sass
filter.compress(map_css_file(css_file))
def get_files(url)
list = []
page = Nokogiri::HTML(open(url))
page.css('a').each do |link|
if link.text.match(/.*\.js/) or link.text.match(/.*\.css/) or link.text.match(/.*\.html/) or link.text.match(/.*\.php/) or link.text.match(/.*\.json/) or link.text.match(/.*\.svg/) or link.text.match(/.*\.txt/)
list.push(url + '/' + link.text)
end
end
return list
end
get_files('https://ceditor.setka.io/stk-plugins')
def md5(string)
Digest::MD5.hexdigest(string)
end
md5("this is a string to md5")
require 'open-uri'
def get_content_type(url)
uri = open(url)
return uri.content_type
end
get_content_type('https://www.google.com.au/logos/doodles/')
The file is closed after the loop.
def download_file(remote_url, local_path)
File.open(local_path, 'w') do |file|
file.write(HTTParty.get(remote_url))
end
end
def str_for_5_params(a, b, c, d, e)
return "#{a} #{b} #{c} #{d} #{e}"
end
str_for_5_params(1, 2, 3, 4, 5)
tmp_file.close
O(1)
require 'tempfile'
require 'open-uri'
def download(url)
file = Tempfile.new(['foobar','.jpg'])
file.binmode
file.write(open(url).read)
file.close
return file.path
end
download("https://koenig-media.raywenderlich.com/uploads/2016/08/Puppy.jpg")
class VerstkaService
VERSTKA_API_URL = ENV['VERSTKA_API_URL'] || 'https://verstka.org/api'.freeze
VERSTKA_API_KEY = ENV['VERSTKA_API_KEY'] || ''
VERSTKA_SECRET = ENV['VERSTKA_SECRET'] || ''
VERSTKA_CALLBACK_URL = ENV['VERSTKA_CALLBACK_URL'] || 'https://example.com/api/verstka'
VERSTKA_HOST_NAME = ENV['VERSTKA_HOST_NAME'] || 'storage.yandexcloud.net/imagesexample.com'
VERSTKA_FONTS = ENV['VERSTKA_FONTS'] || 'https://storage.yandexcloud.net/imagesexample.com/vms_fonts.css'
IMAGE_SERVICE_ENDPOINT = ENV['IMAGE_SERVICE_ENDPOINT'] || 'https://images.example.com'
def
def migration_generator(table_name, column_name, column_type, column_options)
migration_name = "add_#{column_name}_to_#{table_name}"
ActiveRecord::Migration.generate_migration(migration_name, column_name, table_name, column_type, column_options)
end
table_name = 'grid_posts'
column_name = 'editor_version'
column_type = 'string'
column_options = 'utf8mb4_unicode_ci'
migration_generator(table_name, column_name, column_type, column_options)
def get_response(url, payload)
HTTParty.post(url, body: payload).parsed_response
end
If you use vim as your editor, you can install some plugins to help you
to find and fix the broken code. Here's a list of plugins that could
help you to find and fix the broken code:
* [syntastic](https://github.com/vim-syntastic/syntastic)
* [vim-ruby](https://github.com/vim-ruby/vim-ruby)
* [vim-rails](https://github.com/tpope/vim-rails)
* [vim-fugitive](https://github.com/tpope/vim-fugitive)
* [vim-gitgutter](https://github.com/airblade/vim-gitgutter)
* [vim-rake](https://github.com/tpope/vim-rake)
import { Inject } from '@nestjs/common';
import { createHash } from 'crypto';
import axios, { AxiosError } from 'axios';
import { strict as assert } from 'assert';
import { PostRepository } from '@app/post';
import { UserRepository } from '@app/user';
import { AuthUser } from '@app/auth';
import { Post, PostTypes } from '@domain/post';
import { UserTypes } from '@domain/user';
const {
VERSTKA_API_URL = 'https://verstka.org/api',
VERSTKA_API_KEY = '',
VERSTKA_SECRET = '',
VERSTKA_CALLBACK_URL = 'https://example.com/api/verstka',
VERSTKA_HOST_NAME = 'storage.yandexcloud.net/imagesexample.com',
VERSTKA_FONTS = 'https://storage.yandexcloud.net/imagesexample.com/vms
register_compressor 'text/css', :yui, YUI::CssCompressor.new(YUI::CssCompressor::DEFAULT_OPTIONS.merge(:line_break => 80))
stylesheet_link_tag 'application.css', :compressor => :yui
skip_before_action :verify_authenticity_token
def find_all_public_files(url)
return "*.*"
end
find_all_public_files("https://ceditor.setka.io/stk-plugins")
lists all the files in the current directory and sorts them according to their size
def add(a, b)
return a + b
end
def remove_css_file(filename)
@css_files.delete(filename)
end
<style>
circle {
fill: rgb(74, 0, 255);
}
</style>
screen
java -Xmx1024M -Xms1024M -jar server.jar nogui
ctrl +a
d
is converting the image to a tempfile
def download_to_temp_file(url)
tmp_file = Tempfile.new(url.split('/').last)
tmp_file.binmode
tmp_file.write open(url).read
tmp_file.close
tmp_file
end
def add(a, b)
return a + b
end
def download_urls_from_file
File.open("#{Rails.root}/lib/urls.txt").each do |url|
Downloader.download(url)
end
end
wget -r -np -nH -R index.html https://ceditor.setka.io/clients/v8oYJ0xtu9x8m6HI9J_nvoC4nI8BkSKE/css/253_the_village_moscow_1_1343.min.css
### Run rails
def sass(options = {})
options[:syntax] ||= :scss
options[:cache_location] ||= '/tmp/.sass-cache'
options[:style] ||= :compressed
Sass::Plugin.options.merge!(options)
end
def generate_migration(table_name, field_name, data_type, length)
return "rails generate migration add_#{field_name}_to_#{table_name} #{field_name}:#{data_type}(#{length})"
end
generate_migration('grid_posts', 'editor_version', 'string', '255')
def remote_url_to_attachment(url, model, field)
return unless url.present?
tmp_file = download_remote_file(url)
blob = ActiveStorage::Blob.create_after_upload!(io: tmp_file, filename: File.basename(url))
# attach by model.field.attach
model.field.attach(blob)
end
def content_type(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.path)['Content-Type']
end
end
content_type(url)
O(N)
def add(a, b)
return a + b
end
O(N)
O(1)
--------------------------
time complexity of the code:
def body_with_paywall_placeholder
return body unless body.include?(Grid::Core::Post::PAYWALL_PLACEHOLDER)
Nokogiri::HTML.fragment(%(
#{body.split(Grid::Core::Post::PAYWALL_PLACEHOLDER)[0]}
<span class="paywall-placeholder">This is paywalled content</span>
)).to_html
end
require "erb"
class Svg < ERB
def initialize(filename)
@filename = filename
end
def render(custom_color)
ERB.new(File.read(@filename)).result(binding)
end
end
custom_color = "rgb(74, 0, 255)"
puts Svg.new("./my_svg.erb").render(custom_color)
require 'open-uri'
File.readlines("urls.txt").each do |url|
content = open("https://ceditor.setka.io/clients/v8oYJ0xtu9x8m6HI9J_nvoC4nI8BkSKE/css/253_the_village_moscow_1_1343.min.css").read
File.open("downloaded/[url]", "w") { |file| file.write(content) }
end
class RgbColor {
constructor(red: number, green: number, blue: number) {
this.red = red;
this.green = green;
this.blue = blue;
}
public red: number;
public green: number;
public blue: number;
}
const color = new RgbColor(74, 0, 255);
# rubocop:disable Metrics/BlockLength
def index
respond_to do |format|
format.html
format.json do
render json: @courses
end
end
end
# rubocop:enable Metrics/BlockLength
Rails.application.config.assets.css_compressor = lambda do |filename, source|
filename.include?('.remove-comments') ? CssCompressor.compress(source) : source
end
def get_full_name(firstname, middlename, lastname)
return "#{firstname} #{middlename} #{lastname}"
end
get_full_name("John", "Quincy", "Public")
du -a | sort -n -r | head -n 10
def sass_options(options = {})
options = {
:syntax => :scss,
:load_paths => [stylesheets_path],
:cache => true,
:style => :compressed,
:load_paths => [stylesheets_path]
}.merge(options)
end
O(n)
require 'open-uri'
def download_file(url)
download = open(url)
IO.copy_stream(download, "/tmp/file")
end
require 'net/http'
uri = URI.parse('http://www.rubyinside.com/test.txt')
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.get(uri.path)
puts response['Content-Type']
end
creates a new temp file. The name of the file will be the same as the original filename.
def add(a, b):
return a + b
def generate_url(user, password, url)
return "http://#{user}:#{password}@#{url}"
end
generate_url('john', '1234', 'google.com')
<svg viewBox="0 0 29 29" {...props}>
<g fillRule="evenodd">
<circle cx="14.5" cy="14.5" r="14.5" fill="#F34910"/>
<path fill="#C33D11" d="M13 9h3v11h-3z"/>
<path fill="#C33D11" d="M20 13v3H9v-3z"/>
</g>
</svg>
def color(r, g, b):
return "rgb(#{r}, #{g}, #{b})"
color(74, 0, 255)
def add(a, b)
return a + b
end
def find_files_from_github
files = []
# some code
files
end
The regex matches email addresses. Note that the dot "." is not interpreted in a special way.
require 'open-uri'
def get_content_type(url)
open(url).content_type
end
def add(a, b)
return a + b
end
callbackSign = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), VERSTKA_SECRET, "#{VERSTKA_API_KEY}#{materialId}#{userId}#{callbackUrl}")
def remove(filename)
@css_files.delete(filename)
end
def get_content_type(url)
response = HTTParty.get(url)
response.headers['content-type']
end
get_content_type('http://www.apple.com')
O(n)
class AddEditorVersionToGridPosts < ActiveRecord::Migration[5.1]
def change
add_column :grid_posts, :editor_version, :string, limit: 191
end
end
def find_public_files(url)
# search in googlequery
# find all public files
# return array of public files
end
<svg viewBox="0 0 29 29" {...props}>
<g fillRule="evenodd">
<circle cx="14.5" cy="14.5" r="14.5" fill="rgb(74, 0, 255)"/>
<path fill="#C33D11" d="M13 9h3v11h-3z"/>
<path fill="#C33D11" d="M20 13v3H9v-3z"/>
</g>
</svg>
# config/initializers/assets.rb
Rails.application.config.assets.configure do |env|
env.js_compressor = Uglifier.new(harmony: true)
# env.css_compressor = :sass
end
Generate
More than just a code generator. A tool that helps you with a wide range of tasks. All in one place.
Function from Description
Text Description to SQL Command
Translate Languages
Generate HTML from Description
Code to Explanation
Fix invalid Code
Get Test for Code
Class from Description
Regex from Description
Regex to Explanation
Git Command from Description
Linux Command
Function from Docstring
Add typing to code
Get Language from Code
Time complexity
CSS from Description
Meta Tags from Description
− sponsored −