Class: ChJsonApi
- Inherits:
-
Object
- Object
- ChJsonApi
- Defined in:
- lib/chjsonapi.rb,
lib/chjsonapi/company.rb,
lib/chjsonapi/version.rb
Overview
Base class for the CH Json Api Can be used to make direct API calls to Companies House domain The extended classes should implement specific methods to interface with api_call by providing the appropriate URL handles, querystrings and request types
Defined Under Namespace
Classes: Company
Constant Summary collapse
- VERSION =
'0.3.2'
Class Method Summary collapse
-
.api_call(handler, query) ⇒ Object
Calls the Companies House API at the service specified by handler and with the optional parameters provided in query.
- .execute_call(handler, query) ⇒ Object
- .extract_response(response) ⇒ Object
-
.init(key) ⇒ Object
Call this before using any other method.
-
.normalise_query(query) ⇒ Object
TODO accept a key => value hash.
Class Method Details
.api_call(handler, query) ⇒ Object
Calls the Companies House API at the service specified by handler and with the optional parameters provided in query
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/chjsonapi.rb', line 31 def self.api_call(handler, query) raise 'Uninitialised API. Call ChJsonApi.init(key) with your Companies House API key before running any requests' if !@key || @key.empty? query = normalise_query(query) result = execute_call(handler, query) return extract_response(result.body_str) end |
.execute_call(handler, query) ⇒ 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 68 69 70 71 |
# File 'lib/chjsonapi.rb', line 43 def self.execute_call(handler, query) @result ||= Curl::Easy.new @result.url = "https://api.companieshouse.gov.uk/#{handler}#{query}" @result.username = "#{choose_key}:" @result.http_auth_types = :basic @result.perform code = @result.response_code #Detect any errors. #If found, treat them and retry the function #This will if code == 401 handle_invalid_key(@result) @result = execute_call(handler, query) end if code == 429 handle_too_many_requests(@result) @result = execute_call(handler, query) end ##All ok, reset the "tries" counter and return the result @tries = 0 @result end |
.extract_response(response) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/chjsonapi.rb', line 73 def self.extract_response(response) return {} if response.empty? json = JSON.parse(response) return json unless json['errors'] raise json['errors'][0].map { |key, value| "#{key}=>#{value}" }.join(' ') end |
.init(key) ⇒ Object
Call this before using any other method
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/chjsonapi.rb', line 15 def self.init(key) if key.is_a? Array @key = key elsif key.is_a? String @key = [key] else raise 'Invalid Key Type. String or Array of Strings accepted only.' end @index = 0 @tries = 0 true end |
.normalise_query(query) ⇒ Object
TODO accept a key => value hash
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/chjsonapi.rb', line 84 def self.normalise_query(query) if !query query = '' elsif query.kind_of? Array query = "?#{query.join '&'}" elsif !query.kind_of? String raise 'Query must be a string or an array with each element being a "key=value" string' end query end |