Class: Mode::API::Request
- Inherits:
-
Object
- Object
- Mode::API::Request
- Defined in:
- lib/mode/api/request.rb
Class Attribute Summary collapse
-
.access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
.credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
.environment ⇒ Object
readonly
Configuration.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .access_tokens_path ⇒ Object
-
.base_path ⇒ Object
Routes.
- .base_url ⇒ Object
- .configure(environment, options = {}) ⇒ Object
- .data_source_connection_messages_path ⇒ Object
- .data_source_connection_path ⇒ Object
- .data_source_connections_path ⇒ Object
- .delete(path, params = {}) ⇒ Object
- .get(path, params = {}) ⇒ Object
-
.head(path) ⇒ Object
Request Methods.
-
.http(options = {}) ⇒ Object
Connection.
- .packages_path ⇒ Object
- .password ⇒ Object
- .post(path, params = {}) ⇒ Object
- .put(path, params = {}) ⇒ Object
- .resolve_path(path) ⇒ Object
- .username ⇒ Object
- .valid? ⇒ Boolean
- .validate! ⇒ Object
Instance Method Summary collapse
-
#initialize(method, path, params = {}) ⇒ Request
constructor
A new instance of Request.
- #perform(request_opts = {}) ⇒ Object
Constructor Details
#initialize(method, path, params = {}) ⇒ Request
Returns a new instance of Request.
10 11 12 13 14 |
# File 'lib/mode/api/request.rb', line 10 def initialize(method, path, params = {}) @method = method @path = path @params = params end |
Class Attribute Details
.access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
70 71 72 |
# File 'lib/mode/api/request.rb', line 70 def access_token @access_token end |
.credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
69 70 71 |
# File 'lib/mode/api/request.rb', line 69 def credentials @credentials end |
.environment ⇒ Object (readonly)
Configuration
68 69 70 |
# File 'lib/mode/api/request.rb', line 68 def environment @environment end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
8 9 10 |
# File 'lib/mode/api/request.rb', line 8 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/mode/api/request.rb', line 8 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/mode/api/request.rb', line 8 def path @path end |
Class Method Details
.access_tokens_path ⇒ Object
143 144 145 |
# File 'lib/mode/api/request.rb', line 143 def access_tokens_path "#{base_path}/access_tokens" end |
.base_path ⇒ Object
Routes
135 136 137 |
# File 'lib/mode/api/request.rb', line 135 def base_path "/api/#{username}" end |
.base_url ⇒ Object
172 173 174 175 176 |
# File 'lib/mode/api/request.rb', line 172 def base_url @base_url ||= env_uri_klass.build( :host => env_host, :port => env_port ) end |
.configure(environment, options = {}) ⇒ Object
97 98 99 100 101 |
# File 'lib/mode/api/request.rb', line 97 def configure(environment, = {}) @environment = environment @credentials = ['credentials'] @access_token = ['access_token'] end |
.data_source_connection_messages_path ⇒ Object
156 157 158 |
# File 'lib/mode/api/request.rb', line 156 def [data_source_connection_path, 'messages'].join('/').strip end |
.data_source_connection_path ⇒ Object
151 152 153 154 |
# File 'lib/mode/api/request.rb', line 151 def data_source_connection_path token = access_token && access_token['token'] [data_source_connections_path, token].join('/').strip end |
.data_source_connections_path ⇒ Object
147 148 149 |
# File 'lib/mode/api/request.rb', line 147 def data_source_connections_path "#{base_path}/data_source_connections" end |
.delete(path, params = {}) ⇒ Object
123 124 125 |
# File 'lib/mode/api/request.rb', line 123 def delete(path, params = {}) new(:delete, resolve_path(path), params).perform end |
.get(path, params = {}) ⇒ Object
111 112 113 |
# File 'lib/mode/api/request.rb', line 111 def get(path, params = {}) new(:get, resolve_path(path), params).perform end |
.head(path) ⇒ Object
Request Methods
107 108 109 |
# File 'lib/mode/api/request.rb', line 107 def head(path) new(:head, resolve_path(path)).perform end |
.http(options = {}) ⇒ Object
Connection
163 164 165 166 167 168 169 170 |
# File 'lib/mode/api/request.rb', line 163 def http( = {}) @_http ||= Faraday.new(:url => base_url) do |builder| builder.request :multipart # adds small overhead to each request builder.request :url_encoded builder.adapter Faraday.default_adapter builder.basic_auth(username, password) end end |
.packages_path ⇒ Object
139 140 141 |
# File 'lib/mode/api/request.rb', line 139 def packages_path "#{base_path}/packages" end |
.password ⇒ Object
87 88 89 90 |
# File 'lib/mode/api/request.rb', line 87 def password validate! @credentials['password'] end |
.post(path, params = {}) ⇒ Object
115 116 117 |
# File 'lib/mode/api/request.rb', line 115 def post(path, params = {}) new(:post, resolve_path(path), params).perform end |
.put(path, params = {}) ⇒ Object
119 120 121 |
# File 'lib/mode/api/request.rb', line 119 def put(path, params = {}) new(:put, resolve_path(path), params).perform end |
.resolve_path(path) ⇒ Object
127 128 129 |
# File 'lib/mode/api/request.rb', line 127 def resolve_path(path) path.is_a?(Symbol) ? send("#{path}_path".to_sym) : path end |
.username ⇒ Object
82 83 84 85 |
# File 'lib/mode/api/request.rb', line 82 def username validate! @credentials['username'] end |
.valid? ⇒ Boolean
72 73 74 |
# File 'lib/mode/api/request.rb', line 72 def valid? !environment.nil? end |
.validate! ⇒ Object
76 77 78 79 80 |
# File 'lib/mode/api/request.rb', line 76 def validate! unless valid? raise "Request API not configured. Please provide an API environment." end end |
Instance Method Details
#perform(request_opts = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/mode/api/request.rb', line 16 def perform(request_opts = {}) response = http.send(method.to_sym) do |request| request.url path request.body = params unless params.empty? request.[:timeout] = request_opts.fetch(:timeout, 30) end response.success? ? success(response) : error(response) end |