If you would like to implement a request api stub, you have to consider WebMock first. It is very useful basically. At this time, I got confused about how to send a request with queries as a stub, so I’m going to take a note.

webmock

What if we want to send a request with queries?

For example, it is

https://example.com/pages/1?access_token="abcdefg"

like this.

Even if you write like below,

stub_request(:post, "https://example.com/pages/1?access_token="abcdefg").to_return(status: 200)

it won’t work.

We have to do like this.

stub_request(:post, "https://example.com/pages/1").with(query: hash_including({ 'access_token' => "abcdefg" })).to_return(status: 200)

It will work!

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/


We sometimes implement class method in order to express that the method is related to the class not instances. However, the way to define class methods often makes codes more complicated. I have come up with an idea.

Example

class RegisterUser
class << self
def perform!
# some codes...
end
end
end

There might be codes above, but I don’t like ‘class << self’. I want to hide those codes.

The way

class RegisterUser
include Base
def perform!
# some codes...
end
end

and

module Base  extend ActiveSupport::Concern  class_methods do
def perform!(*args, &block)
@instance ||= new
@instance.perform!(*args, &block)
end
end
def…

Recently, I have developed our software’s function of authentication from other services. it was interesting to make cookie and token authentication functions, so I write it down .

Cookie

A user logs in a service for the first time, the server provides the user with session id and saves it. Moreover, the user’s client keeps session id and send it to the server every time the client sends a request message to the server. The server authenticates the user by verifying that the sent session id is the same as saved session id. This way is stateful.

Token

As the cookie does…


When I took a review from other engineers, I knew it. In order not to forget about it, I’ll take a note.

The document

When we use it

Render method in react demands using one tag at least like below.

render() {
return (<div>hello world</div>)
} // works
render() {
return (hello world)
} # not works

Basically, we can deal with a lot of situation with this tag ‘div’. However, sometimes it is difficult to do that. The example is below.

class Table extends React.Component {
render() {
return (
<table>
<tr>
<Columns />
</tr>
</table>
);
}
}

and

class Columns extends React.Component {…

I tried out writing Euclidean algorithm by myself with Ruby. The code is below.

# n > n2def euclid(n, n2)  answer = n.divmod(n2)  answer[0] = (n - answer[1])/answer[0]  if answer[1] == 0    answer[0]  else    euclid(answer[0], answer[1])  endend

We can make a performance of the computer better with this algorithm, when getting GCD of two numbers. Even if n2’s digits are 100, the calculation times are less than 500(Lame’s theorem).

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/


I have had an opportunity to develop an API login function between two web services. The way to do that is a little complex and demanding, so I’ve decided to take a not about necessary knowledge.

Gems

This is a gem by which you can develop an API feature easily. Moreover, I used this gem for oauth.

That’s all.

System

With these gems, user can be given a permission, which means an access token, from an authorization server. This system is defined as RFC6749. The details is here.

Wrap up

You can make an API feature with the knowledge above. …


Sometimes, we see a column which is a “flag”. For example, “status”, “deleted”, and “is_changed”. If their types are boolean, you shouldn’t use as_ boolean input option for the columns.

Because the value is “0” or “1”, which means the type is number. We have to change the numbers into booleans somewhere. It leads to a bug. Actually, the code caused a bug of Stripe(https://stripe.com/en-jp) in our project.

Instead of that, we have to write codes like below.

<%= f.input :published, checked_value: :true, unchecked_value: :false %>

If you want to use “as_boolean”, the type of the column should be number.

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/


I tried out ruby ver 2.7, and found there was a new function.

Time.now 
#=> 2021-04-11 14:43:57.854776

Ms was added. I couldn’t understand that someone said this was handy. Moreover, because in my project I wanted to remove this ms, I looked for the way.

It is below.

Time.now.change(:usec => 0)
#=> 2021-04-11 14:49:23

I managed to remove the ms!

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/


Recently, I’ve gotten interested in assembly language. The purpose is to understand programming more deeply. At this time, I decided to use nasm because it is easy to install. The environment is macOS.

Preparation

$ brew install nasm$ touch assemble.asm

The code

global    _mainsection   .text
_main: mov rax, 0x02000004
mov rdi, 1
mov rsi, message
mov rdx, 13
syscall
mov rax, 0x02000001
xor rdi, rdi
syscall
section .data
message: db "Hello, World", 10

Output

$ nasm -fmacho64 assemble.asm
$ ld assemble.o -lSystem -o assemble
$ ./assemble
Hello, World

Explanation

・global: this file’s code can be used from other object files.

・main: entry point


When I was writing codes of my company’s application, I faced this problem. First of all, I’m going to show you the structure of the tables.

The structure of the tables in our project

The chart is below.

Tomoharu Tsutsumi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store