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.



64
65
66
# File 'lib/mode/api/request.rb', line 64

def access_token
  @access_token
end

.credentialsObject (readonly)

Returns the value of attribute credentials.



63
64
65
# File 'lib/mode/api/request.rb', line 63

def credentials
  @credentials
end

.environmentObject (readonly)

Configuration



62
63
64
# File 'lib/mode/api/request.rb', line 62

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



137
138
139
# File 'lib/mode/api/request.rb', line 137

def access_tokens_path
  "#{base_path}/access_tokens"
end

.base_pathObject

Routes



129
130
131
# File 'lib/mode/api/request.rb', line 129

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

.configure(environment, options = {}) ⇒ Object



91
92
93
94
95
# File 'lib/mode/api/request.rb', line 91

def configure(environment, options = {})
  @environment = environment
  @credentials = options['credentials']
  @access_token = options['access_token']
end

.data_source_connection_messages_pathObject



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

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

.data_source_connection_pathObject



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

def data_source_connection_path
  token = access_token && access_token['token']
  [data_source_connections_path, token].join('/').strip
end

.data_source_connections_pathObject



141
142
143
# File 'lib/mode/api/request.rb', line 141

def data_source_connections_path
  "#{base_path}/data_source_connections"
end

.delete(path, params = {}) ⇒ Object



117
118
119
# File 'lib/mode/api/request.rb', line 117

def delete(path, params = {})
  new(:delete, resolve_path(path), params).perform
end

.get(path) ⇒ Object



105
106
107
# File 'lib/mode/api/request.rb', line 105

def get(path)
  new(:get, resolve_path(path)).perform
end

.head(path) ⇒ Object

Request Methods



101
102
103
# File 'lib/mode/api/request.rb', line 101

def head(path)
  new(:head, resolve_path(path)).perform
end

.http(options = {}) ⇒ Object

Connection



157
158
159
160
161
162
163
164
# File 'lib/mode/api/request.rb', line 157

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



133
134
135
# File 'lib/mode/api/request.rb', line 133

def packages_path
  "#{base_path}/packages"
end

.passwordObject



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

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

.post(path, params = {}) ⇒ Object



109
110
111
# File 'lib/mode/api/request.rb', line 109

def post(path, params = {})
  new(:post, resolve_path(path), params).perform
end

.put(path, params = {}) ⇒ Object



113
114
115
# File 'lib/mode/api/request.rb', line 113

def put(path, params = {})
  new(:put, resolve_path(path), params).perform
end

.resolve_path(path) ⇒ Object



121
122
123
# File 'lib/mode/api/request.rb', line 121

def resolve_path(path)
  path.is_a?(Symbol) ? send("#{path}_path".to_sym) : path
end

.usernameObject



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

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

.valid?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mode/api/request.rb', line 66

def valid?
  !environment.nil?
end

.validate!Object



70
71
72
73
74
# File 'lib/mode/api/request.rb', line 70

def validate!
  unless valid?
    raise "Request API not configured. Please provide an API environment."
  end
end

Instance Method Details

#performObject



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