class Hatenablog::Requester::Basic
Constants
- METHODS
Public Class Methods
new(user_id, api_key)
click to toggle source
Create a new Basic
authentication requester. @params [string] user_id Hatena user ID @params [string] api_key Hatena API key
# File lib/hatenablog/requester.rb, line 82 def initialize(user_id, api_key) @user_id = user_id @api_key = api_key end
Public Instance Methods
delete(uri, headers = {})
click to toggle source
HTTP DELETE method @param [string] uri target URI @param [string] headers HTTP request headers @return [Net::HTTPResponse] HTTP response
# File lib/hatenablog/requester.rb, line 116 def delete(uri, headers = {}) request(uri, :delete, headers: headers) end
get(uri)
click to toggle source
HTTP GET method @param [string] uri target URI @return [Net::HTTPResponse] HTTP response
# File lib/hatenablog/requester.rb, line 90 def get(uri) request(uri, :get) end
post(uri, body, headers = {})
click to toggle source
HTTP POST method @param [string] uri target URI @param [string] body HTTP request body @param [string] headers HTTP request headers @return [Net::HTTPResponse] HTTP response
# File lib/hatenablog/requester.rb, line 99 def post(uri, body, headers = {}) request(uri, :post, body: body, headers: headers) end
put(uri, body, headers = {})
click to toggle source
HTTP PUT method @param [string] uri target URI @param [string] body HTTP request body @param [string] headers HTTP request headers @return [Net::HTTPResponse] HTTP response
# File lib/hatenablog/requester.rb, line 108 def put(uri, body, headers = {}) request(uri, :put, body: body, headers: headers) end
Private Instance Methods
request(uri, method, body: nil, headers: {})
click to toggle source
# File lib/hatenablog/requester.rb, line 122 def request(uri, method, body: nil, headers: {}) uri = URI(uri) req = METHODS[method].new(uri.to_s, headers) req.basic_auth @user_id, @api_key if body req.body = body req.content_type = ATOM_CONTENT_TYPE end http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = uri.port == 443 http.start do |conn| conn.request(req) end rescue => problem raise RequestError, "Fail to #{method.upcase}: " + problem.to_s end