Class: DevinApi::Client

Inherits:
Object
  • Object
show all
Includes:
Endpoints
Defined in:
lib/devin_api/client.rb

Overview

The top-level class that handles configuration and connection to the Devin API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Endpoints

included

Constructor Details

#initialize {|config| ... } ⇒ Client

Creates a new DevinApi::Client instance and yields #config.

Requires a block to be given.

Yields:

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
# File 'lib/devin_api/client.rb', line 27

def initialize
  raise ArgumentError, 'block not given' unless block_given?

  @config = DevinApi::Configuration.new
  yield config

  check_url
  set_access_token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/devin_api/client.rb', line 144

def method_missing(method, *args, &)
  method_name = method.to_s

  if resource_exists?(method_name.singularize)
    resource_class = resource_class_for(method_name.singularize)

    # Special case for 'knowledge' which is both singular and plural
    return Collection.new(self, resource_class, args.first || {}) if method_name == 'knowledge'

    if method_name.singularize == method_name
      id = args.first
      resource_class.new(self, get("#{resource_class.resource_path}/#{id}"))
    else
      Collection.new(self, resource_class, args.first || {})
    end
  else
    super
  end
end

Instance Attribute Details

#config ⇒ Configuration (readonly)

Returns Config instance.

Returns:



22
23
24
# File 'lib/devin_api/client.rb', line 22

def config
  @config
end

Instance Method Details

#connection ⇒ Faraday::Connection

Creates a connection if there is none, otherwise returns the existing connection.

Returns:

  • (Faraday::Connection) —

    Faraday connection for the client



40
41
42
# File 'lib/devin_api/client.rb', line 40

def connection
  @connection ||= build_connection
end

#delete(path, params = {}) ⇒ Hash

Executes a DELETE request

Parameters:

  • path (String) —

    The path to request

  • params (Hash) (defaults to: {}) —

    Query parameters

Returns:

  • (Hash) —

    Response body



85
86
87
88
# File 'lib/devin_api/client.rb', line 85

def delete(path, params = {})
  response = connection.delete(path, params)
  parse_response(response)
end

#get(path, params = {}) ⇒ Hash

Executes a GET request

Parameters:

  • path (String) —

    The path to request

  • params (Hash) (defaults to: {}) —

    Query parameters

Returns:

  • (Hash) —

    Response body



48
49
50
51
# File 'lib/devin_api/client.rb', line 48

def get(path, params = {})
  response = connection.get(path, params)
  parse_response(response)
end

#post(path, params = {}) ⇒ Hash, String

Executes a POST request

Parameters:

  • path (String) —

    The path to request

  • params (Hash) (defaults to: {}) —

    Body parameters

Returns:

  • (Hash, String) —

    Response body



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/devin_api/client.rb', line 57

def post(path, params = {})
  response = connection.post(path) do |req|
    if params.values.any? { |v| v.is_a?(Faraday::UploadIO) }
      req.headers['Content-Type'] = 'multipart/form-data'
      req.body = params
    else
      req.headers['Content-Type'] = 'application/json'
      req.body = params.to_json
    end
  end
  parse_response(response)
end

#put(path, params = {}) ⇒ Hash

Executes a PUT request

Parameters:

  • path (String) —

    The path to request

  • params (Hash) (defaults to: {}) —

    Body parameters

Returns:

  • (Hash) —

    Response body



74
75
76
77
78
79
# File 'lib/devin_api/client.rb', line 74

def put(path, params = {})
  response = connection.put(path) do |req|
    req.body = params.to_json
  end
  parse_response(response)
end