Class: Asana2Flowdock::Asana::Api
- Inherits:
-
Object
- Object
- Asana2Flowdock::Asana::Api
- Defined in:
- lib/asana2flowdock/asana.rb
Overview
Constant Summary collapse
- Uri =
URI.parse('https://app.asana.com/api/1.0')
Instance Method Summary collapse
- #get(url, *args) ⇒ Object
-
#initialize(token = ENV['ASANA_TOKEN']) ⇒ Api
constructor
A new instance of Api.
- #log(level, *args, &block) ⇒ Object
- #me ⇒ Object (also: #my)
- #model_for(model, *args, &block) ⇒ Object
- #result_for(data, options = {}) ⇒ Object
- #url_for(*paths) ⇒ Object
- #users(options = {}) ⇒ Object
Constructor Details
#initialize(token = ENV['ASANA_TOKEN']) ⇒ Api
Returns a new instance of Api.
16 17 18 |
# File 'lib/asana2flowdock/asana.rb', line 16 def initialize(token = ENV['ASANA_TOKEN']) @token = token end |
Instance Method Details
#get(url, *args) ⇒ Object
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/asana2flowdock/asana.rb', line 36 def get(url, *args) # url = url.to_s = Map.(args) model = args.detect{|arg| arg <= Model} || .delete(:model) query = [:query] || # unless url['://'] url = url_for(url) end # uri = URI.parse(url) unless query.blank? uri.query = query.map{|k,v| [CGI.escape(k.to_s), CGI.escape(v.to_s)].join('=')}.join('&') end url = uri.to_s # result = nil error = nil cmd = nil # 3.times do begin cmd = "curl -s -u #{ @token }: #{ url.inspect } 2>/dev/null" log(:debug, cmd) if debug result = `#{ cmd }` break result rescue => e error = e sleep(3) nil end end # raise "#{ cmd } blargh'd!?" if error || result.nil? # result = JSON.parse(result) if result['errors'] raise result['errors'].inspect end data = result['data'] # result_for(data, :model => model) end |
#log(level, *args, &block) ⇒ Object
20 21 22 23 24 |
# File 'lib/asana2flowdock/asana.rb', line 20 def log(level, *args, &block) if logger? logger.send(level, *args, &block) end end |
#me ⇒ Object Also known as: my
26 27 28 |
# File 'lib/asana2flowdock/asana.rb', line 26 def me get("/users/me", :model => User) end |
#model_for(model, *args, &block) ⇒ Object
122 123 124 125 126 |
# File 'lib/asana2flowdock/asana.rb', line 122 def model_for(model, *args, &block) model = model.for(*args, &block) ensure model.api = self if model end |
#result_for(data, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/asana2flowdock/asana.rb', line 102 def result_for(data, = {}) case data when Array data.map do |item| result_for(item, ) end when Hash attributes = Map.for(data) model = [:model] if model model_for(model, attributes) else attributes end else raise ArgumentError, data.class.name end end |
#url_for(*paths) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/asana2flowdock/asana.rb', line 95 def url_for(*paths) uri = Uri.dup path = uri.path uri.path = File.join(path, *paths.flatten.compact.map{|path| path.to_s}) uri.to_s end |
#users(options = {}) ⇒ Object
30 31 32 |
# File 'lib/asana2flowdock/asana.rb', line 30 def users( = {}) get("/users", User, ) end |