Class: Agx::Pictures::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/agx/pictures/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id: nil, client_secret: nil, version: nil, sync_id: nil, access_token: nil, refresh_token: nil, token_expires_at: nil, prod: true, filepath: nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/agx/pictures/client.rb', line 7

def initialize(client_id: nil, client_secret: nil, version: nil,
  sync_id: nil, access_token: nil, refresh_token: nil,
  token_expires_at: nil, prod: true, filepath: nil)
  domain = (prod ? "agxplatform.com" : "qaagxplatform.com")
  @client_id = client_id || ENV['AGX_SYNC_CLIENT_ID']
  @client_secret = client_secret || ENV['AGX_SYNC_CLIENT_SECRET']
  @site = "https://pictures.#{domain}"
  @host = host || "pictures.#{domain}"
  @authorize_url = "https://auth.#{domain}/identity/connect/Authorize"
  @token_url = "https://auth.#{domain}/identity/connect/Token"
  @version = version || "v1"
  @sync_id = sync_id
  @api_url = "#{@site}/api/#{@version}/picture/"
  @filepath = filepath
  @headers = {
    'oauth-scopes' => "Sync",
    'Host' => @host
  }
  @client = set_client
  @token = {
    access_token: access_token,
    refresh_token: refresh_token,
    expires_at: token_expires_at
  }
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def access_token
  @access_token
end

#authorize_urlObject

Returns the value of attribute authorize_url.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def authorize_url
  @authorize_url
end

#client_idObject

Returns the value of attribute client_id.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def client_secret
  @client_secret
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def host
  @host
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def refresh_token
  @refresh_token
end

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def site
  @site
end

#sync_idObject

Returns the value of attribute sync_id.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def sync_id
  @sync_id
end

#token_expires_atObject

Returns the value of attribute token_expires_at.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def token_expires_at
  @token_expires_at
end

#token_urlObject

Returns the value of attribute token_url.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def token_url
  @token_url
end

#versionObject

Returns the value of attribute version.



4
5
6
# File 'lib/agx/pictures/client.rb', line 4

def version
  @version
end

Instance Method Details

#get(id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/agx/pictures/client.rb', line 33

def get(id)
  url = "#{@api_url}#{id}"
  begin
    response = current_token.get(url, :headers => @headers)
    filename = "#{@filepath}/#{@sync_id}_#{id}.jpeg"
    File.open(filename, 'wb') { |fp| fp.write(response.body) }
  rescue => e
    handle_error(e)
  end
end

#get_metadata(id) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/agx/pictures/client.rb', line 44

def (id)
  url = "#{@api_url}#{id}/metadata"
  begin
    response = current_token.get(url, :headers => @headers)
    parse_response(response.body)
  rescue => e
    handle_error(e)
  end
end