Ruby on Rails を使用したアプリケーション API

APIとは何ですか? ROR で API を使用する理由
An API is an application programing interface. It’s an interface between inside and outside where outside interact with inside using codes. So the inside could be a server or devise like mobile, IPad, software libraries like Ruby on Rails, JQuery etc., interacting with HTTP through the web.
API’s are built as a component of the application. Basically we need to have a web API if we want to store and process our data in server and let any other application (web application, mobile application, iOS devices etc.) request and get data from that server if needed. These other applications can be part of your system or you can open your data for others to use.

API を構築する利点
カスタマイズされた UI を構築するために、フロントエンドを Angular JS、Ember JS、またはその他の一般的なクライアント フレームワークに分割できます。

利点:

  • 柔軟性が高い
  • メンテナンスコストの削減
  • より高い拡張性
  • シンプルさ
  • API 開発により、他のユーザー インターフェイスを簡単に統合することもできるため、時間を大幅に節約できます。

APIを構築するデメリット

  • コーダーは冗長な情報を含むメッセージを繰り返し送信するため、帯域幅が増加します。
  • 無視できる遅延
  • ヘッダーの使用は不器用で、API を HTTP レスポンスに結び付けます。

RailsのREST API
Representational state Transfer convention for Hadoop developed in Web.is the software architectural style for the web. The architecture is used for giving flexibility to retrieve data through some endpoints. It runs over HTTP protocol.

How does one makes an API REQUEST?
URL = HTTP, HOSTNAME, URL
E.g. URL: -https://portal.ehealthpro.com/blogs

Protocol  SERVER  URL

Each resource has two URI’s, one represents the collections of the list i.e., https://portal.ehealthpro.com/blogs and other represents the item, i.e. it represents the single item in that collection  https://portal.ehealthpro.com/blogs/12
For building an API, We can use the Gem called “gem rails-api”
https://github.com/rails-api/rails-api , Generally, API needs to respond to uniform resource identifier (URI) and HTTTP. So,

In Routes,

Rails Development 1

名前空間

Rails generator create a Userscontroller and related routes specs. In the above routes, by defining a namespace under the routes.rb file, Rails will automatically map that namespace to an index coordinating the name under the controllers folder, the api/ directory. And the response we are acheiving via JSON format, so we just need to specify this format as the default one. Setting the API under a subdomain due to escalate the application to a DNS level.
And path option is to set the starting path for any request to be root in relation to the subdomain.

バージョン管理

Versioning the  application provides the basic structure to the API, and also when changes needs to be  done then we  can give developers the opportunity to adapt for the new features while old ones are being deprecated. In order to set the version for the API, we first need to add another directory under the API we created i.e. “v1”.we can add scope our API into different versions according to our convenience.

Scope module: :v1,
Constraints: ApiConstraints.new(version: 1, default: true) do
………
End

コントローラーの生成
Command use to generate the User controller, rails generate controller API:: v1::users

In controller we need to do the requests respond with JSON data, Technically JSON is a lightweight data-interchange format, a collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
In rails, there is a gem called Active model serializer for implementing the JSON response. By using this gem we will get the proper format of the JSON.
Active Model Serializer is a great way to build JSON responses using an object oriented approach. The objects have a very similar feel on how your ActiveModel object is set up in terms of attributes and relationships. It likewise permits you to pick your connector to decide what type of JSON structure is produced-or to build your own.

HTTP Status code, is just way to uniformly tell the person what the respond is, whether the response shows error or not.
And some of the status code are,
200 – OK , Successful Get
204 – No content, Successful Delete or Post
201 – Created – Successful Post
422 – Unprocessable Entity
401- Unauthorized, Authentication Failure

安全
For the security reason in our API application, we can use HTTPS,
Rails.application.configure do
config.force_ssl = true
終わり

最後に、API をテストします。
comand line: – curl or htttpie
Browser: – Postman, JSON-Formatter
Rspec :- Request Specs
Rails Minitest :- Integration tests

レールカーマ 過去 8 年間、Ruby on Rails の開発およびコンサルティング サービスを提供してきました。当社の開発者は、あらゆる種類の開発に精通しています。 Ruby on Railsの開発作業 あらゆる種類のプロジェクトを処理します。 お問い合わせ 当社の開発スキルと当社が対応できるプロジェクトについて詳しく知るため。 

最新のアップデートを購読する

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

jaJapanese