Class: NationBuilder::Client
- Inherits:
-
Object
- Object
- NationBuilder::Client
- Defined in:
- lib/nationbuilder/client.rb
Defined Under Namespace
Classes: InvalidEndpoint
Constant Summary collapse
- RETRY_DELAY =
seconds
0.1
Instance Method Summary collapse
- #[](endpoint) ⇒ Object
- #base_url ⇒ Object
- #call(endpoint_name, method_name, args = {}) ⇒ Object
- #classify_response_error(response) ⇒ Object
- #endpoints ⇒ Object
-
#initialize(nation_name, api_key, opts = {}) ⇒ Client
constructor
A new instance of Client.
- #parse_response_body(response) ⇒ Object
- #parsed_endpoints ⇒ Object
- #perform_request_with_retries(method, url, request_args) ⇒ Object
- #print_all_descriptions ⇒ Object
- #print_description(endpoint_name) ⇒ Object
- #raw_call(path, method, body = {}, args = {}) ⇒ Object
-
#response ⇒ Object
This getter is used for fetching the raw response.
- #set_response(value) ⇒ Object
Constructor Details
#initialize(nation_name, api_key, opts = {}) ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nationbuilder/client.rb', line 3 def initialize(nation_name, api_key, opts = {}) @nation_name = nation_name @api_key = api_key @name_to_endpoint = {} @base_url = opts[:base_url] || 'https://:nation_name.nationbuilder.com' @retries = opts[:retries] || 8 if @retries < 0 raise 'Retries must be at least zero' end parsed_endpoints.each do |endpoint| @name_to_endpoint[endpoint.name] = endpoint end end |
Instance Method Details
#[](endpoint) ⇒ Object
26 27 28 29 30 |
# File 'lib/nationbuilder/client.rb', line 26 def [](endpoint) e = @name_to_endpoint[endpoint] raise InvalidEndpoint.new(endpoint) if e.nil? e end |
#base_url ⇒ Object
36 37 38 |
# File 'lib/nationbuilder/client.rb', line 36 def base_url @base_url.gsub(':nation_name', @nation_name) end |
#call(endpoint_name, method_name, args = {}) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/nationbuilder/client.rb', line 68 def call(endpoint_name, method_name, args={}) endpoint = self[endpoint_name] method = endpoint[method_name] nonmethod_args = method.nonmethod_args(args) method_args = method.method_args(args) method.validate_args(method_args) return raw_call(method.uri, method.http_method, nonmethod_args, args) end |
#classify_response_error(response) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/nationbuilder/client.rb', line 115 def classify_response_error(response) case when response.code == 429 NationBuilder::RateLimitedError.new(response.body) when response.code.to_s.start_with?('4') NationBuilder::ClientError.new(response.body) when response.code.to_s.start_with?('5') NationBuilder::ServerError.new(response.body) end end |
#endpoints ⇒ Object
32 33 34 |
# File 'lib/nationbuilder/client.rb', line 32 def endpoints @name_to_endpoint.keys end |
#parse_response_body(response) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/nationbuilder/client.rb', line 126 def parse_response_body(response) error = classify_response_error(response) raise error if error if response.header['Content-Type'].first != 'application/json' return true end body = response.body.strip return {} if body.length == 0 return JSON.parse(body) end |
#parsed_endpoints ⇒ Object
19 20 21 22 |
# File 'lib/nationbuilder/client.rb', line 19 def parsed_endpoints NationBuilder::SpecParser .parse(File.join(File.dirname(__FILE__), 'api_spec.json')) end |
#perform_request_with_retries(method, url, request_args) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/nationbuilder/client.rb', line 77 def perform_request_with_retries(method, url, request_args) raw_response = nil parsed_response = nil exception_to_reraise = nil (@retries + 1).times do |i| begin raw_response = HTTPClient.send(method, url, request_args) parsed_response = parse_response_body(raw_response) rescue NationBuilder::RateLimitedError => e exception_to_reraise = e Kernel.sleep(RETRY_DELAY * 2**i) rescue => e raise e else exception_to_reraise = nil break end end # If the retry cycle ended with an error, reraise it if exception_to_reraise raise exception_to_reraise end set_response(raw_response) parsed_response end |
#print_all_descriptions ⇒ Object
139 140 141 142 143 144 |
# File 'lib/nationbuilder/client.rb', line 139 def print_all_descriptions endpoints.each do |endpoint_name| self.print_description(endpoint_name) puts end end |
#print_description(endpoint_name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/nationbuilder/client.rb', line 146 def print_description(endpoint_name) endpoint_name = endpoint_name.to_sym unless self.endpoints.include?(endpoint_name) puts "Invalid endpoint name: #{endpoint_name}" puts puts "Valid endpoint names:" self.endpoints.each do |endpoint| puts " #{endpoint}" end return end endpoint_str = "Endpoint: #{endpoint_name}" puts "=" * endpoint_str.length puts endpoint_str puts "=" * endpoint_str.length self[endpoint_name].methods.each do |method_name| puts method = self[endpoint_name][method_name] puts " Method: #{method_name}" puts " Description: #{method.description}" required_params = method.parameters.map { |p| p } if required_params.any? puts " Required parameters: #{required_params.join(', ')}" end end end |
#raw_call(path, method, body = {}, args = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nationbuilder/client.rb', line 42 def raw_call(path, method, body = {}, args = {}) url = NationBuilder::URL.new(base_url).generate_url(path, args) request_args = { header: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }, query: { access_token: @api_key } } if method == :get request_args[:query].merge!(body) else body[:access_token] = @api_key if !body[:fire_webhooks].nil? request_args[:query][:fire_webhooks] = body[:fire_webhooks] end request_args[:body] = JSON(body) end perform_request_with_retries(method, url, request_args) end |
#response ⇒ Object
This getter is used for fetching the raw response
111 112 113 |
# File 'lib/nationbuilder/client.rb', line 111 def response Thread.current[:nationbuilder_rb_response] end |
#set_response(value) ⇒ Object
106 107 108 |
# File 'lib/nationbuilder/client.rb', line 106 def set_response(value) Thread.current[:nationbuilder_rb_response] = value end |