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)
-
- (Object) id
Returns the value of attribute id.
-
- (Object) password
writeonly
Sets the attribute password.
-
- (Object) token
Returns the value of attribute token.
-
- (Object) token_secret
Returns the value of attribute token_secret.
-
- (Object) username
Returns the value of attribute username.
Instance Method Summary (collapse)
-
- (String) api_url(str)
Creates a Basecamp API URL prepopulated with needed values.
-
- (Faraday::Connection) connection
Create Faraday connection with parameters from #setup.
-
- (Faraday::Response) delete(url, headers = {})
DELETE request for an API url with default headers.
-
- (Faraday::Response) get(url, headers = {})
GET request for an API url with default headers.
-
- (Faraday::Response) post(url, body = {}, headers = {})
POST request for an API url with default headers.
-
- (Faraday::Response) put(url, body = {}, headers = {})
PUT request for an API url with default headers.
-
- (true, false) setup(args)
Sets authentication details and creates Faraday::Connection object.
-
- (Hash) status
Fetch API status from 37signals.
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
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
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
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
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
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
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
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
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
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 |