Class: Rocky::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Registrations, StateRequirements
Defined in:
lib/rockthevote/client.rb,
lib/rockthevote/client/registrations.rb,
lib/rockthevote/client/state_requirements.rb

Defined Under Namespace

Modules: Registrations, StateRequirements

Instance Method Summary collapse

Methods included from Registrations

#create_registration

Methods included from StateRequirements

#get_state_requirements

Constructor Details

#initialize(env, api_key = nil, api_version = nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rockthevote/client.rb', line 13

def initialize(env, api_key=nil, api_version=nil)
	@env ||= "staging"
	@api_key ||= ENV["ROCKY_API_KEY"]
	@api_version ||= "4"

	if env == "production"
		url_prefix = "register.rockthevote.com"
	elsif env == "staging"
		url_prefix = "staging.rocky.rockthevote.com"
	else
		raise StandardError.new "Please specify either a 'production' or 'staging' env argument."
	end
			
	@base_uri = "https://#{url_prefix}/api/v#{@api_version}"
	self.class.default_options.merge!(headers: { 'Authorization' => "Bearer #{api_key}" })
end

Instance Method Details

#perform_get_request(path, options = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rockthevote/client.rb', line 31

def perform_get_request(path, options = {})
	url = "#{@base_uri}#{path}"
	response = self.class.get(url, { query: options })
	body = response.parsed_response
	body.define_singleton_method(:_response) { response }
	return body
end

#perform_post_request(path, options = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rockthevote/client.rb', line 40

def perform_post_request(path, options = {})
	url = "#{@base_uri}#{path}"
	response = self.class.post(url, { body: options })
	body = response.parsed_response
	body.define_singleton_method(:_response) { response }
	return body
end