Skip to content

Instantly share code, notes, and snippets.

View nafidurmus's full-sized avatar
🏠
Working from home

Nafi Durmuş nafidurmus

🏠
Working from home
View GitHub Profile
def index
require ‘uri’
require ‘net/http’
url = URI(“https://api.hurriyet.com.tr/v1/articles?$top=20")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(url)
request[“apikey”] = ‘kendi apikeyinizi buraya yapıştırın’
module Api::V1
class BooksController < ApplicationController
before_action :set_book, only: [:show, :update, :destroy]
# GET /books
def index
@books = Book.all
render json: @books
end
class Wand
def cast_spell
spell = get_new_spell
spell.cast!
end
def get_new_spell
raise "abstract method #get_new_spell must be defined"
end
end
# Prototype Pattern kullanmadan
class ProductDefinition
def create_product
@product = Product.new(
product_definition_name: self.name,
properties: self.properties,
active: self.active?,
category: self.category
)
end
class RestaurantServer
def initialize(meal_factory)
@meal_factory = meal_factory
end
def serve!
@meal_factory.new_utensil
@meal_factory.new_dish
end
class EventDirector
def initialize
@subevents = [:welcome, :intermediary,:closing]
end
def organize event
@subevents.each { |se| event.send(se) if event.respond_to? se }
end
end
class Wedding
require 'singleton'
class SingletonObject
include Singleton
attr_accessor :counter
def initialize
@counter = 0
end
end
# app/lib/json_web_token.rb
class JsonWebToken
SECRET_KEY = Rails.application.secrets.secret_key_base.to_s
def self.encode(payload, exp = 24.hours.from_now)
payload[:exp] = exp.to_i
JWT.encode(payload, SECRET_KEY)
end
def self.decode(token)
begin
@decoded_token = JWT.decode(token, SECRET_KEY)[0]
class ApplicationController < ActionController::API
def authorize_request
header = request.headers['Authorization']
if !header.nil?
header = header.split(' ').last
@decoded_token ||= JsonWebToken.decode(header)
begin
@user = User.find(@decoded_token[:id])
rescue Exception => e
module Api::V1
class UsersController < ApplicationController
before_action :authorize_request, except: [:create, :show, :index, :destroy, :update]
# GET /users
def index
@users = User.all
render json: @users, status: :ok
end