module WhenIWork::Request
Public Instance Methods
auth_params()
click to toggle source
# File lib/wheniwork/request.rb, line 72 def auth_params { username: WhenIWork.configuration.username, password: WhenIWork.configuration.password, key: WhenIWork.configuration.api_key } end
cache_enabled()
click to toggle source
# File lib/wheniwork/request.rb, line 88 def cache_enabled WhenIWork.configuration.cache_enabled end
cache_key_for(uri, params)
click to toggle source
# File lib/wheniwork/request.rb, line 36 def cache_key_for(uri, params) params[:uri] = uri ::Marshal.dump(params) end
cache_store()
click to toggle source
# File lib/wheniwork/request.rb, line 84 def cache_store WhenIWork.configuration.cache_store end
connection()
click to toggle source
# File lib/wheniwork/request.rb, line 45 def connection @connection ||= Faraday.new(:url => endpoint) do |faraday| faraday.request :url_encoded faraday.response :json, :content_type => /\bjson$/ faraday.adapter Faraday.default_adapter end end
default_options()
click to toggle source
# File lib/wheniwork/request.rb, line 41 def default_options { expires_in: WhenIWork.configuration.expires_in } end
default_request_params()
click to toggle source
# File lib/wheniwork/request.rb, line 54 def default_request_params { "W-Token" => token } end
endpoint()
click to toggle source
# File lib/wheniwork/request.rb, line 58 def endpoint WhenIWork.configuration.endpoint end
get(path, params={}, options={})
click to toggle source
# File lib/wheniwork/request.rb, line 4 def get(path, params={}, options={}) request :get, path, default_request_params.merge(params), options end
login()
click to toggle source
# File lib/wheniwork/request.rb, line 80 def login connection.post('login', auth_params.to_json).body end
post(path, params={}, options={})
click to toggle source
# File lib/wheniwork/request.rb, line 8 def post(path, params={}, options={}) request :post, path, default_request_params.merge(params), options end
request(method, path, params, cache_options)
click to toggle source
# File lib/wheniwork/request.rb, line 12 def request(method, path, params, cache_options) json_params = params.to_json if cache_enabled key = cache_options.delete(:key) || cache_key_for(path, params) options = default_options.merge(cache_options) cache_store.fetch(key, options) do if method == :post conn = connection response = conn.post do |req| req.url endpoint + path req.headers['Content-Type'] = 'application/json' req.headers['W-Token'] = token.to_s req.body = json_params.to_s end response.body else connection.send(method, path, params).body end end else connection.send(method, path, params).body end end
token()
click to toggle source
# File lib/wheniwork/request.rb, line 62 def token if cache_enabled cache_store.fetch('wheniwork_token', default_options) do login['token'] end else login['token'] end end