lib/researchable/freedcamp_api/sessions/signed_session.rb in researchable-freedcamp-api-0.1.5 vs lib/researchable/freedcamp_api/sessions/signed_session.rb in researchable-freedcamp-api-0.1.6

- old
+ new

@@ -21,20 +21,17 @@ attr_reader :public_key sig { returns(String) } attr_reader :secret_key - sig { returns(T::Boolean) } - attr_reader :use_cache - # rubocop:enable Style/AccessorGrouping - sig { params(public_key: String, secret_key: String, use_cache: T::Boolean).void } - def initialize(public_key: ENV['PUBLIC_KEY'], secret_key: ENV['SECRET_KEY'], use_cache: ENV['USE_CACHE']) + sig { params(public_key: String, secret_key: String).void } + def initialize(public_key: ENV['PUBLIC_KEY'], secret_key: ENV['SECRET_KEY']) @public_key = public_key @secret_key = secret_key - @use_cache = use_cache == 'true' + @debug = false end sig { override.params(path: String).returns(T::Hash[T.untyped, T.untyped]) } def get(path) call_url(path, :get) @@ -66,36 +63,25 @@ T::Hash[T.untyped, T.untyped] ) ).returns(T::Hash[T.untyped, T.untyped]) end def call_url(path, method, data = {}) - cache_url = "output/#{Digest::SHA1.hexdigest(path + method.to_s)}" - content = if data == {} && File.exist?(cache_url) && @use_cache - render_cache(cache_url) - else - render_call(path, method, data, cache_url) - end + content = render_call(path, method, data) content = JSON.parse(content) if content content end - def render_cache(cache_url) - file = File.open(cache_url) - content = file.read - end - - def render_call(path, method, data, cache_url) + def render_call(path, method, data) signature = generate_signature url = make_url(path, signature) data = serialize_data(data) - content = HTTParty.send(method, - url, - body: data, - headers: HEADERS).body - @use_cache && File.open(cache_url, 'w') { |f| f.write content } - content + puts url if @debug + HTTParty.send(method, + url, + body: data, + headers: HEADERS).body end def serialize_data(data = {}) return {} if data == {} @@ -105,10 +91,13 @@ sig { params(path: String, signature: Signature).returns(String) } def make_url(path, signature) result = BASE + path sep = result.include?('?') ? '&' : '?' - result + "#{sep}api_key=#{signature.api_key}&timestamp=#{signature.timestamp}&hash=#{signature.calculated_hash}" + result += "#{sep}api_key=#{signature.api_key}&timestamp=#{signature.timestamp}&hash=#{signature.calculated_hash}" + result = result.gsub(/\n\s+/, '') + puts result if @debug + result end sig { returns(Signature) } def generate_signature time = Time.new.to_i * 1000