Class: Logan::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/logan/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(basecamp_id, auth_hash, user_agent) ⇒ Client

Initializes the Logan shared client with information required to communicate with Basecamp

Parameters:

  • basecamp_id (String)

    the Basecamp company ID

  • auth_hash (Hash)

    authorization hash consisting of a username and password combination (:username, :password) or an access_token (:access_token)

  • user_agent (String)

    the user-agent string to include in header of requests



12
13
14
15
16
# File 'lib/logan/client.rb', line 12

def initialize(basecamp_id, auth_hash, user_agent)
  self.class.base_uri "https://basecamp.com/#{basecamp_id}/api/v1"
  self.class.headers 'User-Agent' => user_agent
  self.auth = auth_hash
end

Instance Method Details

#auth=(auth_hash) ⇒ Object

Updates authorization information for Logan shared client

Parameters:

  • auth_hash (Hash)

    authorization hash consisting of a username and password combination (:username, :password) or an access_token (:access_token)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logan/client.rb', line 21

def auth=(auth_hash)
  # symbolize the keys
  new_auth_hash = {}
  auth_hash.each {|k, v| new_auth_hash[k.to_sym] = v}
  auth_hash = new_auth_hash
  
  if auth_hash.has_key? :access_token
    # clear the basic_auth, if it's set
    self.class.default_options.reject!{ |k| k == :basic_auth }
            
    # set the Authorization headers
    self.class.headers.merge!("Authorization" => "Bearer #{auth_hash[:access_token]}")
  elsif auth_hash.has_key?(:username) && auth_hash.has_key?(:password)
    self.class.basic_auth auth_hash[:username], auth_hash[:password]
    
    # remove the access_token from the headers, if it exists
    self.class.headers.reject!{ |k| k == "Authorization" }
  else
    raise """
    Incomplete authorization information passed in authorization hash. 
    You must have either an :access_token or a username password combination (:username, :password).
    """
  end
end

#events(since_time, page = 1) ⇒ Object



71
72
73
74
# File 'lib/logan/client.rb', line 71

def events(since_time, page = 1)
  response = self.class.get "/events.json?since=#{since_time.to_s}&page=#{page}"
  response.map { |h| e = Logan::Event.new(h) }
end

#projectsArray<Logan::Project>

get projects from Basecamp API

Returns:



49
50
51
52
# File 'lib/logan/client.rb', line 49

def projects
  response = self.class.get '/projects.json'
  response.parsed_response.map { |h| p = Logan::Project.new(h) }
end

#todolistsArray<Logan::TodoList>

get active Todo lists for all projects from Basecamp API

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/logan/client.rb', line 57

def todolists
  response = self.class.get '/todolists.json'
  
  response.parsed_response.map do |h| 
    list = Logan::TodoList.new(h)
    
    # grab the project ID for this list from the url
    list.project_id = list.url.scan( /projects\/(\d+)/).last.first
    
    # return the list so this method returns an array of lists
    list
  end
end