Class: Harvest

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

Constant Summary collapse

CONFIG_PATH =

define Harvest config file path

File.join(%x[echo ~].chomp, ".harvestthingsrc")

Instance Method Summary collapse

Constructor Details

#initializeHarvest

Returns a new instance of Harvest.



24
25
26
27
28
29
30
31
# File 'lib/harvestthings/harvest.rb', line 24

def initialize
  generate_config unless File.exists?(CONFIG_PATH)
  load CONFIG_PATH
  
  @company             = HarvestConfig.attrs[:subdomain]
  @preferred_protocols = [HarvestConfig.attrs[:has_ssl], ! HarvestConfig.attrs[:has_ssl]]
  connect!
end

Instance Method Details

#auth_stringObject



83
84
85
# File 'lib/harvestthings/harvest.rb', line 83

def auth_string
  Base64.encode64("#{HarvestConfig.attrs[:email]}:#{HarvestConfig.attrs[:password]}").delete("\r\n")
end

#generate_configObject

generate a config file if one doesn’t exist



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/harvestthings/harvest.rb', line 34

def generate_config
  # define email
  puts "enter the email you use to log into Harvest:"
  email = gets
  # define password
  puts "enter the password for this Harvest account:"
  password = gets
  # define subdomain
  puts "enter the subdomain for your Harvest account:"
  subdomain = gets

str = <<EOS
class HarvestConfig
def self.attrs(overwrite = {})
{
:email      => "#{email.chomp!}", 
:password   => "#{password.chomp!}", 
:subdomain => "#{subdomain.chomp!}",
:has_ssl    => false,
:user_agent => "Ruby/HarvestThings"
}.merge(overwrite)
end
end
EOS

  File.open(CONFIG_PATH, 'w') {|f| f.write(str) }
end

#headersObject

HTTP headers you need to send with every request.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/harvestthings/harvest.rb', line 63

def headers
  {
    # Declare that you expect response in XML after a _successful_
    # response.
    "Accept"        => "application/xml",

    # Promise to send XML.
    "Content-Type"  => "application/xml; charset=utf-8",

    # All requests will be authenticated using HTTP Basic Auth, as
    # described in rfc2617. Your library probably has support for
    # basic_auth built in, I've passed the Authorization header
    # explicitly here only to show what happens at HTTP level.
    "Authorization" => "Basic #{auth_string}",

    # Tell Harvest a bit about your application.
    "User-Agent"    => HarvestConfig.attrs[:user_agent]
  }
end

#request(path, method = :get, body = "") ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/harvestthings/harvest.rb', line 87

def request path, method = :get, body = ""
  response = send_request( path, method, body)
  if response.class < Net::HTTPSuccess
    # response in the 2xx range
    on_completed_request
    return response
  elsif response.class == Net::HTTPServiceUnavailable
    # response status is 503, you have reached the API throttle
    # limit. Harvest will send the "Retry-After" header to indicate
    # the number of seconds your boot needs to be silent.
    raise "Got HTTP 503 three times in a row" if retry_counter > 3
    sleep(response['Retry-After'].to_i + 5)
    request(path, method, body)
  elsif response.class == Net::HTTPFound
    # response was a redirect, most likely due to protocol
    # mismatch. Retry again with a different protocol.
    @preferred_protocols.shift
    raise "Failed connection using http or https" if @preferred_protocols.empty?
    connect!
    request(path, method, body)
  else
    dump_headers = response.to_hash.map { |h,v| [h.upcase,v].join(': ') }.join("\n")
    raise "#{response.message} (#{response.code})\n\n#{dump_headers}\n\n#{response.body}\n"
  end
end