Class: Nimbu::Endpoint

Inherits:
Object
  • Object
show all
Includes:
Authentication, Connection, Request, Utils::Constants
Defined in:
lib/nimbu-api/endpoint.rb

Overview

Core class for api interface operations

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

Connection::ALLOWED_OPTIONS

Instance Attribute Summary collapse

Attributes included from Authentication

#scopes

Class Method Summary collapse

Instance Method Summary collapse

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(options={}, &block)
  setup(options)
  client(options) 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_optionsObject

Returns the value of attribute current_options.



14
15
16
# File 'lib/nimbu-api/endpoint.rb', line 14

def current_options
  @current_options
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), options={}, &block)
  if not_set
    @arguments
  else
    @arguments = Arguments.new(self, options).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., self.password = auth.split(':', 2)
  when Hash
    self.    = 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

Raises:

  • (ArgumentError)


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
    set_options 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(options={})
  options.each do |k,v|
    self.set(k,v,true)
  end
  options = Nimbu.options.merge(options)
  self.current_options = options
  Configuration.keys.each do |key|
    send("#{key}=", options[key])
  end
  process_basic_auth(options[: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