[Ruby] Chew on API

2 minute read

Recent report

** I want to gain practical experience! **

I will follow this. Looking at the tweets of tech seniors on Twitter, it can be seen that there is an overwhelming difference between self-study and practice. As the days go by, I get more and more interested in how much difference there is. Since there are no web engineers around me, when I ask about the current situation, the number of PM/CTOs I interview with will increase. However, I can’t say it because I’m likely to be asked a question such as “what is the experience gained by self-education in practice?” As far as I can think, delivery speed, the ability to instantly decide what to use such as a method, ggr Is it power? However, after all, you will not be able to realize unless you experience it side by side.

Today’s theme

“API” A word that I was passing through without understanding at all when I was participating in the tech camp. However, it is a word that often appears 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 translated literally. I understood it as a window to operate 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 considered that the information in the application “youtube” was extracted from outside the application using “ruby”. In this way, the API is the API that can be accessed from outside to process and extract application information. The API here is considered to be an API for getting information about youtube. In this way, the API changes its behavior depending on the purpose.

API for extracting data, API for operating applications, API for synchronizing, API for sorting, etc.

Data or information Wall ← API ← Wall Outside

I see it with the recognition that it is an invisible way to access the inside like. In other words, anyone can manipulate the information in it, and from a security point of view, it is a bad idea, so as a countermeasure, the API of youtube will be issued an API key and can be accessed 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'Today's weather will be' + today_tel +'
puts'The lowest temperature will be' + min_tem +'°C' +'
puts'The maximum temperature will be' + max_tem +'°C' +'
puts'Tomorrow's weather will be' + tomor_tel +'
puts''
puts'weather forecast' .center(60,'-')
puts''
puts''
puts'Weather summary'.center(60,'-')
puts
puts des
puts''
print'' Time of announcement of weather summary''
puts des2.strftime('%Y year %m month %d day %H hour %M minute %S second')
puts''
puts'Weather summary'.center(60,'-')

Here, http://weather.livedoor.com/forecast/webservice/json/v1?city=170010 is accessed and the weather information is extracted and displayed.

in conclusion

Did you get an image? Please tell me if there is a better expression!

Tags:

Updated: