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
user.access_tokens.
-
.base_path ⇒ Object
Routes.
- .base_url ⇒ Object
- .configure(environment, options = {}) ⇒ Object
-
.data_source_connection_messages_path ⇒ Object
connection.links(‘messages’).follow().
- .data_source_connection_path ⇒ Object
-
.data_source_connections_path ⇒ Object
access_token.data_source_connections.
- .delete(path, params = {}, opts = {}) ⇒ Object
- .get(path, params = {}, opts = {}) ⇒ Object
-
.head(path, opts = {}) ⇒ Object
Request Methods.
-
.http(options = {}) ⇒ Object
Connection.
- .packages_path ⇒ Object
- .password ⇒ Object
- .patch(path, params = {}, opts = {}) ⇒ Object
- .post(path, params = {}, opts = {}) ⇒ Object
- .put(path, params = {}, opts = {}) ⇒ 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(opts = {}) ⇒ Object
Constructor Details
#initialize(method, path, params = {}) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 |
# File 'lib/mode/api/request.rb', line 11 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.
76 77 78 |
# File 'lib/mode/api/request.rb', line 76 def access_token @access_token end |
.credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
75 76 77 |
# File 'lib/mode/api/request.rb', line 75 def credentials @credentials end |
.environment ⇒ Object (readonly)
Configuration
74 75 76 |
# File 'lib/mode/api/request.rb', line 74 def environment @environment end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
9 10 11 |
# File 'lib/mode/api/request.rb', line 9 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/mode/api/request.rb', line 9 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/mode/api/request.rb', line 9 def path @path end |
Class Method Details
.access_tokens_path ⇒ Object
user.access_tokens
154 155 156 |
# File 'lib/mode/api/request.rb', line 154 def access_tokens_path "#{base_path}/access_tokens" end |
.base_path ⇒ Object
Routes
145 146 147 |
# File 'lib/mode/api/request.rb', line 145 def base_path "/api/#{username}" end |
.base_url ⇒ Object
190 191 192 193 194 |
# File 'lib/mode/api/request.rb', line 190 def base_url @base_url ||= env_uri_klass.build( :host => env_host, :port => env_port ) end |
.configure(environment, options = {}) ⇒ Object
103 104 105 106 107 |
# File 'lib/mode/api/request.rb', line 103 def configure(environment, = {}) @environment = environment @credentials = ['credentials'] @access_token = ['access_token'] end |
.data_source_connection_messages_path ⇒ Object
connection.links(‘messages’).follow()
169 170 171 |
# File 'lib/mode/api/request.rb', line 169 def [data_source_connection_path, 'messages'].join('/').strip end |
.data_source_connection_path ⇒ Object
163 164 165 166 |
# File 'lib/mode/api/request.rb', line 163 def data_source_connection_path token = access_token && access_token['token'] [data_source_connections_path, token].join('/').strip end |
.data_source_connections_path ⇒ Object
access_token.data_source_connections
159 160 161 |
# File 'lib/mode/api/request.rb', line 159 def data_source_connections_path "#{base_path}/data_source_connections" end |
.delete(path, params = {}, opts = {}) ⇒ Object
133 134 135 |
# File 'lib/mode/api/request.rb', line 133 def delete(path, params = {}, opts = {}) new(:delete, resolve_path(path), params).perform(opts) end |
.get(path, params = {}, opts = {}) ⇒ Object
117 118 119 |
# File 'lib/mode/api/request.rb', line 117 def get(path, params = {}, opts = {}) new(:get, resolve_path(path), params).perform(opts) end |
.head(path, opts = {}) ⇒ Object
Request Methods
113 114 115 |
# File 'lib/mode/api/request.rb', line 113 def head(path, opts = {}) new(:head, resolve_path(path)).perform(opts) end |
.http(options = {}) ⇒ Object
Connection
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mode/api/request.rb', line 176 def http( = {}) @_http ||= Faraday.new(:url => base_url) do |builder| builder.request :multipart # adds small overhead to each request builder.request :url_encoded builder.request :json # requires middleware builder.basic_auth(username, password) builder.use FaradayMiddleware::FollowRedirects, limit: 5 builder.adapter Faraday.default_adapter end end |
.packages_path ⇒ Object
149 150 151 |
# File 'lib/mode/api/request.rb', line 149 def packages_path "#{base_path}/packages" end |
.password ⇒ Object
93 94 95 96 |
# File 'lib/mode/api/request.rb', line 93 def password validate! @credentials['password'] end |
.patch(path, params = {}, opts = {}) ⇒ Object
129 130 131 |
# File 'lib/mode/api/request.rb', line 129 def patch(path, params = {}, opts = {}) new(:patch, resolve_path(path), params).perform(opts) end |
.post(path, params = {}, opts = {}) ⇒ Object
121 122 123 |
# File 'lib/mode/api/request.rb', line 121 def post(path, params = {}, opts = {}) new(:post, resolve_path(path), params).perform(opts) end |
.put(path, params = {}, opts = {}) ⇒ Object
125 126 127 |
# File 'lib/mode/api/request.rb', line 125 def put(path, params = {}, opts = {}) new(:put, resolve_path(path), params).perform(opts) end |
.resolve_path(path) ⇒ Object
137 138 139 |
# File 'lib/mode/api/request.rb', line 137 def resolve_path(path) path.is_a?(Symbol) ? send("#{path}_path".to_sym) : path end |
.username ⇒ Object
88 89 90 91 |
# File 'lib/mode/api/request.rb', line 88 def username validate! @credentials['username'] end |
.valid? ⇒ Boolean
78 79 80 |
# File 'lib/mode/api/request.rb', line 78 def valid? !environment.nil? end |
.validate! ⇒ Object
82 83 84 85 86 |
# File 'lib/mode/api/request.rb', line 82 def validate! unless valid? raise "Request API not configured. Please provide an API environment." end end |
Instance Method Details
#perform(opts = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mode/api/request.rb', line 17 def perform(opts = {}) response = http.send(method.to_sym) do |request| request.url path request.body = params unless params.empty? if opts[:content_type] request.headers['Content-Type'] = opts[:content_type] end request.[:timeout] = opts.fetch(:timeout, 30) end response.success? ? success(response) : error(response) end |