Module: TypekitClient::HttpRequest
- Defined in:
- lib/typekit_client/http_request.rb
Constant Summary collapse
- BASE_URI =
'https://typekit.com/api/v1'- REQUEST_TYPE =
This allow us to not pass strings to determine the request type.
{ :GET => Net::HTTP::Get, :POST => Net::HTTP::Post, :DELETE => Net::HTTP::Delete }
Class Method Summary collapse
-
.execute_request(request) ⇒ Object
Private: Makes http calls with given request.
-
.request(uri_string, type, format, body = nil) ⇒ Object
Public: This function makes network calls to the Typekit API depending on the parameters passed and returns the response from the server.
Class Method Details
.execute_request(request) ⇒ Object
Private: Makes http calls with given request. It can be any http request such as get, post, delete.
request- request instance such as Net::HTTP::Get.new(), Net::HTTP::POST.new()
Returns the response object Raises error when there is a timeout issue or internet connection is down
61 62 63 64 65 66 |
# File 'lib/typekit_client/http_request.rb', line 61 def self.execute_request(request) http = Net::HTTP.new(request.uri.host, request.uri.port) http.use_ssl = true if request.uri.to_s.include? 'https' http.request(request) end |
.request(uri_string, type, format, body = nil) ⇒ Object
Public: This function makes network calls to the Typekit API depending on the parameters passed and returns the response from the server. The function can make 3 types of HTTP calls including get, post, delete
uri_string- The uri string to be appended to the base uri type- the type of HTTP request to make format- format of the response data body- hash to set as request body for POST requests
Example:
request('kits',:GET, 'json', {kits: ''})
=> # Net::HTTPResponse object
It returns the response from the request if there are no runtime errors
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/typekit_client/http_request.rb', line 33 def self.request(uri_string, type, format, body=nil) request = REQUEST_TYPE[type].new(URI.parse("#{BASE_URI}/#{format}/#{uri_string}")) request['X-Typekit-Token'] = TypekitClient::TOKEN_CACHE.get("token") request.body = Rack::Utils.build_nested_query(body) if body response = execute_request(request) # clear the cache if the response code is not 401(unauthorized) TypekitClient::TOKEN_CACHE.clear if response.code == '401' return response rescue SocketError puts "Socket Error, Please check your internet connection.".red rescue Net::OpenTimeout puts "The request timed out when trying to connect, please check your connection.".red end |