Class: FaithTeams::API::V2::Connection
- Inherits:
-
Object
- Object
- FaithTeams::API::V2::Connection
- 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
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #get(path:, params: {}) ⇒ Hash
-
#initialize(user_id:, password:) ⇒ Connection
constructor
A new instance of Connection.
- #post(path:, body: nil) ⇒ Hash
-
#request(method: :get, path:, params: {}, body: {}) ⇒ HTTP::Response
Request an endpoint from faithteams, checking for errors.
-
#request_and_parse(method: "get", path:, params: {}, body: {}) ⇒ Hash
Request an endpoint from faithteams and parse the response.
- #user_resource ⇒ Resource::User
-
#valid? ⇒ Boolean
Is the connection to FaithTeams valid?.
Constructor Details
#initialize(user_id:, password:) ⇒ Connection
Returns a new instance of Connection.
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
#password ⇒ Object (readonly)
Returns the value of attribute password.
20 21 22 |
# File 'lib/faithteams/api/v2/connection.rb', line 20 def password @password end |
#user_id ⇒ Object (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
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
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
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
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_resource ⇒ 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 |