Class: Nimbu::Endpoint
- Inherits:
-
Object
- Object
- Nimbu::Endpoint
- Includes:
- Authentication, Connection, Request, Utils::Constants
- Defined in:
- lib/nimbu-api/endpoint.rb
Overview
Core class for api interface operations
Direct Known Subclasses
Client, Nimbu::Endpoints::Activities, Nimbu::Endpoints::Authorizations, Nimbu::Endpoints::Channels, Nimbu::Endpoints::Channels::Entries, Nimbu::Endpoints::Collections, Nimbu::Endpoints::Coupons, Nimbu::Endpoints::Customers, Nimbu::Endpoints::Devices, Nimbu::Endpoints::Functions, Nimbu::Endpoints::Jobs, Nimbu::Endpoints::Login, Nimbu::Endpoints::Orders, Nimbu::Endpoints::Products, Nimbu::Endpoints::Roles, Nimbu::Endpoints::Simulator, Nimbu::Endpoints::Sites, Nimbu::Endpoints::Themes, Nimbu::Endpoints::Themes::Assets, Nimbu::Endpoints::Themes::Layouts, Nimbu::Endpoints::Themes::Snippets, Nimbu::Endpoints::Themes::Templates, Nimbu::Endpoints::Translations, Nimbu::Endpoints::Videos, Nimbu::Endpoints::Webhooks
Constant Summary
Constants included from Request
Request::METHODS, Request::METHODS_WITH_BODIES
Constants included from Utils::Constants
Utils::Constants::ACCEPT, Utils::Constants::ACCEPTED_OAUTH_SCOPES, Utils::Constants::ACCEPT_CHARSET, Utils::Constants::CACHE_CONTROL, Utils::Constants::CONTENT_LENGTH, Utils::Constants::CONTENT_TYPE, Utils::Constants::DATE, Utils::Constants::ETAG, Utils::Constants::HEADER_LAST, Utils::Constants::HEADER_LINK, Utils::Constants::HEADER_NEXT, Utils::Constants::LOCATION, Utils::Constants::META_FIRST, Utils::Constants::META_LAST, Utils::Constants::META_NEXT, Utils::Constants::META_PREV, Utils::Constants::META_REL, Utils::Constants::NIMBU_SITE, Utils::Constants::OAUTH_SCOPES, Utils::Constants::PARAM_PAGE, Utils::Constants::PARAM_PER_PAGE, Utils::Constants::PARAM_START_PAGE, Utils::Constants::RATELIMIT_LIMIT, Utils::Constants::RATELIMIT_REMAINING, Utils::Constants::SERVER, Utils::Constants::USER_AGENT
Constants included from Connection
Instance Attribute Summary collapse
-
#current_options ⇒ Object
Returns the value of attribute current_options.
Attributes included from Authentication
Class Method Summary collapse
-
.inherited(klass) ⇒ Object
Returns all API public methods for a given class.
Instance Method Summary collapse
- #api_methods_in(klass) ⇒ Object
- #append_arguments(method) ⇒ Object
-
#arguments(args = (not_set = true), options = {}, &block) ⇒ Object
Acts as setter and getter for api requests arguments parsing.
-
#initialize(options = {}, &block) ⇒ Endpoint
constructor
Create new API.
-
#method_missing(method, *args, &block) ⇒ Object
Responds to attribute query or attribute clear.
-
#process_basic_auth(auth) ⇒ Object
Extract login and password from basic_auth parameter.
-
#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ Object
Set an option to a given value.
-
#setup(options = {}) ⇒ Object
Configure options and process basic authorization.
-
#with(args) ⇒ Object
Scope for passing request required arguments.
- #yield_or_eval(&block) ⇒ Object
Methods included from Request
#delete_request, #get_request, #patch_request, #post_request, #put_request, #request
Methods included from Connection
#caching?, #clear_cache, #connection, #default_middleware, #default_options, #stack
Methods included from Authentication
#auth_code, #authenticated?, #authentication, #authorize_url, #basic_authed?, #client, #get_token
Constructor Details
#initialize(options = {}, &block) ⇒ Endpoint
Create new API
71 72 73 74 75 |
# File 'lib/nimbu-api/endpoint.rb', line 71 def initialize(={}, &block) setup() client() if client_id? && client_secret? yield_or_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Responds to attribute query or attribute clear
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/nimbu-api/endpoint.rb', line 109 def method_missing(method, *args, &block) # :nodoc: case method.to_s when /^(.*)\?$/ return !!self.send($1.to_s) when /^clear_(.*)$/ self.send("#{$1.to_s}=", nil) else super end end |
Instance Attribute Details
#current_options ⇒ Object
Returns the value of attribute current_options.
14 15 16 |
# File 'lib/nimbu-api/endpoint.rb', line 14 def end |
Class Method Details
.inherited(klass) ⇒ Object
Returns all API public methods for a given class.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nimbu-api/endpoint.rb', line 17 def self.inherited(klass) klass.class_eval " def self.actions\n self.new.api_methods_in(\#{klass})\n end\n def actions\n api_methods_in(\#{klass})\n end\n RUBY_EVAL\n super\nend\n", __FILE__, __LINE__ + 1 |
Instance Method Details
#api_methods_in(klass) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/nimbu-api/endpoint.rb', line 29 def api_methods_in(klass) puts "---" (klass.send(:instance_methods, false) - ['actions']).sort.each do |method| puts "|--> #{method}" end klass.included_modules.each do |mod| if mod.to_s =~ /#{klass}/ puts "| \\ #{mod.to_s}" mod.instance_methods(false).each do |met| puts "| |--> #{met}" end puts "| /" end end puts "---" nil end |
#append_arguments(method) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nimbu-api/endpoint.rb', line 47 def append_arguments(method) _method = self.method(method) if _method.arity == 0 args = "()" elsif _method.arity > 0 args = "(few)" else args = "(else)" end args end |
#arguments(args = (not_set = true), options = {}, &block) ⇒ Object
Acts as setter and getter for api requests arguments parsing.
Returns Arguments instance.
124 125 126 127 128 129 130 |
# File 'lib/nimbu-api/endpoint.rb', line 124 def arguments(args=(not_set = true), ={}, &block) if not_set @arguments else @arguments = Arguments.new(self, ).parse(*args, &block) end end |
#process_basic_auth(auth) ⇒ Object
Extract login and password from basic_auth parameter
98 99 100 101 102 103 104 105 106 |
# File 'lib/nimbu-api/endpoint.rb', line 98 def process_basic_auth(auth) case auth when String self.login, self.password = auth.split(':', 2) when Hash self.login = auth[:login] self.password = auth[:password] end end |
#set(option, value = (not_set=true), ignore_setter = false, &block) ⇒ Object
Set an option to a given value
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/nimbu-api/endpoint.rb', line 144 def set(option, value=(not_set=true), ignore_setter=false, &block) raise ArgumentError, 'value not set' if block and !not_set return self if !not_set and value.nil? if not_set option return self end if respond_to?("#{option}=") and not ignore_setter return __send__("#{option}=", value) end define_accessors option, value self end |
#setup(options = {}) ⇒ Object
Configure options and process basic authorization
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/nimbu-api/endpoint.rb', line 84 def setup(={}) .each do |k,v| self.set(k,v,true) end = Nimbu..merge() self. = Configuration.keys.each do |key| send("#{key}=", [key]) end process_basic_auth([:basic_auth]) end |
#with(args) ⇒ Object
Scope for passing request required arguments.
134 135 136 137 138 139 140 141 |
# File 'lib/nimbu-api/endpoint.rb', line 134 def with(args) case args when Hash set args else ::Kernel.raise ArgumentError, 'This api does not support passed in arguments' end end |
#yield_or_eval(&block) ⇒ Object
77 78 79 80 |
# File 'lib/nimbu-api/endpoint.rb', line 77 def yield_or_eval(&block) return unless block block.arity > 0 ? yield(self) : self.instance_eval(&block) end |