class BaseApi::Client
TODO: 責務でクラス分ける
Attributes
access_token[RW]
client_id[RW]
client_secret[RW]
code[RW]
offset[RW]
refresh_token[RW]
response[RW]
Public Class Methods
new(options = {}) { |self| ... }
click to toggle source
# File lib/base_api/client.rb, line 28 def initialize(options = {}) BaseApi::Configurable.keys.each do |key| value = options.key?(key) ? options[key] : BaseApi.instance_variable_get(:"@#{key}") instance_variable_set(:"@#{key}", value) end yield(self) if block_given? end
Public Instance Methods
fetch_next_page(&callback)
click to toggle source
# File lib/base_api/client.rb, line 48 def fetch_next_page(&callback) paginate(@last_page_args[:path], next_page_payload, &callback) end
inspect()
click to toggle source
Calls superclass method
# File lib/base_api/client.rb, line 37 def inspect inspected = super inspected.gsub! @code, '*******' if @code inspected.gsub! @access_token, "#{'*'*28}#{@access_token[28..-1]}" if @access_token inspected.gsub! @refresh_token, "#{'*'*28}#{@refresh_token[28..-1]}" if @refresh_token inspected.gsub! @client_secret, "#{'*'*28}#{@client_secret[28..-1]}" if @client_secret inspected end
reset_response()
click to toggle source
# File lib/base_api/client.rb, line 52 def reset_response @response = nil @last_page_args = nil end
Private Instance Methods
call_get_api(path, payload = {}, &callback)
click to toggle source
# File lib/base_api/client.rb, line 64 def call_get_api(path, payload = {}, &callback) @response = self.class.get(path, { query: payload, headers: authorization_header }) handle_response(&callback) end
call_post_api(path, payload = {}, &callback)
click to toggle source
# File lib/base_api/client.rb, line 59 def call_post_api(path, payload = {}, &callback) @response = self.class.post(path, { body: payload, headers: authorization_header }) handle_response(&callback) end
check_status()
click to toggle source
# File lib/base_api/client.rb, line 106 def check_status if response.client_error? raise BaseApi::ClientError.new(response) elsif response.server_error? raise BaseApi::ServerError.new(response) end end
different_path_request_called?(path)
click to toggle source
# File lib/base_api/client.rb, line 102 def different_path_request_called?(path) @last_page_args.is_a?(Hash) && @last_page_args[:path] != path end
error(error, &block)
click to toggle source
# File lib/base_api/client.rb, line 119 def error(error, &block) if block_given? block.call(response, error) response else raise error end end
handle_response(&callback)
click to toggle source
# File lib/base_api/client.rb, line 69 def handle_response(&callback) check_status success(response, &callback) rescue => e error(e, &callback) end
next_page_payload()
click to toggle source
# File lib/base_api/client.rb, line 97 def next_page_payload next_offset = @last_page_args[:payload][:offset] + @last_page_args[:payload][:limit] @last_page_args[:payload].merge(offset: next_offset) end
paginate(path, payload = {}, &callback)
click to toggle source
# File lib/base_api/client.rb, line 80 def paginate(path, payload = {}, &callback) if different_path_request_called?(path) reset_response end if payload[:offset].nil? payload.merge!(offset: @offset) end if payload[:limit].nil? payload.merge!(limit: @limit) end @last_page_args = { path: path, payload: payload } call_get_api(path, payload, &callback) end
success(response, &block)
click to toggle source
# File lib/base_api/client.rb, line 114 def success(response, &block) block.call(response, nil) if block_given? response end