Class: Basecampeverest::Connect

Inherits:
Object
  • Object
show all
Includes:
HTTParty, JSON
Defined in:
lib/basecampeverest/connect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basecamp_id, authorization, user_agent) ⇒ Connect

Initializes the connection to Basecamp using httparty.

Parameters:

  • basecamp_id (String)

    the Basecamp company ID

  • authorization (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



70
71
72
73
74
75
76
77
# File 'lib/basecampeverest/connect.rb', line 70

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

  # end method
end

Instance Attribute Details

#authorizationObject

Set a few variables



63
64
65
# File 'lib/basecampeverest/connect.rb', line 63

def authorization
  @authorization
end

#base_uriObject

Set a few variables



63
64
65
# File 'lib/basecampeverest/connect.rb', line 63

def base_uri
  @base_uri
end

#basecamp_idObject

Set a few variables



63
64
65
# File 'lib/basecampeverest/connect.rb', line 63

def basecamp_id
  @basecamp_id
end

#headersObject

Set a few variables



63
64
65
# File 'lib/basecampeverest/connect.rb', line 63

def headers
  @headers
end

#user_agentObject

Set a few variables



63
64
65
# File 'lib/basecampeverest/connect.rb', line 63

def user_agent
  @user_agent
end

Instance Method Details

#auth=(authorization) ⇒ Object

Sets the authorization information. Need to

Parameters:

  • authorization (Hash)

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/basecampeverest/connect.rb', line 83

def auth=(authorization)
  clensed_auth_hash = {}
  authorization.each {|k, v| 
    clensed_auth_hash[k.to_sym] = v
    }

  # nice and pretty now
  authorization = clensed_auth_hash
  
if authorization.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 #{authorization[:access_token]}")

elsif authorization.has_key?(:username) && authorization.has_key?(:password)

  # ... then we pass it off to basic auth
  self.class.basic_auth authorization[:username], authorization[:password]
  
  # check if the user tried passing in some other stupid stuff.
  # this should never be the case if the user follows instructions. 
  self.class.headers.reject!{ |k, v| k == "Authorization" }

else
  # something inportant is missing if we get here. 
  raise "Incomplete Authorization hash. Please check the Authentication Hash."

  #end else 
end


# end method
end