Class: Ketchup::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ketchup/api.rb

Overview

The direct interface with Ketchup’s RESTful API. You should generally not have any need to use this class yourself, unless you’re playing around with the internals. That said, it’s a simple class - with get, post, put and delete instance methods for matching HTTP requests, that automatically include the access token.

For pure access to the API, without any implicit authentication, use the class level methods, which are provided by the mixed-in HTTParty module.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ API

Creates a new API instance for the given profile, as determined by the email and password. This API object can then be used by all other objects to interact with the API without having to re-authenticate.

Parameters:

  • email (String)

    The user’s email address

  • password (String)

    The user’s password

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ketchup/api.rb', line 28

def initialize(email, password)
  response = self.class.get '/profile.json', :basic_auth => {
    :username => email,
    :password => password
  }
  
  if response == 'Access Denied'
    raise Ketchup::AccessDenied
  else
    @access_token = response['single_access_token']
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



18
19
20
# File 'lib/ketchup/api.rb', line 18

def access_token
  @access_token
end

Instance Method Details

#delete(path, data = {}) ⇒ Object

Execute a DELETE request to the API server using the stored access token.

Parameters:

  • path (String)

    The URL path

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

    Parameters for the request

Returns:

  • The web response



77
78
79
# File 'lib/ketchup/api.rb', line 77

def delete(path, data = {})
  self.class.delete path, :body => data.merge(:u => access_token)
end

#get(path, data = {}) ⇒ Object

Execute a GET request to the API server using the stored access token.

Parameters:

  • path (String)

    The URL path

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

    Parameters for the request

Returns:

  • The web response



47
48
49
# File 'lib/ketchup/api.rb', line 47

def get(path, data = {})
  self.class.get path, :query => data.merge(:u => access_token)
end

#post(path, data = {}) ⇒ Object

Execute a POST request to the API server using the stored access token.

Parameters:

  • path (String)

    The URL path

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

    Parameters for the request

Returns:

  • The web response



57
58
59
# File 'lib/ketchup/api.rb', line 57

def post(path, data = {})
  self.class.post path, :body => data.merge(:u => access_token)
end

#put(path, data = {}) ⇒ Object

Execute a PUT request to the API server using the stored access token.

Parameters:

  • path (String)

    The URL path

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

    Parameters for the request

Returns:

  • The web response



67
68
69
# File 'lib/ketchup/api.rb', line 67

def put(path, data = {})
  self.class.put path, :body => data.merge(:u => access_token)
end