Class: FaithTeams::API::V2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/faithteams/api/v2/connection.rb

Overview

Faithteams API v2 connection

Constant Summary collapse

ENDPOINT_BASE_URLS =

Specific base urls for different resources

{
  "batches" => "https://api-v2.faithteams.com",
  "contributions" => "https://api-v2.faithteams.com",
  "contributiontypes" => "https://api-v2.faithteams.com",
  "funds" => "https://api-v2.faithteams.com",
  "people" => "https://app.faithteams.com/api/v2"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id:, password:) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • user_id (String)
  • password (String)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
# File 'lib/faithteams/api/v2/connection.rb', line 24

def initialize(user_id:, password:)
  raise ArgumentError.new("A user_id is required") if user_id.blank?
  raise ArgumentError.new("A password is required") if password.blank?

  @user_id = user_id
  @password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



20
21
22
# File 'lib/faithteams/api/v2/connection.rb', line 20

def password
  @password
end

#user_idObject (readonly)

Returns the value of attribute user_id.



20
21
22
# File 'lib/faithteams/api/v2/connection.rb', line 20

def user_id
  @user_id
end

Instance Method Details

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

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

    Parameters to send with the request

Returns:

  • (Hash)

Raises:



45
46
47
# File 'lib/faithteams/api/v2/connection.rb', line 45

def get(path:, params: {})
  request_and_parse(method: :get, path: path, params: params)
end

#post(path:, body: nil) ⇒ Hash

Parameters:

  • path (String)
  • body (Hash, nil) (defaults to: nil)

    Body to send

Returns:

  • (Hash)

Raises:



53
54
55
# File 'lib/faithteams/api/v2/connection.rb', line 53

def post(path:, body: nil)
  request_and_parse(method: :post, path: path, body: body)
end

#request(method: :get, path:, params: {}, body: {}) ⇒ HTTP::Response

Request an endpoint from faithteams, checking for errors

Parameters:

  • method (String) (defaults to: :get)

    :get or :post

  • path (String)

    Path to the endpoint relative to ENDPOINT_BASE_URLS

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

    Request parameters

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

    Body to send

Returns:

  • (HTTP::Response)

    Results of the request

Raises:

  • (Error::Request)

    Caller is responsible for capturing the error if needed



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/faithteams/api/v2/connection.rb', line 64

def request(method: :get, path:, params: {}, body: {})
  url = "#{base_url(path: path)}#{path}"

  retries ||= 0
  response = nil
  loop do
    retries += 1

    case method.to_s
    when "post"
      response = http.post(url, json: body)
    else # get
      response = http.get(url, params: params)
    end
    break if response.status != 401 || retries >= 2
  end

  raise Error::Request.new(response: response, message: "Request unsuccessful (#{response.status})") unless response.status.success?

  response
end

#request_and_parse(method: "get", path:, params: {}, body: {}) ⇒ Hash

Request an endpoint from faithteams and parse the response

Parameters:

  • method (String) (defaults to: "get")

    :get or :post

  • path (String)

    Path to the endpoint relative to ENDPOINT_BASE_URLS

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

    Request parameters

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

    Body to send

Returns:

  • (Hash)

    Contents of the body

Raises:

  • (Error::Request)

    Caller is responsible for capturing the error if needed



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/faithteams/api/v2/connection.rb', line 93

def request_and_parse(method: "get", path:, params: {}, body: {})
  response = request(method: method, path: path, params: params, body: body)
  begin
    response_body = response.parse(:json)
  rescue JSON::ParserError
    raise Error::Request.new(response: response, message: "Failed to parse JSON")
  end

  # raise errors when "status" = 200 but "success" = false
  raise Error::Request.new(response: response, message: "Request unsuccessful") unless response_body["success"] == true

  response_body
end

#user_resourceResource::User

Returns:

  • (Resource::User)


108
109
110
# File 'lib/faithteams/api/v2/connection.rb', line 108

def user_resource
  @user_resource ||= Resource::User.new(connection: self)
end

#valid?Boolean

Is the connection to FaithTeams valid?

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/faithteams/api/v2/connection.rb', line 34

def valid?
  get(path: "/contributiontypes")
  true
rescue Error::Request
  false
end