lib/groupme/client.rb in groupme-api-0.5.0 vs lib/groupme/client.rb in groupme-api-0.6.0

- old
+ new

@@ -3,17 +3,19 @@ module GroupMe class Client API_BASE_URL = 'https://api.groupme.com/v3/' API_DEFAULT_HEADER = { 'Content-Type': 'application/json' }.freeze - def initialize(access_token:) - @access_token = access_token + attr_accessor :access_token + + def initialize(args = {}) + @access_token = args[:access_token] || GroupMe.configuration.access_token @client = HTTPClient.new(base_url: API_BASE_URL, default_header: API_DEFAULT_HEADER) end - def request(method, path, query: {}, body: {}) - response = @client.request(method, path, { token: @access_token }.merge(query), body.to_json) + def request(method, path, query: {}, body: nil) + response = @client.request(method, path, { token: @access_token }.merge(query), body&.to_json) [parse_response_body(response), response.status] end def get(path, query = {}) request(:get, path, query: query) @@ -32,7 +34,15 @@ def parse_response_body(response) return response.reason unless response.ok? JSON.parse(response.body, symbolize_names: true).fetch(:response) end + end + + def self.client + @client ||= Client.new + end + + def self.client=(client) + @client = client end end