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 18 |
# 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 @http_client = opts[:http_client] || HTTPClient.new 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
27 28 29 30 31 |
# File 'lib/nationbuilder/client.rb', line 27 def [](endpoint) e = @name_to_endpoint[endpoint] raise InvalidEndpoint.new(endpoint) if e.nil? e end |
#base_url ⇒ Object
37 38 39 |
# File 'lib/nationbuilder/client.rb', line 37 def base_url @base_url.gsub(':nation_name', @nation_name) end |
#call(endpoint_name, method_name, args = {}) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/nationbuilder/client.rb', line 69 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
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/nationbuilder/client.rb', line 112 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
33 34 35 |
# File 'lib/nationbuilder/client.rb', line 33 def endpoints @name_to_endpoint.keys end |
#parse_response_body(response) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nationbuilder/client.rb', line 123 def parse_response_body(response) error = classify_response_error(response) raise error if error content_type = response.header['Content-Type'].first unless content_type && content_type.include?('application/json') return true end parsed_body(response.body) end |
#parsed_endpoints ⇒ Object
20 21 22 23 |
# File 'lib/nationbuilder/client.rb', line 20 def parsed_endpoints NationBuilder::SpecParser .parse(File.join(File.dirname(__FILE__), '..', 'api_spec/spec.json')) end |
#perform_request_with_retries(method, url, request_args) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/nationbuilder/client.rb', line 78 def perform_request_with_retries(method, url, request_args) parsed_response = nil exception_to_reraise = nil (@retries + 1).times do |i| begin set_response(@http_client.send(method, url, request_args)) parsed_response = parse_response_body(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 raise exception_to_reraise if exception_to_reraise parsed_response end |
#print_all_descriptions ⇒ Object
135 136 137 138 139 140 |
# File 'lib/nationbuilder/client.rb', line 135 def print_all_descriptions endpoints.each do |endpoint_name| self.print_description(endpoint_name) puts end end |
#print_description(endpoint_name) ⇒ Object
142 143 144 145 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 |
# File 'lib/nationbuilder/client.rb', line 142 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nationbuilder/client.rb', line 43 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
108 109 110 |
# File 'lib/nationbuilder/client.rb', line 108 def response Thread.current[:nationbuilder_rb_response] end |
#set_response(value) ⇒ Object
103 104 105 |
# File 'lib/nationbuilder/client.rb', line 103 def set_response(value) Thread.current[:nationbuilder_rb_response] = value end |