Class: Logan::Client

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

Instance Method Summary collapse

Methods included from ResponseHandler

#handle_error, #handle_response, #success?

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



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

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)



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

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, v| 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



84
85
86
87
# File 'lib/logan/client.rb', line 84

def events(since_time, page = 1)
  response = self.class.get "/events.json?since=#{URI.escape since_time.to_formatted_s(:iso8601)}&page=#{page}"
  handle_response(response, Proc.new {|h| Logan::Event.new(h) })
end

#peopleObject



89
90
91
92
# File 'lib/logan/client.rb', line 89

def people
  response = self.class.get "/people.json"
  handle_response(response, Proc.new {|h| Logan::Person.new(h) })
end

#person(id) ⇒ Object



94
95
96
97
98
99
# File 'lib/logan/client.rb', line 94

def person(id)
  response = self.class.get  "/people/#{id}.json"
  person = Logan::Person.new response
  person.json_raw = response.body
  person
end

#project_templatesArray<Logan::ProjectTemplate>

get project templates from Basecamp API

Returns:



61
62
63
64
# File 'lib/logan/client.rb', line 61

def project_templates
  response = self.class.get '/project_templates.json'
  handle_response(response, Proc.new {|h| Logan::ProjectTemplate.new(h) })
end

#projects(id = nil) ⇒ Array<Logan::Project>

get projects from Basecamp API

Returns:



50
51
52
53
54
55
56
# File 'lib/logan/client.rb', line 50

def projects(id = nil)
  if id
    project_by_id id
  else
    all_projects
  end
end

#todolistsArray<Logan::TodoList>

get active Todo lists for all projects from Basecamp API

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logan/client.rb', line 69

def todolists
  response = self.class.get '/todolists.json'
  handle_response(response,
    Proc.new { |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