Class: Montage::Client

Inherits:
Object
  • Object
show all
Includes:
Documents, Files, Schemas
Defined in:
lib/montage/client.rb,
lib/montage/client/files.rb,
lib/montage/client/schemas.rb,
lib/montage/client/documents.rb

Defined Under Namespace

Modules: Documents, Files, Schemas

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Documents

#create_or_update_documents, #delete_document, #documents, #update_document

Methods included from Schemas

#schema, #schemas

Constructor Details

#initialize {|_self| ... } ⇒ Client

Initializes the client instance

  • Attributes :

    • token -> API access token required for requests, does not expire

    • username -> Montage username credential

    • password -> Montage password credential

    • domain -> Project subdomain, required for initialization

    • api_version -> API version to query against, defaults to 1

    • environment -> Specifies desired environment for requests, defaults to ‘production’. Valid options are ‘development’, ‘production’, and ‘test’.

    • test_url -> Test URL. Required when environment is set to ‘test’.

  • Returns :

    • A valid Montage::Client instance

  • Raises :

    • MissingAttributeError -> If the domain attribute is not specified

    • InvalidEnvironment -> If the environment attribute is not set to ‘production’ or ‘development’

    • MissingAttributeError -> If the test_url is not provided when environment is ‘test’

Yields:

  • (_self)

Yield Parameters:



42
43
44
45
46
47
48
49
# File 'lib/montage/client.rb', line 42

def initialize
  @api_version = 1
  @environment ||= "production"
  yield(self) if block_given?
  fail MissingAttributeError, "You must declare the domain attribute" unless @domain
  fail InvalidEnvironment, "Valid options are 'production' and 'development'" unless environment_valid?
  fail MissingAttributeError, "You must include a test_url for the 'test' environment" unless test_environment_valid?
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



16
17
18
# File 'lib/montage/client.rb', line 16

def api_version
  @api_version
end

#domainObject

Returns the value of attribute domain.



16
17
18
# File 'lib/montage/client.rb', line 16

def domain
  @domain
end

#environmentObject

Returns the value of attribute environment.



16
17
18
# File 'lib/montage/client.rb', line 16

def environment
  @environment
end

#passwordObject

Returns the value of attribute password.



16
17
18
# File 'lib/montage/client.rb', line 16

def password
  @password
end

#test_urlObject

Returns the value of attribute test_url.



16
17
18
# File 'lib/montage/client.rb', line 16

def test_url
  @test_url
end

#tokenObject

Returns the value of attribute token.



16
17
18
# File 'lib/montage/client.rb', line 16

def token
  @token
end

#usernameObject

Returns the value of attribute username.



16
17
18
# File 'lib/montage/client.rb', line 16

def username
  @username
end

Instance Method Details

#authObject

Attempts to authenticate with the Montage API

  • Returns :

    • A hash containing a valid token or an error string, oh no!



88
89
90
91
92
93
94
95
96
# File 'lib/montage/client.rb', line 88

def auth
  build_response("token") do
    connection.post do |req|
      req.headers.delete("Authorization")
      req.url "auth/"
      req.body = { username: username, password: password }.to_json
    end
  end
end

#build_response(resource_name) ⇒ Object

Instantiates a response object based on the yielded block

  • Args :

    • resource_name -> The name of the Montage resource

  • Returns :

    • A Montage::Response Object containing:

      • A http status code

      • The response body

      • The resource name



214
215
216
217
218
219
220
221
222
223
# File 'lib/montage/client.rb', line 214

def build_response(resource_name)
  response = yield
  resource = response_successful?(response) ? resource_name : "error"

  response_object = Montage::Response.new(response.status, response.body, resource)

  set_token(response_object.token.value) if resource_name == "token" && response.success?

  response_object
end

#connectionObject

Creates an Faraday connection instance for requests

  • Returns :

    • A Faraday connection object with an instance specific configuration



245
246
247
248
249
250
251
252
# File 'lib/montage/client.rb', line 245

def connection
  @connect ||= Faraday.new do |f|
    f.adapter :net_http
    f.headers = connection_headers
    f.url_prefix = "#{default_url_prefix}/api/v#{api_version}/"
    f.response :json, content_type: /\bjson$/
  end
end

#connection_headersObject

Supplies the Faraday connection with proper headers

  • Returns :

    • A hash of instance specific headers for requests



