Class: ZohoSdk::Analytics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho-sdk/analytics/client.rb

Overview

Allows retrieving workspaces, metadata, and other general data not tied to a specific resource.

Instance Method Summary collapse

Constructor Details

#initialize(email, client_id, client_secret, refresh_token) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zoho-sdk/analytics/client.rb', line 19

def initialize(email, client_id, client_secret, refresh_token)
  @email = email
  @client_id = client_id
  @client_secret = client_secret
  @refresh_token = refresh_token

  # Retrieves access token to authenticate all API requests
  conn =
    Faraday.new(url: API_AUTH_BASE_URL) { |conn| conn.adapter :net_http }
  res =
    conn.post do |req|
      payload = {
        "client_id" => @client_id,
        "client_secret" => @client_secret,
        "refresh_token" => @refresh_token,
        "grant_type" => "refresh_token"
      }
      payload.each { |key, value| req.params[key] = value }
    end
  if res.success?
    data = JSON.parse(res.body)
    if data["access_token"]
      @access_token = data["access_token"]
    else
      nil
    end
  else
    nil
  end
end

Instance Method Details

#create_workspace(name, **opts) ⇒ Workspace

Create a new Zoho Analytics workspace

Parameters:

  • name (String)

    Workspace name

  • opts (Hash)

    Optional arguments

Options Hash (**opts):

  • :description (String)

    Workspace description

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zoho-sdk/analytics/client.rb', line 62

def create_workspace(name, **opts)
  res =
    get params: {
          "ZOHO_ACTION" => "CREATEBLANKDB",
          "ZOHO_DATABASE_NAME" => name,
          "ZOHO_DATABASE_DESC" => opts[:description] || ""
        }
  if res.success?
    data = JSON.parse(res.body)
    Workspace.new(name, self)
  else
    nil
  end
end

#get(path: nil, params: {}) ⇒ Faraday::Response

Wrapper for an HTTP GET request via Faraday

Parameters:

  • path (String) (defaults to: nil)

    URL path component

  • params (Hash) (defaults to: {})

    Query parameters for the request

Returns:

  • (Faraday::Response)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zoho-sdk/analytics/client.rb', line 98

def get(path: nil, params: {})
  conn =
    Faraday.new(url: url_for(path)) do |conn|
      conn.adapter :net_http
      conn.headers = {
        "Authorization" => "Zoho-oauthtoken #{@access_token}"
      }
    end
  res =
    conn.get do |req|
      req.params["ZOHO_OUTPUT_FORMAT"] = "JSON"
      req.params["ZOHO_ERROR_FORMAT"] = "JSON"
      req.params["ZOHO_API_VERSION"] = "1.0"
      params.each { |key, value| req.params[key] = value }
    end
end

#post_json(path: nil, io:, params: {}) ⇒ Faraday::Response

Wrapper for posting JSON via Faraday. Used primarily for IMPORT tasks.

Parameters:

  • path (String) (defaults to: nil)

    URL path component

  • io (IO)

    A readable IO object

  • params (Hash) (defaults to: {})

    Query parameters for the request

Returns:

  • (Faraday::Response)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/zoho-sdk/analytics/client.rb', line 120

def post_json(path: nil, io:, params: {})
  conn =
    Faraday.new(url: url_for(path)) do |conn|
      conn.request :multipart
      conn.adapter :net_http
      conn.headers = {
        "Authorization" => "Zoho-oauthtoken #{@access_token}",
        "Content-Type" => "multipart/form-data"
      }
    end
  res =
    conn.post do |req|
      payload = {
        "ZOHO_OUTPUT_FORMAT" => "JSON",
        "ZOHO_ERROR_FORMAT" => "JSON",
        "ZOHO_API_VERSION" => "1.0"
      }
      params.merge(payload).each { |key, value| req.params[key] = value }
      req.body = {
        "ZOHO_FILE" =>
          Faraday::FilePart.new(io, "application/json", "ZOHO_FILE.json")
      }
    end
end

#url_for(path = nil) ⇒ Object

Helper function to build a complete URL path that includes email ID and encodes path elements.



147
148
149
150
151
152
153
154
# File 'lib/zoho-sdk/analytics/client.rb', line 147

def url_for(path = nil)
  parts = [API_BASE_URL, @email]
  if !path.nil?
    path = path[1..-1] if path[0] == "/"
    parts << path
  end
  URI.encode(parts.join("/"))
end

#workspace(name) ⇒ Workspace

Retrieve a workspace by name

Parameters:

  • name (String)

    The workspace name

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/zoho-sdk/analytics/client.rb', line 80

def workspace(name)
  res = get params: { "ZOHO_ACTION" => "ISDBEXIST", "ZOHO_DB_NAME" => name }
  if res.success?
    data = JSON.parse(res.body)
    if data.dig("response", "result", "isdbexist") == "true"
      Workspace.new(name, self)
    else
      nil
    end
  else
    nil
  end
end

#workspace_metadataObject



50
51
52
53
54
55
# File 'lib/zoho-sdk/analytics/client.rb', line 50

def 
  get params: {
        "ZOHO_ACTION" => "DATABASEMETADATA",
        "ZOHO_METADATA" => "ZOHO_CATALOG_LIST"
      }
end