nimbu-api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/nimbu-api.rb +43 -0
- data/lib/nimbu-api/authentication.rb +76 -0
- data/lib/nimbu-api/builder.rb +31 -0
- data/lib/nimbu-api/client.rb +26 -0
- data/lib/nimbu-api/configuration.rb +109 -0
- data/lib/nimbu-api/connection.rb +88 -0
- data/lib/nimbu-api/endpoint.rb +199 -0
- data/lib/nimbu-api/endpoints/authorizations.rb +113 -0
- data/lib/nimbu-api/endpoints/channels.rb +31 -0
- data/lib/nimbu-api/endpoints/channels/entries.rb +50 -0
- data/lib/nimbu-api/endpoints/sites.rb +34 -0
- data/lib/nimbu-api/endpoints/videos.rb +26 -0
- data/lib/nimbu-api/errors.rb +31 -0
- data/lib/nimbu-api/errors/bad_request.rb +14 -0
- data/lib/nimbu-api/errors/client_error.rb +20 -0
- data/lib/nimbu-api/errors/forbidden.rb +14 -0
- data/lib/nimbu-api/errors/internal_server_error.rb +15 -0
- data/lib/nimbu-api/errors/invalid_options.rb +18 -0
- data/lib/nimbu-api/errors/not_acceptable.rb +15 -0
- data/lib/nimbu-api/errors/not_found.rb +14 -0
- data/lib/nimbu-api/errors/required_params.rb +18 -0
- data/lib/nimbu-api/errors/service_error.rb +65 -0
- data/lib/nimbu-api/errors/service_unavailable.rb +15 -0
- data/lib/nimbu-api/errors/unauthorized.rb +15 -0
- data/lib/nimbu-api/errors/unknown_value.rb +18 -0
- data/lib/nimbu-api/errors/unprocessable_entity.rb +14 -0
- data/lib/nimbu-api/errors/validations.rb +18 -0
- data/lib/nimbu-api/pagination.rb +106 -0
- data/lib/nimbu-api/pagination/page_iterator.rb +157 -0
- data/lib/nimbu-api/pagination/page_links.rb +48 -0
- data/lib/nimbu-api/pagination/paged_request.rb +41 -0
- data/lib/nimbu-api/pagination/pagination.rb +102 -0
- data/lib/nimbu-api/request.rb +81 -0
- data/lib/nimbu-api/request/arguments.rb +171 -0
- data/lib/nimbu-api/request/basic_auth.rb +31 -0
- data/lib/nimbu-api/request/json.rb +46 -0
- data/lib/nimbu-api/request/normalizer.rb +29 -0
- data/lib/nimbu-api/request/oauth2.rb +42 -0
- data/lib/nimbu-api/request/parameter_filter.rb +34 -0
- data/lib/nimbu-api/request/validations.rb +25 -0
- data/lib/nimbu-api/request/validations/format.rb +26 -0
- data/lib/nimbu-api/request/validations/presence.rb +32 -0
- data/lib/nimbu-api/request/validations/required.rb +26 -0
- data/lib/nimbu-api/request/validations/token.rb +35 -0
- data/lib/nimbu-api/response.rb +34 -0
- data/lib/nimbu-api/response/header.rb +76 -0
- data/lib/nimbu-api/response/json.rb +29 -0
- data/lib/nimbu-api/response/mashify.rb +24 -0
- data/lib/nimbu-api/response/raise_error.rb +18 -0
- data/lib/nimbu-api/response/wrapper.rb +149 -0
- data/lib/nimbu-api/response/xmlize.rb +26 -0
- data/lib/nimbu-api/utils/all.rb +6 -0
- data/lib/nimbu-api/utils/constants.rb +37 -0
- data/lib/nimbu-api/utils/descendants.rb +11 -0
- data/lib/nimbu-api/utils/extend_array.rb +17 -0
- data/lib/nimbu-api/utils/extend_hash.rb +73 -0
- data/lib/nimbu-api/utils/json.rb +19 -0
- data/lib/nimbu-api/utils/url.rb +56 -0
- data/lib/nimbu-api/version.rb +3 -0
- data/nimbu-api.gemspec +31 -0
- metadata +294 -0
@@ -0,0 +1,199 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Nimbu
|
4
|
+
# Core class for api interface operations
|
5
|
+
class Endpoint
|
6
|
+
include ::Nimbu::Utils::Constants
|
7
|
+
include Authentication
|
8
|
+
include Connection
|
9
|
+
include Request
|
10
|
+
|
11
|
+
attr_reader *Configuration.keys
|
12
|
+
attr_accessor *Request::Validations::VALID_API_KEYS
|
13
|
+
|
14
|
+
attr_accessor :current_options
|
15
|
+
|
16
|
+
# Returns all API public methods for a given class.
|
17
|
+
def self.inherited(klass)
|
18
|
+
klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
19
|
+
def self.actions
|
20
|
+
self.new.api_methods_in(#{klass})
|
21
|
+
end
|
22
|
+
def actions
|
23
|
+
api_methods_in(#{klass})
|
24
|
+
end
|
25
|
+
RUBY_EVAL
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def api_methods_in(klass)
|
30
|
+
puts "---"
|
31
|
+
(klass.send(:instance_methods, false) - ['actions']).sort.each do |method|
|
32
|
+
puts "|--> #{method}"
|
33
|
+
end
|
34
|
+
klass.included_modules.each do |mod|
|
35
|
+
if mod.to_s =~ /#{klass}/
|
36
|
+
puts "| \\ #{mod.to_s}"
|
37
|
+
mod.instance_methods(false).each do |met|
|
38
|
+
puts "| |--> #{met}"
|
39
|
+
end
|
40
|
+
puts "| /"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
puts "---"
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def append_arguments(method)
|
48
|
+
_method = self.method(method)
|
49
|
+
if _method.arity == 0
|
50
|
+
args = "()"
|
51
|
+
elsif _method.arity > 0
|
52
|
+
args = "(few)"
|
53
|
+
else
|
54
|
+
args = "(else)"
|
55
|
+
end
|
56
|
+
args
|
57
|
+
end
|
58
|
+
|
59
|
+
# Callback to update current configuration options
|
60
|
+
class_eval do
|
61
|
+
Configuration.keys.each do |key|
|
62
|
+
define_method "#{key}=" do |arg|
|
63
|
+
self.instance_variable_set("@#{key}", arg)
|
64
|
+
self.current_options.merge!({:"#{key}" => arg})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Create new API
|
70
|
+
#
|
71
|
+
def initialize(options={}, &block)
|
72
|
+
setup(options)
|
73
|
+
client(options) if client_id? && client_secret?
|
74
|
+
yield_or_eval(&block) if block_given?
|
75
|
+
end
|
76
|
+
|
77
|
+
def yield_or_eval(&block)
|
78
|
+
return unless block
|
79
|
+
block.arity > 0 ? yield(self) : self.instance_eval(&block)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Configure options and process basic authorization
|
83
|
+
#
|
84
|
+
def setup(options={})
|
85
|
+
options.each do |k,v|
|
86
|
+
self.set(k,v,true)
|
87
|
+
end
|
88
|
+
options = Nimbu.options.merge(options)
|
89
|
+
self.current_options = options
|
90
|
+
Configuration.keys.each do |key|
|
91
|
+
send("#{key}=", options[key])
|
92
|
+
end
|
93
|
+
process_basic_auth(options[:basic_auth])
|
94
|
+
end
|
95
|
+
|
96
|
+
# Extract login and password from basic_auth parameter
|
97
|
+
#
|
98
|
+
def process_basic_auth(auth)
|
99
|
+
case auth
|
100
|
+
when String
|
101
|
+
self.login, self.password = auth.split(':', 2)
|
102
|
+
when Hash
|
103
|
+
self.login = auth[:login]
|
104
|
+
self.password = auth[:password]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Responds to attribute query or attribute clear
|
109
|
+
def method_missing(method, *args, &block) # :nodoc:
|
110
|
+
case method.to_s
|
111
|
+
when /^(.*)\?$/
|
112
|
+
return !!self.send($1.to_s)
|
113
|
+
when /^clear_(.*)$/
|
114
|
+
self.send("#{$1.to_s}=", nil)
|
115
|
+
else
|
116
|
+
super
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Acts as setter and getter for api requests arguments parsing.
|
121
|
+
#
|
122
|
+
# Returns Arguments instance.
|
123
|
+
#
|
124
|
+
def arguments(args=(not_set = true), options={}, &block)
|
125
|
+
if not_set
|
126
|
+
@arguments
|
127
|
+
else
|
128
|
+
@arguments = Arguments.new(self, options).parse(*args, &block)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Scope for passing request required arguments.
|
133
|
+
#
|
134
|
+
def with(args)
|
135
|
+
case args
|
136
|
+
when Hash
|
137
|
+
set args
|
138
|
+
else
|
139
|
+
::Kernel.raise ArgumentError, 'This api does not support passed in arguments'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Set an option to a given value
|
144
|
+
def set(option, value=(not_set=true), ignore_setter=false, &block)
|
145
|
+
raise ArgumentError, 'value not set' if block and !not_set
|
146
|
+
return self if !not_set and value.nil?
|
147
|
+
|
148
|
+
if not_set
|
149
|
+
set_options option
|
150
|
+
return self
|
151
|
+
end
|
152
|
+
|
153
|
+
if respond_to?("#{option}=") and not ignore_setter
|
154
|
+
return __send__("#{option}=", value)
|
155
|
+
end
|
156
|
+
|
157
|
+
define_accessors option, value
|
158
|
+
self
|
159
|
+
end
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
# Set multiple options
|
164
|
+
#
|
165
|
+
def set_options(options)
|
166
|
+
unless options.respond_to?(:each)
|
167
|
+
raise ArgumentError, 'cannot iterate over value'
|
168
|
+
end
|
169
|
+
options.each { |key, value| set(key, value) }
|
170
|
+
end
|
171
|
+
|
172
|
+
def define_accessors(option, value)
|
173
|
+
setter = proc { |val| set option, val, true }
|
174
|
+
getter = proc { value }
|
175
|
+
|
176
|
+
define_singleton_method("#{option}=", setter) if setter
|
177
|
+
define_singleton_method(option, getter) if getter
|
178
|
+
end
|
179
|
+
|
180
|
+
# Dynamically define a method for setting request option
|
181
|
+
#
|
182
|
+
def define_singleton_method(method_name, content=Proc.new)
|
183
|
+
(class << self; self; end).class_eval do
|
184
|
+
undef_method(method_name) if method_defined?(method_name)
|
185
|
+
if String === content
|
186
|
+
class_eval("def #{method_name}() #{content}; end")
|
187
|
+
else
|
188
|
+
define_method(method_name, &content)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def _merge_mime_type(resource, params) # :nodoc:
|
194
|
+
# params['resource'] = resource
|
195
|
+
# params['mime_type'] = params['mime_type'] || :raw
|
196
|
+
end
|
197
|
+
|
198
|
+
end # Endpoint
|
199
|
+
end # Nimbu
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nimbu
|
4
|
+
module Endpoints
|
5
|
+
class Authorizations < Endpoint
|
6
|
+
|
7
|
+
VALID_AUTH_PARAM_NAMES = %w[
|
8
|
+
scopes
|
9
|
+
add_scopes
|
10
|
+
remove_scopes
|
11
|
+
note
|
12
|
+
note_url
|
13
|
+
client_id
|
14
|
+
client_secret
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
# List authorizations
|
18
|
+
#
|
19
|
+
# = Examples
|
20
|
+
# nimbu = Nimbu.new :basic_auth => 'login:password'
|
21
|
+
# nimbu.oauth.list
|
22
|
+
# nimbu.oauth.list { |auth| ... }
|
23
|
+
#
|
24
|
+
def list(*args)
|
25
|
+
require_authentication
|
26
|
+
arguments(args)
|
27
|
+
|
28
|
+
response = get_request("/authorizations", arguments.params)
|
29
|
+
return response unless block_given?
|
30
|
+
response.each { |el| yield el }
|
31
|
+
end
|
32
|
+
alias :all :list
|
33
|
+
|
34
|
+
# Get a single authorization
|
35
|
+
#
|
36
|
+
# = Examples
|
37
|
+
# nimbu = Nimbu.new :basic_auth => 'login:password'
|
38
|
+
# nimbu.oauth.get 'authorization-id'
|
39
|
+
#
|
40
|
+
def get(*args)
|
41
|
+
require_authentication
|
42
|
+
arguments(args, :required => [:authorization_id])
|
43
|
+
|
44
|
+
get_request("/authorizations/#{authorization_id}", arguments.params)
|
45
|
+
end
|
46
|
+
alias :find :get
|
47
|
+
|
48
|
+
# Create a new authorization
|
49
|
+
#
|
50
|
+
# = Inputs
|
51
|
+
# * <tt>:scopes</tt> - Optional array - A list of scopes that this authorization is in.
|
52
|
+
# * <tt>:note</tt> - Optional string - A note to remind you what the OAuth token is for.
|
53
|
+
# * <tt>:note_url</tt> - Optional string - A URL to remind you what the OAuth token is for.
|
54
|
+
#
|
55
|
+
# = Examples
|
56
|
+
# nimbu = Nimbu.new :basic_auth => 'login:password'
|
57
|
+
# nimbu.oauth.create
|
58
|
+
# "scopes" => ["public_repo"]
|
59
|
+
#
|
60
|
+
def create(*args)
|
61
|
+
require_authentication
|
62
|
+
arguments(args) do
|
63
|
+
sift VALID_AUTH_PARAM_NAMES
|
64
|
+
end
|
65
|
+
|
66
|
+
post_request("/authorizations", arguments.params)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Update an existing authorization
|
70
|
+
#
|
71
|
+
# = Inputs
|
72
|
+
# * <tt>:scopes</tt> - Optional array - A list of scopes that this authorization is in.
|
73
|
+
# * <tt>:add_scopes</tt> - Optional array - A list of scopes to add to this authorization.
|
74
|
+
# * <tt>:remove_scopes</tt> - Optional array - A list of scopes to remove from this authorization.
|
75
|
+
# * <tt>:note</tt> - Optional string - A note to remind you what the OAuth token is for.
|
76
|
+
# * <tt>:note_url</tt> - Optional string - A URL to remind you what the OAuth token is for.
|
77
|
+
#
|
78
|
+
# = Examples
|
79
|
+
# nimbu = Nimbu.new :basic_auth => 'login:password'
|
80
|
+
# nimbu.oauth.update "authorization-id", "add_scopes" => ["repo"],
|
81
|
+
#
|
82
|
+
def update(*args)
|
83
|
+
require_authentication
|
84
|
+
arguments(args, :required => [:authorization_id]) do
|
85
|
+
sift VALID_AUTH_PARAM_NAMES
|
86
|
+
end
|
87
|
+
|
88
|
+
patch_request("/authorizations/#{authorization_id}", arguments.params)
|
89
|
+
end
|
90
|
+
alias :edit :update
|
91
|
+
|
92
|
+
# Delete an authorization
|
93
|
+
#
|
94
|
+
# = Examples
|
95
|
+
# nimbu.oauth.delete 'authorization-id'
|
96
|
+
#
|
97
|
+
def delete(*args)
|
98
|
+
require_authentication
|
99
|
+
arguments(args, :required => [:authorization_id])
|
100
|
+
|
101
|
+
delete_request("/authorizations/#{authorization_id}", arguments.params)
|
102
|
+
end
|
103
|
+
alias :remove :delete
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def require_authentication
|
108
|
+
raise ArgumentError, 'You can only access authentication tokens through Basic Authentication' unless authenticated?
|
109
|
+
end
|
110
|
+
|
111
|
+
end # Authorizations
|
112
|
+
end # Endpoints
|
113
|
+
end # Nimbu
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
|
4
|
+
module Nimbu
|
5
|
+
module Endpoints
|
6
|
+
class Channels < Endpoint
|
7
|
+
|
8
|
+
def entries(options={}, &block)
|
9
|
+
@entries ||= Nimbu::Builder.new('Channels::Entries', current_options.merge(options), &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def list(*args)
|
13
|
+
arguments(args)
|
14
|
+
|
15
|
+
response = get_request("/channels", arguments.params)
|
16
|
+
return response unless block_given?
|
17
|
+
response.each { |el| yield el }
|
18
|
+
end
|
19
|
+
alias :all :list
|
20
|
+
|
21
|
+
def get(*args)
|
22
|
+
arguments(args, :required => [:channel_id])
|
23
|
+
|
24
|
+
get_request("/channels/#{channel_id}", arguments.params)
|
25
|
+
end
|
26
|
+
alias :find :get
|
27
|
+
|
28
|
+
|
29
|
+
end # Authorizations
|
30
|
+
end # Endpoints
|
31
|
+
end # Nimbu
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nimbu
|
4
|
+
module Endpoints
|
5
|
+
class Channels::Entries < Endpoint
|
6
|
+
|
7
|
+
def list(*args)
|
8
|
+
arguments(args, :required => [:channel_id])
|
9
|
+
|
10
|
+
response = get_request("/channels/#{channel_id}/entries", arguments.params)
|
11
|
+
|
12
|
+
if block_given?
|
13
|
+
response.each { |el| yield el }
|
14
|
+
else
|
15
|
+
return response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
alias :all :list
|
19
|
+
|
20
|
+
def get(*args)
|
21
|
+
arguments(args, :required => [:channel_id, :entry_id])
|
22
|
+
|
23
|
+
get_request("/channels/#{channel_id}/entries/#{entry_id}", arguments.params)
|
24
|
+
end
|
25
|
+
alias :find :get
|
26
|
+
|
27
|
+
def create(*args)
|
28
|
+
arguments(args, :required => [:channel_id])
|
29
|
+
|
30
|
+
post_request("/channels/#{channel_id}/entries", arguments.params, :with_attachments => true)
|
31
|
+
end
|
32
|
+
|
33
|
+
def update(*args)
|
34
|
+
arguments(args, :required => [:channel_id, :entry_id])
|
35
|
+
|
36
|
+
patch_request("/channels/#{channel_id}/entries/#{entry_id}", arguments.params, :with_attachments => true)
|
37
|
+
end
|
38
|
+
alias :edit :update
|
39
|
+
|
40
|
+
def delete(*args)
|
41
|
+
arguments(args, :required => [:channel_id, :entry_id])
|
42
|
+
|
43
|
+
delete_request("/channels/#{channel_id}/entries/#{entry_id}", arguments.params)
|
44
|
+
end
|
45
|
+
alias :remove :delete
|
46
|
+
|
47
|
+
|
48
|
+
end # Authorizations
|
49
|
+
end # Endpoints
|
50
|
+
end # Nimbu
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nimbu
|
4
|
+
module Endpoints
|
5
|
+
class Sites < Endpoint
|
6
|
+
|
7
|
+
def list(*args)
|
8
|
+
require_authentication
|
9
|
+
arguments(args)
|
10
|
+
|
11
|
+
response = get_request("/sites", arguments.params)
|
12
|
+
return response unless block_given?
|
13
|
+
response.each { |el| yield el }
|
14
|
+
end
|
15
|
+
alias :all :list
|
16
|
+
|
17
|
+
def get(*args)
|
18
|
+
require_authentication
|
19
|
+
arguments(args, :required => [:site_id])
|
20
|
+
|
21
|
+
get_request("/authorizations/#{site_id}", arguments.params)
|
22
|
+
end
|
23
|
+
alias :find :get
|
24
|
+
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def require_authentication
|
29
|
+
raise ArgumentError, 'You need to be authentication to access the sites' unless authenticated?
|
30
|
+
end
|
31
|
+
|
32
|
+
end # Authorizations
|
33
|
+
end # Endpoints
|
34
|
+
end # Nimbu
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nimbu
|
4
|
+
module Endpoints
|
5
|
+
class Videos < Endpoint
|
6
|
+
|
7
|
+
def list(*args)
|
8
|
+
arguments(args)
|
9
|
+
|
10
|
+
response = get_request("/videos", arguments.params)
|
11
|
+
return response unless block_given?
|
12
|
+
response.each { |el| yield el }
|
13
|
+
end
|
14
|
+
alias :all :list
|
15
|
+
|
16
|
+
def get(*args)
|
17
|
+
arguments(args, :required => [:video_id])
|
18
|
+
|
19
|
+
get_request("/videos/#{video_id}", arguments.params)
|
20
|
+
end
|
21
|
+
alias :find :get
|
22
|
+
|
23
|
+
|
24
|
+
end # Authorizations
|
25
|
+
end # Endpoints
|
26
|
+
end # Nimbu
|