Class: IDoneThis::Client
- Inherits:
-
Object
- Object
- IDoneThis::Client
- Includes:
- Connection
- Defined in:
- lib/idonethis/client.rb
Instance Attribute Summary
Attributes included from Connection
Instance Method Summary collapse
- #dones(start_date, end_date = nil) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
- #login! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 |
# File 'lib/idonethis/client.rb', line 10 def initialize(={}) = IDoneThis..merge() Configuration::VALID_OPTIONS_KEYS.each do |key| send("#{key}=", [key]) end end |
Instance Method Details
#dones(start_date, end_date = nil) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/idonethis/client.rb', line 57 def dones(start_date, end_date=nil) start_date = Date.parse(start_date.to_s) end_date = Date.parse((end_date || Date.today).to_s) self.get "dones/?start=#{start_date.to_s}&end=#{end_date.to_s}" end |
#get(path) ⇒ Object
51 52 53 54 55 |
# File 'lib/idonethis/client.rb', line 51 def get(path) req = self.connection.get(path) raise StandardError.new(req.body) unless req.status == 200 req.body end |
#login ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/idonethis/client.rb', line 18 def login begin self.login! rescue return false end true end |
#login! ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/idonethis/client.rb', line 28 def login! base = base_connection login_form = base.get('https://idonethis.com/accounts/login/') login_html = Nokogiri::HTML(login_form.body) login_csrf = login_html.search('form input[@name=csrfmiddlewaretoken]').first["value"] login_creds = { username: self.username, password: self.password, csrfmiddlewaretoken: login_csrf} login_resp = base.post do |request| request.url 'https://idonethis.com/accounts/login/' request.headers['Referer'] = 'https://idonethis.com/accounts/login/' request.body = login_creds end raise ServiceError.new('Unable to login') if login_resp.status != 302 self.connection = base self.connection.url_prefix = URI("https://idonethis.com/api/v3/team/#{self.team}/") login_resp end |