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.
Files changed (66) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +1 -0
  6. data/lib/nimbu-api.rb +43 -0
  7. data/lib/nimbu-api/authentication.rb +76 -0
  8. data/lib/nimbu-api/builder.rb +31 -0
  9. data/lib/nimbu-api/client.rb +26 -0
  10. data/lib/nimbu-api/configuration.rb +109 -0
  11. data/lib/nimbu-api/connection.rb +88 -0
  12. data/lib/nimbu-api/endpoint.rb +199 -0
  13. data/lib/nimbu-api/endpoints/authorizations.rb +113 -0
  14. data/lib/nimbu-api/endpoints/channels.rb +31 -0
  15. data/lib/nimbu-api/endpoints/channels/entries.rb +50 -0
  16. data/lib/nimbu-api/endpoints/sites.rb +34 -0
  17. data/lib/nimbu-api/endpoints/videos.rb +26 -0
  18. data/lib/nimbu-api/errors.rb +31 -0
  19. data/lib/nimbu-api/errors/bad_request.rb +14 -0
  20. data/lib/nimbu-api/errors/client_error.rb +20 -0
  21. data/lib/nimbu-api/errors/forbidden.rb +14 -0
  22. data/lib/nimbu-api/errors/internal_server_error.rb +15 -0
  23. data/lib/nimbu-api/errors/invalid_options.rb +18 -0
  24. data/lib/nimbu-api/errors/not_acceptable.rb +15 -0
  25. data/lib/nimbu-api/errors/not_found.rb +14 -0
  26. data/lib/nimbu-api/errors/required_params.rb +18 -0
  27. data/lib/nimbu-api/errors/service_error.rb +65 -0
  28. data/lib/nimbu-api/errors/service_unavailable.rb +15 -0
  29. data/lib/nimbu-api/errors/unauthorized.rb +15 -0
  30. data/lib/nimbu-api/errors/unknown_value.rb +18 -0
  31. data/lib/nimbu-api/errors/unprocessable_entity.rb +14 -0
  32. data/lib/nimbu-api/errors/validations.rb +18 -0
  33. data/lib/nimbu-api/pagination.rb +106 -0
  34. data/lib/nimbu-api/pagination/page_iterator.rb +157 -0
  35. data/lib/nimbu-api/pagination/page_links.rb +48 -0
  36. data/lib/nimbu-api/pagination/paged_request.rb +41 -0
  37. data/lib/nimbu-api/pagination/pagination.rb +102 -0
  38. data/lib/nimbu-api/request.rb +81 -0
  39. data/lib/nimbu-api/request/arguments.rb +171 -0
  40. data/lib/nimbu-api/request/basic_auth.rb +31 -0
  41. data/lib/nimbu-api/request/json.rb +46 -0
  42. data/lib/nimbu-api/request/normalizer.rb +29 -0
  43. data/lib/nimbu-api/request/oauth2.rb +42 -0
  44. data/lib/nimbu-api/request/parameter_filter.rb +34 -0
  45. data/lib/nimbu-api/request/validations.rb +25 -0
  46. data/lib/nimbu-api/request/validations/format.rb +26 -0
  47. data/lib/nimbu-api/request/validations/presence.rb +32 -0
  48. data/lib/nimbu-api/request/validations/required.rb +26 -0
  49. data/lib/nimbu-api/request/validations/token.rb +35 -0
  50. data/lib/nimbu-api/response.rb +34 -0
  51. data/lib/nimbu-api/response/header.rb +76 -0
  52. data/lib/nimbu-api/response/json.rb +29 -0
  53. data/lib/nimbu-api/response/mashify.rb +24 -0
  54. data/lib/nimbu-api/response/raise_error.rb +18 -0
  55. data/lib/nimbu-api/response/wrapper.rb +149 -0
  56. data/lib/nimbu-api/response/xmlize.rb +26 -0
  57. data/lib/nimbu-api/utils/all.rb +6 -0
  58. data/lib/nimbu-api/utils/constants.rb +37 -0
  59. data/lib/nimbu-api/utils/descendants.rb +11 -0
  60. data/lib/nimbu-api/utils/extend_array.rb +17 -0
  61. data/lib/nimbu-api/utils/extend_hash.rb +73 -0
  62. data/lib/nimbu-api/utils/json.rb +19 -0
  63. data/lib/nimbu-api/utils/url.rb +56 -0
  64. data/lib/nimbu-api/version.rb +3 -0
  65. data/nimbu-api.gemspec +31 -0
  66. metadata +294 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nimbu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Peter Dedene
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Nimbu
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nimbu'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nimbu
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/nimbu-api.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "nimbu-api/version"
2
+
3
+ require 'openssl'
4
+ require 'faraday'
5
+ require 'multi_json'
6
+
7
+ require "nimbu-api/utils/all"
8
+ require "nimbu-api/configuration"
9
+ require "nimbu-api/connection"
10
+ require 'nimbu-api/pagination'
11
+ require 'nimbu-api/response'
12
+ require 'nimbu-api/request'
13
+ require 'nimbu-api/authentication'
14
+ require 'nimbu-api/endpoint'
15
+ require 'nimbu-api/client'
16
+
17
+ require 'nimbu-api/endpoints/authorizations'
18
+ require 'nimbu-api/endpoints/sites'
19
+ require 'nimbu-api/endpoints/channels'
20
+ require 'nimbu-api/endpoints/channels/entries'
21
+ require 'nimbu-api/endpoints/videos'
22
+
23
+ module Nimbu
24
+ extend Configuration
25
+
26
+ class << self
27
+ def new(options = {}, &block)
28
+ Nimbu::Client.new(options, &block)
29
+ end
30
+
31
+ # Delegate to Nimbu::Client
32
+ #
33
+ def method_missing(method, *args, &block)
34
+ return super unless new.respond_to?(method)
35
+ new.send(method, *args, &block)
36
+ end
37
+
38
+ def respond_to?(method, include_private = false)
39
+ new.respond_to?(method, include_private) || super(method, include_private)
40
+ end
41
+
42
+ end
43
+ end # Nimbu
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ module Nimbu
4
+ module Authentication
5
+
6
+ attr_accessor :scopes
7
+
8
+ # Setup OAuth2 instance
9
+ def client(options={})
10
+ @client ||= ::OAuth2::Client.new(client_id, client_secret,
11
+ {
12
+ :site => options.fetch(:site) { Nimbu.site },
13
+ :authorize_url => 'login/oauth/authorize',
14
+ :token_url => 'login/oauth/access_token',
15
+ :ssl => { :verify => false }
16
+ }
17
+ )
18
+ end
19
+
20
+ # Strategy token
21
+ def auth_code
22
+ _verify_client
23
+ client.auth_code
24
+ end
25
+
26
+ # Sends authorization request to Nimbu.
27
+ # = Parameters
28
+ # * <tt>:redirect_uri</tt> - Required string.
29
+ # * <tt>:scope</tt> - Optional string. Comma separated list of scopes.
30
+ # Available scopes:
31
+ # * (no scope) - public read-only access (includes public user profile info, public repo info, and gists).
32
+ # * <tt>user</tt> - DB read/write access to profile info only.
33
+ # * <tt>public_repo</tt> - DB read/write access, and Git read access to public repos.
34
+ # * <tt>repo</tt> - DB read/write access, and Git read access to public and private repos.
35
+ # * <tt>gist</tt> - write access to gists.
36
+ #
37
+ def authorize_url(params = {})
38
+ _verify_client
39
+ client.auth_code.authorize_url(params)
40
+ end
41
+
42
+ # Makes request to token endpoint and retrieves access token value
43
+ def get_token(authorization_code, params = {})
44
+ _verify_client
45
+ client.auth_code.get_token(authorization_code, params)
46
+ end
47
+
48
+ # Check whether authentication credentials are present
49
+ def authenticated?
50
+ basic_authed? || oauth_token?
51
+ end
52
+
53
+ # Check whether basic authentication credentials are present
54
+ def basic_authed?
55
+ basic_auth? || (login? && password?)
56
+ end
57
+
58
+ # Select authentication parameters
59
+ def authentication
60
+ if basic_auth?
61
+ { :basic_auth => basic_auth }
62
+ elsif login? && password?
63
+ { :login => login, :password => password }
64
+ else
65
+ { }
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def _verify_client # :nodoc:
72
+ raise ArgumentError, 'Need to provide client_id and client_secret' unless client_id? && client_secret?
73
+ end
74
+
75
+ end # Authorization
76
+ end # Nimbu
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'nimbu-api/utils/all'
4
+
5
+ module Nimbu
6
+ class Builder
7
+
8
+ # Instantiates a new Nimbu api object
9
+ #
10
+ def self.new(klass, options={}, &block)
11
+ return create_instance(klass, options, &block) if klass
12
+ raise ArgumentError, 'must provide Endpoint class to be instantiated'
13
+ end
14
+
15
+ # Passes configuration options to instantiated class
16
+ #
17
+ def self.create_instance(klass, options, &block)
18
+ options.symbolize_keys!
19
+ convert_to_constant(klass.to_s).new options, &block
20
+ end
21
+
22
+ # Convert name to constant
23
+ #
24
+ def self.convert_to_constant(classes)
25
+ classes.split('::').inject(Nimbu::Endpoints) do |constant, klass|
26
+ constant.const_get klass
27
+ end
28
+ end
29
+
30
+ end # Builder
31
+ end # Nimbu
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'nimbu-api/builder'
4
+
5
+ module Nimbu
6
+ class Client < Endpoint
7
+
8
+ def oauth(options={}, &block)
9
+ Nimbu::Builder.new('Authorizations', current_options.merge(options), &block)
10
+ end
11
+ alias :authorizations :oauth
12
+
13
+ def sites(options={}, &block)
14
+ Nimbu::Builder.new('Sites', current_options.merge(options), &block)
15
+ end
16
+
17
+ def channels(options={}, &block)
18
+ Nimbu::Builder.new('Channels', current_options.merge(options), &block)
19
+ end
20
+
21
+ def videos(options={}, &block)
22
+ Nimbu::Builder.new('Videos', current_options.merge(options), &block)
23
+ end
24
+
25
+ end # Client
26
+ end # Nimbu
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+
3
+ module Nimbu
4
+ module Configuration
5
+
6
+ VALID_OPTIONS_KEYS = [
7
+ :client_id,
8
+ :client_secret,
9
+ :oauth_token,
10
+ :endpoint,
11
+ :site,
12
+ :ssl,
13
+ :mime_type,
14
+ :user_agent,
15
+ :connection_options,
16
+ :login,
17
+ :password,
18
+ :basic_auth,
19
+ :auto_pagination,
20
+ :adapter
21
+ ].freeze
22
+
23
+ # By default, don't set an application key
24
+ DEFAULT_CLIENT_ID = nil
25
+
26
+ # By default, don't set an application secret
27
+ DEFAULT_CLIENT_SECRET = nil
28
+
29
+ # By default, don't set a user oauth access token
30
+ DEFAULT_OAUTH_TOKEN = nil
31
+
32
+ # By default, don't set a user login name
33
+ DEFAULT_LOGIN = nil
34
+
35
+ # By default, don't set a user password
36
+ DEFAULT_PASSWORD = nil
37
+
38
+ # By default, don't set a user basic authentication
39
+ DEFAULT_BASIC_AUTH = nil
40
+
41
+ # The api endpoint used to connect to Nimbu if none is set
42
+ DEFAULT_ENDPOINT = 'https://api.nimbu.io'.freeze
43
+
44
+ # The web endpoint used to connect to Nimbu if none is set
45
+ DEFAULT_SITE = 'https://www.nimbu.io'.freeze
46
+
47
+ # The default SSL configuration
48
+ DEFAULT_SSL = {}
49
+
50
+ # The value sent in the http header for 'User-Agent' if none is set
51
+ DEFAULT_USER_AGENT = "Nimbu Ruby Gem #{Nimbu::VERSION}".freeze
52
+
53
+ # By default the <tt>Accept</tt> header will make a request for <tt>JSON</tt>
54
+ DEFAULT_MIME_TYPE = :json
55
+
56
+ # By default uses the Faraday connection options if none is set
57
+ DEFAULT_CONNECTION_OPTIONS = {}
58
+
59
+ # By default, don't traverse the page links
60
+ DEFAULT_AUTO_PAGINATION = false
61
+
62
+ # Other adapters are :typhoeus, :patron, :em_synchrony, :excon, :test
63
+ DEFAULT_ADAPTER = :net_http
64
+
65
+ attr_accessor *VALID_OPTIONS_KEYS
66
+
67
+ # Convenience method to allow for global setting of configuration options
68
+ def configure
69
+ yield self
70
+ end
71
+
72
+ def self.extended(base)
73
+ base.reset!
74
+ end
75
+
76
+ class << self
77
+ def keys
78
+ VALID_OPTIONS_KEYS
79
+ end
80
+ end
81
+
82
+ def options
83
+ options = {}
84
+ VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
85
+ options
86
+ end
87
+
88
+ # Reset configuration options to their defaults
89
+ #
90
+ def reset!
91
+ self.client_id = DEFAULT_CLIENT_ID
92
+ self.client_secret = DEFAULT_CLIENT_SECRET
93
+ self.oauth_token = DEFAULT_OAUTH_TOKEN
94
+ self.endpoint = DEFAULT_ENDPOINT
95
+ self.site = DEFAULT_SITE
96
+ self.ssl = DEFAULT_SSL
97
+ self.user_agent = DEFAULT_USER_AGENT
98
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
99
+ self.mime_type = DEFAULT_MIME_TYPE
100
+ self.login = DEFAULT_LOGIN
101
+ self.password = DEFAULT_PASSWORD
102
+ self.basic_auth = DEFAULT_BASIC_AUTH
103
+ self.auto_pagination = DEFAULT_AUTO_PAGINATION
104
+ self.adapter = DEFAULT_ADAPTER
105
+ self
106
+ end
107
+
108
+ end # Configuration
109
+ end # Nimbu
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+
3
+ module Nimbu
4
+ module Connection
5
+ extend self
6
+ include Nimbu::Utils::Constants
7
+
8
+ ALLOWED_OPTIONS = [
9
+ :headers,
10
+ :url,
11
+ :params,
12
+ :request,
13
+ :ssl
14
+ ].freeze
15
+
16
+ def default_options(options={})
17
+ {
18
+ # :headers => {
19
+ # # ACCEPT => "application/json",
20
+ # # ACCEPT_CHARSET => "utf-8",
21
+ # # USER_AGENT => user_agent,
22
+ # # CONTENT_TYPE => 'application/json'
23
+ # },
24
+ :ssl => options.fetch(:ssl) { ssl },
25
+ :url => options.fetch(:endpoint) { Nimbu.endpoint }
26
+ }.merge(options)
27
+ end
28
+
29
+ # Default middleware stack that uses default adapter as specified at
30
+ # configuration stage.
31
+ #
32
+ def default_middleware(options={})
33
+ Proc.new do |builder|
34
+ unless options[:with_attachments]
35
+ builder.use Nimbu::Request::Json
36
+ end
37
+ builder.use Faraday::Request::Multipart
38
+ builder.use Faraday::Request::UrlEncoded
39
+ builder.use Nimbu::Request::OAuth2, oauth_token if oauth_token?
40
+ builder.use Nimbu::Request::BasicAuth, authentication if basic_authed?
41
+
42
+ builder.use Faraday::Response::Logger if ENV['DEBUG']
43
+ unless options[:raw]
44
+ builder.use Nimbu::Response::Mashify
45
+ builder.use Nimbu::Response::Json
46
+ end
47
+ builder.use Nimbu::Response::RaiseError
48
+ builder.adapter adapter
49
+ end
50
+ end
51
+
52
+ @connection = nil
53
+
54
+ @stack = nil
55
+
56
+ def clear_cache
57
+ @connection = nil
58
+ end
59
+
60
+ def caching?
61
+ !@connection.nil?
62
+ end
63
+
64
+ # Exposes middleware builder to facilitate custom stacks and easy
65
+ # addition of new extensions such as cache adapter.
66
+ #
67
+ def stack(options={}, &block)
68
+ @stack ||= begin
69
+ if block_given?
70
+ Faraday::Builder.new(&block)
71
+ else
72
+ Faraday::Builder.new(&default_middleware(options))
73
+ end
74
+ end
75
+ end
76
+
77
+ # Returns a Fraday::Connection object
78
+ #
79
+ def connection(options={})
80
+ conn_options = default_options(options)
81
+ clear_cache unless options.empty?
82
+ puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
83
+
84
+ Faraday.new(conn_options.merge(:builder => stack(options)))
85
+ end
86
+
87
+ end # Connection
88
+ end # Nimbu