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

.credentialsObject (readonly)

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

.environmentObject (readonly)

Configuration



60
61
62
# File 'lib/mode/api/request.rb', line 60

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_tokenObject



78
79
80
81
# File 'lib/mode/api/request.rb', line 78

def access_token
  validate!
  @credentials[:access_token]
end

.access_tokens_pathObject



124
125
126
# File 'lib/mode/api/request.rb', line 124

def access_tokens_path
  "#{base_path}/access_tokens"
end

.base_pathObject

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

.data_source_connection_messages_pathObject



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

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

.data_source_connection_pathObject



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_pathObject



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(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, access_token)
  end
end

.packages_pathObject



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

.usernameObject



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

def username
  validate!
  @credentials[:username]
end

.valid?Boolean

Returns:

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

#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