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, #document, #documents, #update_document

Methods included from Schemas

#schema, #schemas

Constructor Details

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

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

Raises:



18
19
20
21
22
# File 'lib/montage/client.rb', line 18

def initialize
  @api_version = 1
  yield(self) if block_given?
  raise MissingAttributeError, "You must declare the domain attribute" unless @domain
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

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#url_prefixObject

Returns the value of attribute url_prefix.



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

def url_prefix
  @url_prefix
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



32
33
34
35
36
37
38
39
40
# File 'lib/montage/client.rb', line 32

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, &block) ⇒ Object



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

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

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

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

  response_object
end

#connectionObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/montage/client.rb', line 101

def connection
  @connect ||= Faraday.new do |f|
    f.adapter :net_http
    f.url_prefix = "#{url_prefix || default_url_prefix}/api/v#{api_version}/"
    f.headers["User-Agent"] = "Montage Ruby v#{Montage::VERSION}"
    f.headers["Content-Type"] = content_type
    f.headers["Accept"] = "*/*"
    f.headers["Authorization"] = "Token #{token}"
    f.response :json, content_type: /\bjson$/
  end
end

#content_typeObject



28
29
30
# File 'lib/montage/client.rb', line 28

def content_type
  "application/json"
end

#default_url_prefixObject



24
25
26
# File 'lib/montage/client.rb', line 24

def default_url_prefix
  "http://#{domain}.dev.montagehot.club"
end

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



69
70
71
72
73
74
75
76
# File 'lib/montage/client.rb', line 69

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

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



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

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



51
52
53
54
55
56
57
58
# File 'lib/montage/client.rb', line 51

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



60
61
62
63
64
65
66
67
# File 'lib/montage/client.rb', line 60

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

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/montage/client.rb', line 83

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

#set_token(token) ⇒ Object



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

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