Class: Kashflow::Api

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, page) ⇒ Api

Returns a new instance of Api.



44
45
46
47
# File 'lib/kashflow/api.rb', line 44

def initialize(name, page)
  @name = name
  @page = page
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/kashflow/api.rb', line 43

def name
  @name
end

#pageObject (readonly)

Returns the value of attribute page.



43
44
45
# File 'lib/kashflow/api.rb', line 43

def page
  @page
end

Class Method Details

.api_page(path) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/kashflow/api.rb', line 13

def api_page(path)
  cache_path = File.join(Kashflow.root, 'tmp', 'kf_api_pages', path)
  
  unless File.exists?(cache_path)
    File.open(cache_path, 'w'){ |f| f << open("http://accountingapi.com/#{path}").read }
  end

  Nokogiri::HTML(open(cache_path))
end

.apis_from_webObject



23
24
25
26
27
28
29
30
31
# File 'lib/kashflow/api.rb', line 23

def apis_from_web
  main_page = api_page('manual_class_customer.asp')
  
  main_page.search('#mr_manual_inner a').select do |node|
    node['href'] =~ /manual_methods/ and
    node['href'] != 'manual_methods_overview.asp'

  end.map{|n| new(n.content, n['href']) }
end

.export_yamlObject



33
34
35
36
37
38
39
# File 'lib/kashflow/api.rb', line 33

def export_yaml
  api_methods = apis_from_web.map { |a| ApiMethod.new(a.name, a.import) }
  
  File.open(Client.yaml_path, 'w') do |f|
    YAML.dump(api_methods, f)
  end
end

Instance Method Details

#api_pageObject



49
50
51
# File 'lib/kashflow/api.rb', line 49

def api_page
  @api_page ||= self.class.api_page(page)
end

#importObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/kashflow/api.rb', line 53

def import
  @fields ||= api_page.search('.mnl_tbl1 tr').map do |row|
    {
      :name       => row.search('span.mnl_varName').first.try(:content),
      :type       => row.search('span.mnl_varType').first.try(:content),
      :direction  => row.search('td strong').first.try(:content),
      :desc       => row.search('td').last.try(:content)
    }
  end
end