Class: ActiveInTime::Base

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

Constant Summary collapse

API =
"https://api.activeintime.com/v1/"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

API = “api.lvh.me:3000/v1/”



6
7
8
9
10
11
12
13
14
15
# File 'lib/ait_connect/base.rb', line 6

def initialize(*args)
  case args.size
  when 1
    @access_token = args.first
  when 2
    @key, @secret = args
  else
    raise ArgumentError, "You need to pass either an access_token or key and secret"
  end
end

Instance Method Details

#access_token(code, redirect_uri) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ait_connect/base.rb', line 78

def access_token(code, redirect_uri)
  # http://developer.foursquare.com/docs/oauth.html
  
  # check params
  raise "you need to define a client id before" if @key.blank?
  raise "you need to define a client secret before" if @secret.blank?
  raise "no code provided" if code.blank?
  raise "no redirect_uri provided" if redirect_uri.blank?
  
  # params
  params = {}
  params["key"] = @key
  params["secret"] = @secret
  params["grant_type"] = "authorization_code"
  params["redirect_uri"] = redirect_uri
  params["code"] = code
  
  # url
  url = oauth2_url('access_token', params)
  
  # response
  # http://developer.foursquare.com/docs/oauth.html
  response = JSON.parse(Typhoeus::Request.get(url).body)
  response["access_token"]
end

#authorize_url(redirect_uri) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ait_connect/base.rb', line 61

def authorize_url(redirect_uri)
  # http://developer.foursquare.com/docs/oauth.html
  
  # check params
  raise "you need to define a client id before" if @key.blank?
  raise "no callback url provided" if redirect_uri.blank?
  
  # params
  params = {}
  params["key"] = @key
  params["response_type"] = "code"
  params["redirect_uri"] = redirect_uri
  
  # url
  oauth2_url('authenticate', params)
end

#get(path, params = {}) ⇒ Object

def settings

@settings ||= ActiveInTime::Settings.new(self)

end



41
42
43
44
45
46
47
48
49
# File 'lib/ait_connect/base.rb', line 41

def get(path, params={})
  params = camelize(params)
  ActiveInTime.log("GET #{API + path}")
  ActiveInTime.log("PARAMS: #{params.inspect}")
  merge_auth_params(params)
  response = JSON.parse(Typhoeus::Request.get(API + path + '.json', :params => params).body)
  ActiveInTime.log(response.inspect)
  error(response) || response["response"]
end

#post(path, params = {}) ⇒ Object



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

def post(path, params={})
  params = camelize(params)
  ActiveInTime.log("POST #{API + path}")
  ActiveInTime.log("PARAMS: #{params.inspect}")
  merge_auth_params(params)
  response = JSON.parse(Typhoeus::Request.post(API + path, :params => params).body)
  ActiveInTime.log(response.inspect)
  error(response) || response["response"]
end

#sitesObject

def checkins

ActiveInTime::CheckinProxy.new(self)

end



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

def sites
  ActiveInTime::SiteProxy.new(self)
end

#timetable_entriesObject



33
34
35
# File 'lib/ait_connect/base.rb', line 33

def timetable_entries
  ActiveInTime::TimetableEntryProxy.new(self)
end

#timetablesObject



29
30
31
# File 'lib/ait_connect/base.rb', line 29

def timetables
  ActiveInTime::TimetableProxy.new(self)
end