[RUBY] Chew about API

Recent report

** I want to gain practical experience! ** **

I will get this. Even if you look at the tweets of tech seniors on Twitter, you can see that there is an overwhelming difference between self-study and practice. As the days go by, I become more interested in how much difference there is. Since there are no web engineers around me, when it comes to hearing about the current situation, the number of PMs and CTOs interviewed increases. However, I can't say "what is the difference between practical work and self-taught experience?" W I think, delivery speed, ability to instantly decide what to use, ggr Is it power? However, you will not be able to realize it unless you experience it side by side in the field.

Today's topic

「API」 A word that I didn't understand at all when I was attending a tech camp. However, words that often appear when you are job hunting. Chew about "API"

API Referenced site Not an API article but an easy-to-understand article API ・ ・ ・ Application Programming Interface

The surface of application programming. .. .. It is an abbreviation whose meaning cannot be understood even if it is translated literally. I understood it as a window for operating the application from outside the application **.

Concrete example

Using the youtube API, we extracted a video with 100 million views with ruby and output it.

In this case, it is probable that the information in the application "youtube" was extracted from outside the application using "ruby". In this way, the API is a karakuri that can be accessed from the outside to process and extract application information. The API here can be thought of as an API ** for obtaining information on youtube. In this way, the behavior of the API changes depending on the purpose.

API to extract data, API to operate application, API to synchronize, API to sort, etc.

Data or information | Wall ← API ← Wall | Outside

I think of it as a karakuri to access the inside like. In other words, if there is an API, anyone can manipulate the information inside **, so from the viewpoint of security, it is bad, so as a countermeasure, the API key of youtube will be issued and you will be able to access it using it. ..

Practical example (weather forecast)

require 'net/http'
require 'uri'
require 'json'
require 'time'
require 'date'

uri = URI.parse('http://weather.livedoor.com/forecast/webservice/json/v1?city=170010')
json = Net::HTTP.get(uri)
result = JSON.parse(json)
today_tel = result['forecasts'][0]['telop']
tomor_tel = result['forecasts'][1]['telop']
min_tem = result['forecasts'][1]['temperature']['min']['celsius']
max_tem = result['forecasts'][1]['temperature']['max']['celsius']
des = result['description']['text']
des1 = result['description']['publicTime']
des2 = DateTime.parse(des1)

#puts result
puts 'weather forecast'.center(60, '-')
puts ''
puts 'How is the weather today,' + today_tel + 'Would be'
puts 'The lowest temperature is' + min_tem + '℃' + 'Would be'
puts 'The maximum temperature is' + max_tem + '℃' + 'Would be'
puts 'Tomorrow's weather' + tomor_tel + 'Would be'
puts ''
puts 'weather forecast'.center(60, '-')
puts ''
puts ''
puts 'Weather overview'.center(60, '-')
puts des
puts ''
print 'Announcement time of weather summary'
puts des2.strftime('%Y year%m month%d day%H o'clock%M minutes%S seconds')
puts ''
puts 'Weather overview'.center(60, '-')

Here, we access http://weather.livedoor.com/forecast/webservice/json/v1?city=170010 to extract and display weather information.

in conclusion

Did you get an image? Tell me if you have a better expression!

Recommended Posts

Chew about API
About Apache Inference API
About =
About Apache Jena Ontology API
[Introduction to Java] About Stream API
About Gradle's compile, api, implementation, etc.
About Kotlin
About attr_accessor
Articles to learn more about Stream API
About Hinemos
Call API [Call]
About params
About Rails 6
About form_for
About Spring ③
About enum
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About active_hash
About static
About exceptions
About scope
[Maven] About Maven
[For beginners] About lambda expressions and Stream API