230
231
232
233
234
235
236
237
238
# File 'lib/montage/client.rb', line 230

def connection_headers
  {
    "User-Agent" => "Montage Ruby v#{Montage::VERSION}",
    "Content-Type" => "application/json",
    "Accept" => "*/*",
    "Authorization" => "Token #{token}",
    "Referer" => "#{default_url_prefix}/"
  }
end

#default_url_prefixObject

Generates a base url for requests using the supplied environment and domain

  • Returns :

    • A string containing the constructed url



75
76
77
78
79
80
81
# File 'lib/montage/client.rb', line 75

def default_url_prefix
  case @environment
  when "development" then "https://#{domain}.dev.montagehot.club"
  when "production" then "https://#{domain}.mntge.com"
  when "test" then @test_url
  end
end

#delete(url, resource_name, options = {}) ⇒ Object

Removes an existing Montage resource with a JSON options string, TODO:ADD EXAMPLES

  • Args :

    • url -> The url of the targeted resource

    • resource_name -> The name of the targeted resource

    • options -> A hash of desired options

  • Returns :

    • A Montage::Response Object containing:

      • A http status code

      • The response body

      • The resource name



173
174
175
176
177
178
179
180
# File 'lib/montage/client.rb', line 173

def delete(url, resource_name, options = {})
  build_response(resource_name) do
    connection.delete do |req|
      req.url url
      req.body = options.to_json
    end
  end
end

#environment_valid?Boolean

Verifies the Montage::Client instance environment

  • Returns :

    • A boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/montage/client.rb', line 56

def environment_valid?
  %w(test production development).include?(@environment)
end

#get(url, resource_name, options = {}) ⇒ Object

Requests resources from the Montage API, TODO:ADD EXAMPLES

  • Args :

    • url -> The url of the targeted resource

    • resource_name -> The name of the targeted resource

    • options -> A hash of desired options

  • Returns :

    • A Montage::Response Object containing:

      • A http status code

      • The response body

      • The resource name



110
111
112
113
114
115
116
117
# File 'lib/montage/client.rb', line 110

def get(url, resource_name, options = {})
  build_response(resource_name) do
    connection.get do |req|
      req.url url
      req.params = options
    end
  end
end

#post(url, resource_name, options = {}) ⇒ Object

Posts to the Montage API with a JSON options string, TODO:ADD EXAMPLES

  • Args :

    • url -> The url of the targeted resource

    • resource_name -> The name of the targeted resource

    • options -> A hash of desired options

  • Returns :

    • A Montage::Response Object containing:

      • A http status code

      • The response body

      • The resource name



131
132
133
134
135
136
137
138
# File 'lib/montage/client.rb', line 131

def post(url, resource_name, options = {})
  build_response(resource_name) do
    connection.post do |req|
      req.url url
      req.body = options.to_json
    end
  end
end

#put(url, resource_name, options = {}) ⇒ Object

Updates an existing Montage resource with a JSON options string, TODO:ADD EXAMPLES

  • Args :

    • url -> The url of the targeted resource

    • resource_name -> The name of the targeted resource

    • options -> A hash of desired options

  • Returns :

    • A Montage::Response Object containing:

      • A http status code

      • The response body

      • The resource name



152
153
154
155
156
157
158
159
# File 'lib/montage/client.rb', line 152

def put(url, resource_name, options = {})
  build_response(resource_name) do
    connection.put do |req|
      req.url url
      req.body = options.to_json
    end
  end
end

#response_successful?(response) ⇒ Boolean

Checks the response body for an errors key and a successful http status code

  • Args :

    • response -> The Montage API response

  • Returns :

    • A boolean

Returns:

  • (Boolean)


199
200
201
202
# File 'lib/montage/client.rb', line 199

def response_successful?(response)
  return false if response.body["errors"]
  response.success?
end

#set_token(token) ⇒ Object

Sets the authentication token on the client instance and http headers

  • Returns :

    • A string with the proper token interpolated



187
188
189
190
# File 'lib/montage/client.rb', line 187

def set_token(token)
  @token = token
  connection.headers["Authorization"] = "Token #{token}"
end

#test_environment_valid?Boolean

Verifies that the test_url is supplied when the environment is set to test

  • Returns :

    • A boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/montage/client.rb', line 65

def test_environment_valid?
  return true unless @environment == "test"
  !!@test_url
end