Module: WhenIWork::Request
- Included in:
- Client
- Defined in:
- lib/wheniwork/request.rb
Instance Method Summary collapse
- #auth_params ⇒ Object
- #cache_enabled ⇒ Object
- #cache_key_for(uri, params) ⇒ Object
- #cache_store ⇒ Object
- #connection ⇒ Object
- #default_options ⇒ Object
- #default_request_params ⇒ Object
- #endpoint ⇒ Object
- #get(path, params = {}, options = {}) ⇒ Object
- #login ⇒ Object
- #post(path, params = {}, options = {}) ⇒ Object
- #request(method, path, params, cache_options) ⇒ Object
- #token ⇒ Object
Instance Method Details
#auth_params ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/wheniwork/request.rb', line 74 def auth_params { username: WhenIWork.configuration.username, password: WhenIWork.configuration.password, key: WhenIWork.configuration.api_key } end |
#cache_enabled ⇒ Object
90 91 92 |
# File 'lib/wheniwork/request.rb', line 90 def cache_enabled WhenIWork.configuration.cache_enabled end |
#cache_key_for(uri, params) ⇒ Object
38 39 40 41 |
# File 'lib/wheniwork/request.rb', line 38 def cache_key_for(uri, params) params[:uri] = uri ::Marshal.dump(params) end |
#cache_store ⇒ Object
86 87 88 |
# File 'lib/wheniwork/request.rb', line 86 def cache_store WhenIWork.configuration.cache_store end |
#connection ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/wheniwork/request.rb', line 47 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 ⇒ Object
43 44 45 |
# File 'lib/wheniwork/request.rb', line 43 def { expires_in: WhenIWork.configuration.expires_in } end |
#default_request_params ⇒ Object
56 57 58 |
# File 'lib/wheniwork/request.rb', line 56 def default_request_params { "W-Token" => token } end |
#endpoint ⇒ Object
60 61 62 |
# File 'lib/wheniwork/request.rb', line 60 def endpoint WhenIWork.configuration.endpoint end |
#get(path, params = {}, options = {}) ⇒ Object
4 5 6 |
# File 'lib/wheniwork/request.rb', line 4 def get(path, params={}, ={}) request :get, path, default_request_params.merge(params), end |
#login ⇒ Object
82 83 84 |
# File 'lib/wheniwork/request.rb', line 82 def login connection.post('login', auth_params.to_json).body end |
#post(path, params = {}, options = {}) ⇒ Object
8 9 10 |
# File 'lib/wheniwork/request.rb', line 8 def post(path, params={}, ={}) request :post, path, default_request_params.merge(params), end |
#request(method, path, params, cache_options) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wheniwork/request.rb', line 12 def request(method, path, params, ) json_params = params.to_json if cache_enabled key = .delete(:key) || cache_key_for(path, params) = .merge() cache_store.fetch(key, ) do if method == :post puts json_params.to_s response = Faraday.post do |req| req.url endpoint+'/'+path req.headers['Content-Type'] = 'application/json' req.headers['W-Token'] = token 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 ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/wheniwork/request.rb', line 64 def token if cache_enabled cache_store.fetch('wheniwork_token', ) do login['token'] end else login['token'] end end |