Class: Mode::API::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mode/api/request.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_tokenObject (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

.credentialsObject (readonly)

Returns the value of attribute credentials.



75
76
77
# File 'lib/mode/api/request.rb', line 75

def credentials
  @credentials
end

.environmentObject (readonly)

Configuration



74
75
76
# File 'lib/mode/api/request.rb', line 74

def environment
  @environment
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/mode/api/request.rb', line 9

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/mode/api/request.rb', line 9

def params
  @params
end

#pathObject (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_pathObject

user.access_tokens



154
155
156
# File 'lib/mode/api/request.rb', line 154

def access_tokens_path
  "#{base_path}/access_tokens"
end

.base_pathObject

Routes



145
146
147
# File 'lib/mode/api/request.rb', line 145

def base_path
  "/api/#{username}"
end

.base_urlObject



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, options = {})
  @environment = environment
  @credentials = options['credentials']
  @access_token = options['access_token']
end

.data_source_connection_messages_pathObject

connection.links(‘messages’).follow()



169
170
171
# File 'lib/mode/api/request.rb', line 169

def data_source_connection_messages_path
  [data_source_connection_path, 'messages'].join('/').strip
end

.data_source_connection_pathObject



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_pathObject

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(options = {})
  @_http ||= Faraday.new(:url => base_url) do |builder|
    builder.request  :multipart # adds small overhead to each request
    builder.request  :url_encoded
    builder.request  :json

    builder.basic_auth(username, password)

    builder.use FaradayMiddleware::FollowRedirects, limit: 5

    builder.adapter  Faraday.default_adapter
  end
end

.packages_pathObject



149
150
151
# File 'lib/mode/api/request.rb', line 149

def packages_path
  "#{base_path}/packages"
end

.passwordObject



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

.usernameObject



88
89
90
91
# File 'lib/mode/api/request.rb', line 88

def username
  validate!
  @credentials['username']
end

.valid?Boolean

Returns:

  • (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.options[:timeout] = opts.fetch(:timeout, 30)
  end

  response.success? ? success(response) : error(response)
end