Class: IDoneThis::Client

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/idonethis/client.rb

Instance Attribute Summary

Attributes included from Connection

#connection

Instance Method Summary collapse

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(options={})
  options = IDoneThis.options.merge(options)

  Configuration::VALID_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[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

Raises:

  • (StandardError)


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

#loginObject



18
19
20
21
22
23
24
25
26
# File 'lib/idonethis/client.rb', line 18

def 
  begin
    self.login! 
  rescue
    return false
  end

  true
end

#login!Object

Raises:



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

   = base.get('https://idonethis.com/accounts/login/')
   = Nokogiri::HTML(.body)
   = .search('form input[@name=csrfmiddlewaretoken]').first["value"]

   =  { username: self.username, password: self.password, csrfmiddlewaretoken: }

   = base.post do |request|
    request.url 'https://idonethis.com/accounts/login/'
    request.headers['Referer'] = 'https://idonethis.com/accounts/login/'
    request.body = 
  end

  raise ServiceError.new('Unable to login') if .status != 302

  self.connection = base
  self.connection.url_prefix = URI("https://idonethis.com/api/v3/team/#{self.team}/")

  
end