Class: ShopkeepReports::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shopkeep_reports/client.rb

Constant Summary collapse

@@agent =
false
false
@@token =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



12
13
14
# File 'lib/shopkeep_reports/client.rb', line 12

def agent
  @agent
end

Returns the value of attribute cookie_jar.



12
13
14
# File 'lib/shopkeep_reports/client.rb', line 12

def cookie_jar
  @cookie_jar
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/shopkeep_reports/client.rb', line 12

def token
  @token
end

Class Method Details

.instanceObject



14
15
16
# File 'lib/shopkeep_reports/client.rb', line 14

def self.instance
  @@instance ||= new
end

Instance Method Details

#authorize(params = {}) ⇒ Object



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

def authorize(params = {})
  return @@agent if @@agent
  @username = configuration.email
  @password = configuration.password
  
end

#download_report(report_link, type = 'get', start_date = nil, end_date = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shopkeep_reports/client.rb', line 25

def download_report(report_link, type = 'get', start_date = nil, end_date = nil)
  new_agent = Mechanize.new
  new_agent.cookie_jar = @@cookie_jar
  case type
  when 'get'
    new_agent.get(report_link)
    if !start_date.blank? and !end_date.blank?
      new_agent.page.forms.first.set_fields({
        'start_date' => start_date.to_s,
        'end_date' => end_date.to_s
      })
    end
    new_agent.page.forms.first.submit
  when 'post'
    new_agent.post(report_link, { 'authenticity_token' => @@token })
  end
  download_link = new_agent.page.link_with(:text => 'Download')
  if download_link.blank?
    file_uri = new_agent.page.forms.first.submit.uri
  else
    file_uri = download_link.uri
  end
  file_name = File.join(configuration.tmp_directory, (file_uri.to_s.split('.csv').first.split('/').last + '.csv'))
  File.open(file_name, "wb") do |file|
    file_uri.open do |uri|
      file.write(uri.read)
    end
  end
  file_name
rescue Exception => e
  reset
  raise ShopkeepException, "#{e.message}"
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/shopkeep_reports/client.rb', line 80

def download_report_link(report_link)
  new_agent = Mechanize.new
  new_agent.cookie_jar = @@cookie_jar
  download = new_agent.get(report_link)
  file_body = download.body
  file_name = File.join(configuration.tmp_directory, download.filename)
  File.open(file_name, "wb") do |file|
    file.write(file_body)
  end
  file_name
rescue Exception => e
  reset
  raise ShopkeepException, "#{e.message}"
end


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shopkeep_reports/client.rb', line 59

def download_transaction_report_link(report_link, start, finish)
  new_agent = Mechanize.new
  new_agent.cookie_jar = @@cookie_jar
  new_agent.post(report_link, { 'authenticity_token' => @@token, 'start' => start, 'finish' => finish })
  sleep(5)
  export_list = new_agent.get(configuration.uri("/export_center.json"))
  export_json = export_list.body
  export_hash = JSON.parse(export_json)
  report_to_download = export_hash["aaData"].first["url"]
  download = new_agent.get(configuration.uri(report_to_download))
  file_body = download.body
  file_name = File.join(configuration.tmp_directory, download.filename)
  File.open(file_name, "wb") do |file|
    file.write(file_body)
  end
  file_name
rescue Exception => e
  reset
  raise ShopkeepException, "#{e.message}"
end