Class: Mode::API::Request
- Inherits:
-
Object
- Object
- Mode::API::Request
- Defined in:
- lib/mode/api/request.rb
Class Attribute Summary collapse
-
.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_token ⇒ Object
- .access_tokens_path ⇒ Object
-
.base_path ⇒ Object
Routes.
- .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) ⇒ Object
-
.head(path) ⇒ Object
Request Methods.
-
.http(options = {}) ⇒ Object
Connection.
- .packages_path ⇒ 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 ⇒ 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
.credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
61 62 63 |
# File 'lib/mode/api/request.rb', line 61 def credentials @credentials end |
.environment ⇒ Object (readonly)
Configuration
60 61 62 |
# File 'lib/mode/api/request.rb', line 60 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_token ⇒ Object
78 79 80 81 |
# File 'lib/mode/api/request.rb', line 78 def access_token validate! @credentials[:access_token] end |
.access_tokens_path ⇒ Object
124 125 126 |
# File 'lib/mode/api/request.rb', line 124 def access_tokens_path "#{base_path}/access_tokens" end |
.base_path ⇒ Object
Routes
120 121 122 |
# File 'lib/mode/api/request.rb', line 120 def base_path "/api/#{username}" end |
.configure(environment, options = {}) ⇒ Object
83 84 85 86 |
# File 'lib/mode/api/request.rb', line 83 def configure(environment, = {}) @environment = environment @credentials = [:credentials] end |
.data_source_connection_messages_path ⇒ Object
140 141 142 |
# File 'lib/mode/api/request.rb', line 140 def [data_source_connection_path, 'messages'].join('/').strip end |
.data_source_connection_path ⇒ Object
136 137 138 |
# File 'lib/mode/api/request.rb', line 136 def data_source_connection_path [data_source_connections_path, access_token].join('/').strip end |
.data_source_connections_path ⇒ Object
132 133 134 |
# File 'lib/mode/api/request.rb', line 132 def data_source_connections_path "#{base_path}/data_source_connections" end |
.delete(path, params = {}) ⇒ Object
108 109 110 |
# File 'lib/mode/api/request.rb', line 108 def delete(path, params = {}) new(:delete, resolve_path(path), params).perform end |
.get(path) ⇒ Object
96 97 98 |
# File 'lib/mode/api/request.rb', line 96 def get(path) new(:get, resolve_path(path)).perform end |
.head(path) ⇒ Object
Request Methods
92 93 94 |
# File 'lib/mode/api/request.rb', line 92 def head(path) new(:head, resolve_path(path)).perform end |
.http(options = {}) ⇒ Object
Connection
147 148 149 150 151 152 153 154 |
# File 'lib/mode/api/request.rb', line 147 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, access_token) end end |
.packages_path ⇒ Object
128 129 130 |
# File 'lib/mode/api/request.rb', line 128 def packages_path "#{base_path}/packages" end |
.post(path, params = {}) ⇒ Object
100 101 102 |
# File 'lib/mode/api/request.rb', line 100 def post(path, params = {}) new(:post, resolve_path(path), params).perform end |
.put(path, params = {}) ⇒ Object
104 105 106 |
# File 'lib/mode/api/request.rb', line 104 def put(path, params = {}) new(:put, resolve_path(path), params).perform end |
.resolve_path(path) ⇒ Object
112 113 114 |
# File 'lib/mode/api/request.rb', line 112 def resolve_path(path) path.is_a?(Symbol) ? send("#{path}_path".to_sym) : path end |
.username ⇒ Object
73 74 75 76 |
# File 'lib/mode/api/request.rb', line 73 def username validate! @credentials[:username] end |
.valid? ⇒ Boolean
63 64 65 |
# File 'lib/mode/api/request.rb', line 63 def valid? !environment.nil? end |
.validate! ⇒ Object
67 68 69 70 71 |
# File 'lib/mode/api/request.rb', line 67 def validate! unless valid? raise "Request API not configured. Please provide an API environment." end end |
Instance Method Details
#perform ⇒ Object
16 17 18 19 |
# File 'lib/mode/api/request.rb', line 16 def perform response = http.send(method.to_sym, path, params) response.success? ? success(response) : error(response) end |