Module: Camper::Connection

Included in:
Client
Defined in:
lib/camper/connection.rb

Constant Summary

DEFAULT_HEADERS =

Default headers used for Camper requests

{ 
  'User-Agent' => "camper/#{Camper::VERSION} (evan@massivedanger.com)"
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) id

Returns the value of attribute id



7
8
9
# File 'lib/camper/connection.rb', line 7

def id
  @id
end

- (Object) password=(value) (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



8
9
10
# File 'lib/camper/connection.rb', line 8

def password=(value)
  @password = value
end

- (Object) token

Returns the value of attribute token



7
8
9
# File 'lib/camper/connection.rb', line 7

def token
  @token
end

- (Object) token_secret

Returns the value of attribute token_secret



7
8
9
# File 'lib/camper/connection.rb', line 7

def token_secret
  @token_secret
end

- (Object) username

Returns the value of attribute username



7
8
9
# File 'lib/camper/connection.rb', line 7

def username
  @username
end

Instance Method Details

- (String) api_url(str)

Creates a Basecamp API URL prepopulated with needed values

Examples:

Regular usage

api_url("people") # => /1234567/api/v1/people.json

GET params

api_url(["people", { page: 1, status: 'active' }]) # => /1234567/api/v1/people.json?page=1&status=active

Parameters:

  • str (String, Array)

    The URL string (or URL array if GET params are needed)

Returns:

  • (String)

    the URL



91
92
93
94
95
96
97
# File 'lib/camper/connection.rb', line 91

def api_url(str)
  if str.is_a? String
    "#{@id}/api/v1/#{str}.json"
  elsif str.is_a? Array
    "#{@id}/api/v1/#{str[0]}.json?".concat(URI.escape(str[1].collect{|k,v| "#{k}=#{v}"}.join('&')))
  end
end

- (Faraday::Connection) connection

Create Faraday connection with parameters from #setup

Returns:

  • (Faraday::Connection)

    Connection object from Faraday



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/camper/connection.rb', line 102

def connection
  @conn = Faraday.new(url: "https://basecamp.com") do |c|
    c.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
    c.use Faraday::Request::JSON
    c.adapter Faraday.default_adapter

    if @token && @token_secret
      c.request :oauth, {
        consumer_key:     "61701fd835eab3546de0effc5533cced8ae3908a",
        consumer_secret:  "03213633c75c33f7593fa47ebe398b8240cb609e",
        token: @token,
        token_secret: @token_secret
      }
    end
  end

  if @username && @password
    @conn.basic_auth @username, @password
  end

  @conn
end

- (Faraday::Response) delete(url, headers = {})

DELETE request for an API url with default headers

Parameters:

  • url (String)

    URL fed to #api_url

  • headers (Hash) (defaults to: {})

    Headers used for request

Returns:

  • (Faraday::Response)

    Response from Faraday::Connection



76
77
78
# File 'lib/camper/connection.rb', line 76

def delete(url, headers = {})
  @conn.delete api_url(url), DEFAULT_HEADERS.merge(headers)
end

- (Faraday::Response) get(url, headers = {})

GET request for an API url with default headers

Parameters:

  • url (String)

    URL fed to #api_url

  • headers (Hash) (defaults to: {})

    Headers used for request

Returns:

  • (Faraday::Response)

    Response from Faraday::Connection



44
45
46
# File 'lib/camper/connection.rb', line 44

def get(url, headers = {})
  @conn.get api_url(url), DEFAULT_HEADERS.merge(headers)
end

- (Faraday::Response) post(url, body = {}, headers = {})

POST request for an API url with default headers

Parameters:

  • url (String)

    URL fed to #api_url

  • body (Hash) (defaults to: {})

    data sent via POST

  • headers (Hash) (defaults to: {})

    Headers used for request

Returns:

  • (Faraday::Response)

    Response from Faraday::Connection



55
56
57
# File 'lib/camper/connection.rb', line 55

def post(url, body = {}, headers = {})
  @conn.post api_url(url), body, DEFAULT_HEADERS.merge(headers)
end

- (Faraday::Response) put(url, body = {}, headers = {})

PUT request for an API url with default headers

Parameters:

  • url (String)

    URL fed to #api_url

  • body (Hash) (defaults to: {})

    data sent via PUT

  • headers (Hash) (defaults to: {})

    Headers used for request

Returns:

  • (Faraday::Response)

    Response from Faraday::Connection



66
67
68
# File 'lib/camper/connection.rb', line 66

def put(url, body = {}, headers = {})
  @conn.put api_url(url), body, DEFAULT_HEADERS.merge(headers)
end

- (true, false) setup(args)

Sets authentication details and creates Faraday::Connection object

Parameters:

  • args (Hash)

    The settings for the connection to Basecamp's API

Options Hash (args):

  • :id (Fixnum, String)

    the Basecamp group ID

  • :username (String)

    the username for basic authentication

  • :password (String)

    the password for basic authentication

  • :token (String)

    Token for OAuth

  • :token_secret (String)

    Token secret for OAuth

Returns:

  • (true, false)

    If connection is successfully created, returns true. Otherwise, false.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/camper/connection.rb', line 26

def setup(args)
  @id = args[:id]
  @username = args[:username]
  @password = args[:password]
  @token = args[:token]
  @token_secret = args[:token_secret]

  connection

  !!@conn
end

- (Hash) status

Fetch API status from 37signals

Returns:

  • (Hash)

    :description, :mood, and :updated_at



128
129
130
# File 'lib/camper/connection.rb', line 128

def status
  JSON.parse(Faraday.get("http://status.37signals.com/status.json").body)['status']
end