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.



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

.credentialsObject (readonly)

Returns the value of attribute credentials.



69
70
71
# File 'lib/mode/api/request.rb', line 69

def credentials
  @credentials
end

.environmentObject (readonly)

Configuration



68
69
70
# File 'lib/mode/api/request.rb', line 68

def environment
  @environment
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

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



143
144
145
# File 'lib/mode/api/request.rb', line 143

def access_tokens_path
  "#{base_path}/access_tokens"
end

.base_pathObject

Routes



135
136
137
# File 'lib/mode/api/request.rb', line 135

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

.base_urlObject



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

.data_source_connection_messages_pathObject



156
157
158
# File 'lib/mode/api/request.rb', line 156

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

.data_source_connection_pathObject



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_pathObject



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(options = {})
  @_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_pathObject



139
140
141
# File 'lib/mode/api/request.rb', line 139

def packages_path
  "#{base_path}/packages"
end

.passwordObject



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

.usernameObject



82
83
84
85
# File 'lib/mode/api/request.rb', line 82

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

.valid?Boolean

Returns:

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

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