Class: EasyPdfCloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_pdf_cloud.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easy_pdf_cloud.rb', line 27

def initialize(options)

  @options = options
  @host = options['host'] || 'https://www.easypdfcloud.com'
  @api_host = options['api_host'] || "https://api.easypdfcloud.com"
  @version = options['version'] || "v1"
  @api_url = "#{@api_host}/#{@version}"
  @workflow_url = "#{@api_url}/workflows"
  @client_id = options['client_id']
  @client_secret = options['client_secret']
  @access_token = options['access_token']
  @refresh_token = options['refresh_token']

  client_options = {
    :site => @host,
    :authorize_url    => '/oauth2/authorize',
    :token_url        => '/oauth2/token'
  }
  @client = OAuth2::Client.new(@client_id, @client_secret, client_options)
  #@client.auth_code.authorize_url(:redirect_uri => 'http://localhost', :scope => "epc.api", :state => "EasyPDFCloud")

  @access_token = OAuth2::AccessToken.from_hash(@client, {:access_token => @access_token, :refresh_token => @refresh_token})
end

Instance Attribute Details

#access_tokenObject

The OAuth access token in use by the client



13
14
15
# File 'lib/easy_pdf_cloud.rb', line 13

def access_token
  @access_token
end

#api_hostObject

The host to use for API requests. Defaults to api.easypdfcloud.com



19
20
21
# File 'lib/easy_pdf_cloud.rb', line 19

def api_host
  @api_host
end

#api_urlObject (readonly)

The base URL to the Base API https://#api_host/#version



25
26
27
# File 'lib/easy_pdf_cloud.rb', line 25

def api_url
  @api_url
end

#client_idObject

The client id (aka “Consumer Key”) to use for OAuth2 authentication



9
10
11
# File 'lib/easy_pdf_cloud.rb', line 9

def client_id
  @client_id
end

#client_secretObject

The client secret (aka “Consumer Secret” to use for OAuth2 authentication)



11
12
13
# File 'lib/easy_pdf_cloud.rb', line 11

def client_secret
  @client_secret
end

#hostObject

The host to use for OAuth2 authentication. Defaults to www.easypdfcloud.com



17
18
19
# File 'lib/easy_pdf_cloud.rb', line 17

def host
  @host
end

#refresh_tokenObject

The OAuth refresh token in use by the client



15
16
17
# File 'lib/easy_pdf_cloud.rb', line 15

def refresh_token
  @refresh_token
end

#versionObject

The API version the client is using. Defaults to v1



21
22
23
# File 'lib/easy_pdf_cloud.rb', line 21

def version
  @version
end

#workflow_urlObject (readonly)

The base URL to the workflow API



23
24
25
# File 'lib/easy_pdf_cloud.rb', line 23

def workflow_url
  @workflow_url
end

Instance Method Details

#check_access_tokenObject



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

def check_access_token
  if @access_token.expired? || @access_token.token.empty?
    puts "Refreshing EasyPdfCloud Access Token"
    # For older versions of oauth2 the refresh_token is not properly carried over after the call to refresh!
    @access_token = OAuth2::AccessToken.from_hash(@client, {:access_token => @options["access_token"], :refresh_token => @options["refresh_token"]})
    @access_token = @access_token.refresh!
    verify_access_token
  end
end

#convert(filename, input_type, output_type, workflow_id = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/easy_pdf_cloud.rb', line 70

def convert(filename, input_type, output_type, workflow_id=nil)
  check_access_token
  out_filepath = nil
  wid = workflow_id || @options["workflow_id"]
  if wid
    out_filepath = workflow(wid).convert(filename, input_type, output_type)
  else
    raise "No workflow id was specified"
  end
  return out_filepath
end

#pdf2word(filename, pdf_data, workflow_id = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/easy_pdf_cloud.rb', line 82

def pdf2word(filename, pdf_data, workflow_id=nil)
  check_access_token
  word_data = ""
  if @options.has_key?("workflow_id") || workflow_id
    id = (workflow_id ? workflow_id : @options["workflow_id"])
    word_data = workflow(id).convert_data(filename, pdf_data, 'pdf', 'docx')
  else
    raise "No workflow id was specified"
  end
  return word_data
end

#verify_access_tokenObject



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

def verify_access_token
  begin
    workflows()
  rescue => e
    puts e.message
    raise "Access denied to easypdfcloud.com API. Verify your access and/or refresh token."
  end
end

#workflow(id) ⇒ Object



107
108
109
# File 'lib/easy_pdf_cloud.rb', line 107

def workflow(id)
  Workflow.new(self, id)
end

#workflow_details(id) ⇒ Object



101
102
103
104
105
# File 'lib/easy_pdf_cloud.rb', line 101

def workflow_details(id)
  response = @access_token.get("#{@workflow_url}/#{id}")
  workflow_id = response.parsed["workflowID"].to_i
  Workflow.new(self, workflow_id)
end

#workflowsObject



94
95
96
97
98
99
# File 'lib/easy_pdf_cloud.rb', line 94

def workflows
  check_access_token
  response = @access_token.get(@workflow_url)
  hash = response.parsed
  hash["workflows"]
end