Class: Alula::Client
Constant Summary collapse
- DEFAULT_CUSTOM_OPTIONS =
Has pseudo accessors :api_key, :api_url, :user_agent, :debug
{ omitRelationships: true }
Class Method Summary collapse
- .ensure_api_key_set ⇒ Object
- .ensure_method_allowable(http_method) ⇒ Object
- .ensure_role_set ⇒ Object
- .make_request(http_method, full_url, request_opts) ⇒ Object
- .request(http_method, resource_path, filters = {}, opts = {}) ⇒ Object
- .role ⇒ Object
-
.role=(user_type) ⇒ Object
Stash the user type as a role for us to infer from later You can set a symbolized role via role = :user, or provide the string userType We cast to a symbolized role for ease of use in consumers.
Class Method Details
.ensure_api_key_set ⇒ Object
92 93 94 95 96 97 |
# File 'lib/alula/client.rb', line 92 def ensure_api_key_set if api_key.nil? raise Alula::NotConfiguredError, 'Set your API access token before making requests with Alula::Client.api_key = {access_token}' end end |
.ensure_method_allowable(http_method) ⇒ Object
99 100 101 102 103 |
# File 'lib/alula/client.rb', line 99 def ensure_method_allowable(http_method) unless i[get post put patch delete].include?(http_method) raise "Unable to send a request with #{http_method} http method in #{self::OBJECT_NAME} class" end end |
.ensure_role_set ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/alula/client.rb', line 105 def ensure_role_set if role.nil? = 'User role not configured! You must set '\ 'Alula::Client.role '\ 'before attempting to save any resources' raise Alula::NotConfiguredError, end end |
.make_request(http_method, full_url, request_opts) ⇒ Object
60 61 62 |
# File 'lib/alula/client.rb', line 60 def make_request(http_method, full_url, request_opts) send(http_method, full_url, request_opts) end |
.request(http_method, resource_path, filters = {}, opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/alula/client.rb', line 13 def request(http_method, resource_path, filters = {}, opts = {}) ensure_api_key_set ensure_role_set if i[patch post put].include? http_method ensure_method_allowable(http_method) raise Alula::NotConfiguredError, 'did you forget to set the Alula::Client.api_url config option?' unless api_url request_opts = { headers: { 'Authorization': "Bearer #{api_key}", 'User-Agent': "#{user_agent || 'No Agent Set'}/alula-ruby v#{Alula::VERSION}" } }.merge(opts) request_opts[:headers]['Content-Type'] = 'application/json' unless opts[:multipart] case http_method when :patch, :post request_opts[:body] = opts[:multipart] ? filters : JSON.generate(filters) when :get if resource_path.match(%r{^/rest}) request_opts = request_opts.merge (filters) elsif resource_path.match(%r{^/rpc}) request_opts = request_opts.merge (filters) end when :delete # Nothing special end if debug == true request_opts[:debug_output] = Alula.logger logger Alula.logger, :info end # TODO: Handle network failures response = make_request(http_method, api_url + resource_path, request_opts) begin resp = AlulaResponse.from_httparty_response(response) rescue JSON::ParserError # TODO: Should be able to send better info up with this raise 'Unable to decode JSON' end resp end |
.role ⇒ Object
88 89 90 |
# File 'lib/alula/client.rb', line 88 def role RequestStore.store['alula_role'] end |
.role=(user_type) ⇒ Object
Stash the user type as a role for us to infer from later You can set a symbolized role via role = :user, or provide the string userType We cast to a symbolized role for ease of use in consumers.
80 81 82 83 84 85 86 |
# File 'lib/alula/client.rb', line 80 def role=(user_type) unless user_type.is_a?(Symbol) || user_type.is_a?(String) raise Alula::InvalidRoleError, 'Role must be symbol or string' end RequestStore.store['alula_role'] = Util.underscore(user_type).to_sym end |