ruby_desk 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,17 +1,24 @@
1
- = ruby_desk
2
-
3
- Description goes here.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but
13
- bump version in a commit by itself I can ignore when I pull)
14
- * Send me a pull request. Bonus points for topic branches.
1
+ = RubyDesk
2
+
3
+ A gem built by BadrIT (www.badrit.com) that works as an interface for oDesk APIs. It can be used for both desktop and web applications
4
+
5
+ == Example
6
+ Initialize with your api key
7
+ rd = RubyDesk::Connector.new(api_key, api_secret)
8
+
9
+ Gets the URL that will ask the user to authenticate your application
10
+ rd.auth_url
11
+
12
+ This will get you the frob you need to access all APIs.
13
+ When you get the frob set it using
14
+ rd.frob = frob
15
+
16
+ After that you should request an api_token
17
+ rd.get_token
18
+
19
+ Now you are ready to use all the APIs you need
20
+ team_rooms = RubyDesk::TeamRoom.get_teamrooms(rd)
21
+ team_rooms.each { |team_room| puts team_room.id }
15
22
 
16
23
  == Copyright
17
24
 
data/Rakefile CHANGED
@@ -10,7 +10,8 @@ begin
10
10
  gem.email = "ahmed.eldawy@badrit.com"
11
11
  gem.homepage = "http://github.com/aseldawy/ruby_desk"
12
12
  gem.authors = ["Ahmed ElDawy"]
13
- # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
13
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
14
+ # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
16
17
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
@@ -0,0 +1,149 @@
1
+ require 'digest/md5'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ module RubyDesk
7
+ class Connector
8
+ ODESK_API_URL = "www.odesk.com/api/"
9
+ ODESK_AUTH_URL = "www.odesk.com/services/api/auth/"
10
+ DEFAULT_OPTIONS = {:secure=>true, :sign=>true, :format=>'xml'}
11
+
12
+ attr_writer :frob
13
+
14
+ def initialize(api_key=nil, api_secret=nil, frob=nil, api_token=nil)
15
+ @api_key = api_key
16
+ @api_secret = api_secret
17
+ @frob = frob
18
+ @api_token = api_token
19
+ @api_key = "991ac77f4c202873b0ab88f11762370c"
20
+ @api_secret = "c3382d5902e5a7b0"
21
+ end
22
+
23
+ # Sign the given parameters and returns the signature
24
+ def sign(params)
25
+ # sort parameters by its names (keys)
26
+ sorted_params = params.sort { |a, b| a.to_s <=> b.to_s}
27
+
28
+ # concatenate secret with names, values
29
+ concatenated = @api_secret + sorted_params.to_s
30
+
31
+ # Calculate md5 of concatenated string
32
+ md5 = Digest::MD5.hexdigest(concatenated)
33
+
34
+ # Return the calculated value as the signature
35
+ md5
36
+ end
37
+
38
+ # Returns the correct URL to go to to invoke the given api
39
+ # path: the path of the API to call. e.g. 'auth'
40
+ # params: a hash of parameters that needs to be appended
41
+ # options:
42
+ # * :secure=>false: Whether a secure connection is required or not.
43
+ # * :sign=>true: Whether you need to sign the parameters or not
44
+ def prepare_api_call(path, params = {}, options = {})
45
+ options = DEFAULT_OPTIONS.merge(options)
46
+ params[:api_sig] = sign(params) if options[:sign]
47
+ {:url=>"http#{options[:secure]? 's' : ''}://" + ODESK_API_URL + path + "." + options[:format],
48
+ :params=> params, :method=>options[:method]}
49
+ end
50
+
51
+ # invokes the given API call and returns body of the response as text
52
+ def invoke_api_call(api_call)
53
+ url = URI.parse(api_call[:url])
54
+ http = Net::HTTP.new(url.host, url.port)
55
+ http.use_ssl = true
56
+
57
+ data = api_call[:params].to_a.map{|pair| pair.join '='}.join('&')
58
+ headers = {
59
+ 'Content-Type' => 'application/x-www-form-urlencoded'
60
+ }
61
+
62
+ case api_call[:method]
63
+ when :get, 'get' then
64
+ resp, data = http.request(Net::HTTP::Get.new(url.path+"?"+data, headers))
65
+ when :post, 'post' then
66
+ resp, data = http.request(Net::HTTP::Post.new(url.path, headers), data)
67
+ when :delete, 'delete' then
68
+ resp, data = http.request(Net::HTTP::Delete.new(url.path, headers), data)
69
+ end
70
+
71
+ return data
72
+ end
73
+
74
+ # Prepares an API call with the given arguments then invokes it and returns its body
75
+ def prepare_and_invoke_api_call(path, params = {}, options = {})
76
+ api_call = prepare_api_call(path, params, options)
77
+ invoke_api_call(api_call)
78
+ end
79
+
80
+ # Returns the URL that authenticates the application for the current user
81
+ def auth_url
82
+ params = {:api_key=>@api_key}
83
+ params[:api_sig] = sign(params)
84
+ "https://"+ODESK_AUTH_URL+"?"+params.to_a.map{|pair| pair.join '='}.join('&')
85
+ end
86
+
87
+ # return the URL that logs user out of odesk applications
88
+ def logout_url
89
+ "https://"+ODESK_AUTH_URL
90
+ end
91
+
92
+ # Returns an authentication frob.
93
+ # Parameters
94
+ # * frob
95
+ # * api_key
96
+ # * api_sig
97
+ #
98
+ # Return Data
99
+ # * token
100
+ def get_token
101
+ response = prepare_and_invoke_api_call 'auth/v1/keys/tokens', {:frob=>@frob, :api_key=>@api_key}, :method=>:post, :format=>'json'
102
+ json = JSON.parse(response)
103
+ @api_token = json['token']
104
+ end
105
+
106
+ # Returns an authentication frob.
107
+ #Parameters
108
+ # * api_key
109
+ # * api_sig
110
+ #
111
+ #Return Data
112
+ # * frob
113
+
114
+ def get_frob
115
+ response = prepare_and_invoke_api_call 'auth/v1/keys/frobs', {:api_key=>@api_key}, :method=>:post, :format=>'json'
116
+ json = JSON.parse(response)
117
+ @frob = json['frob']
118
+ end
119
+
120
+ # Returns the authenticated user associated with the given authorization token.
121
+ # Parameters
122
+ #
123
+ # * api_key
124
+ # * api_sig
125
+ # * api_token
126
+ #
127
+ # Return Data
128
+ #
129
+ # * token
130
+ def check_token
131
+ prepare_and_invoke_api_call 'auth/v1/keys/token', {:api_key=>@api_key, :api_token=>@api_token}, :method=>:get, :format=>'json'
132
+ json = JSON.parse(response)
133
+ # TODO what to make with results?
134
+ return json
135
+ end
136
+
137
+ # Revokes the given aut
138
+ # Parameters
139
+ # * api_key
140
+ # * api_sig
141
+ # * api_token
142
+
143
+ def revoke_token
144
+ prepare_and_invoke_api_call 'auth/v1/keys/token', {:api_key=>@api_key, :api_token=>@api_token}, :method=>:delete, :format=>'json'
145
+ @api_token = nil
146
+ end
147
+
148
+ end
149
+ end
@@ -0,0 +1,19 @@
1
+ module RubyDesk
2
+ class Snapshot
3
+ attr_reader :status, :time, :billing_status, :report_url, :screenshot_img, :activity,
4
+ :online_presence, :user, :screenshot_url, :mouse_events_count, :company_id,
5
+ :timezone, :uid, :keyboard_events_count, :last_worked_status, :last_worked,
6
+ :memo, :active_window_title, :portrait_img, :report24_img, :computer_name,
7
+ :screenshot_img_thmb, :online_presence_img, :user_id, :role, :client_version,
8
+ :snapshot_api, :workdiary_api
9
+ def initialize(params={})
10
+ params.each do |k, v|
11
+ if k.to_s == 'user'
12
+ @user = RubyDesk::User.new(v)
13
+ else
14
+ self.instance_variable_set("@#{k}", v)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ module RubyDesk
2
+ class TeamRoom
3
+ class << self
4
+ def get_teamrooms(connector)
5
+ response = connector.prepare_and_invoke_api_call 'team/v1/teamrooms', {:api_token=>@api_token, :api_key=>@api_key}, :method=>:get, :format=>'json'
6
+ # parses a JSON result returned from oDesk and extracts an array of TeamRooms out of it
7
+ json = JSON.parse(response)
8
+ team_rooms = []
9
+ json['teamrooms']['teamroom'].each do |teamroom|
10
+ # Append this TeamRoom to array
11
+ team_rooms << self.new(teamroom)
12
+ end
13
+ # return the resulting array
14
+ team_rooms
15
+ end
16
+
17
+ end
18
+
19
+ # Attribute readers for all attributes
20
+ attr_reader :company_recno, :company_name, :name, :id, :recno, :teamroom_api
21
+
22
+ # Create a new TeamRoom from a hash similar to the one in ActiveRecord::Base.
23
+ # The given hash maps each attribute name to its value
24
+ def initialize(params={})
25
+ params.each do |k, v|
26
+ self.instance_variable_set("@#{k}", v)
27
+ end
28
+ end
29
+
30
+ def snapshot(connector, online='now')
31
+ response = connector.prepare_and_invoke_api_call "team/v1/teamrooms/#{self.id}", {:api_token=>@api_token, :api_key=>@api_key, :online=>online}, :method=>:get, :format=>'json'
32
+ json = JSON.parse(response)
33
+ RubyDesk::Snapshot.new(json['teamroom']['snapshot'])
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ module RubyDesk
2
+ class User
3
+
4
+ attr_accessor :messenger_id, :timezone, :uid, :timezone_offset, :last_name,
5
+ :mail, :creation_time, :first_name
6
+ def initialize(params)
7
+ params.each do |k, v|
8
+ self.instance_variable_set("@#{k}", v)
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/ruby_desk.rb CHANGED
@@ -1,96 +1,5 @@
1
- require 'digest/md5'
1
+ module RubyDesk
2
+ end
2
3
 
3
- class RubyDesk
4
- ODESK_API_URL = "www.odesk.com/api/"
5
- ODESK_AUTH_URL = "www.odesk.com/services/api/auth/"
6
- DEFAULT_OPTIONS = {:secure=>true, :sign=>true, :format=>'xml'}
7
-
8
- def initialize(api_key=nil, api_secret=nil)
9
- @api_key = api_key
10
- @api_secret = api_secret
11
- @api_key = "991ac77f4c202873b0ab88f11762370c"
12
- @api_secret = "c3382d5902e5a7b0"
13
- end
14
-
15
- # Sign the given parameters and updates the given
16
- def sign(params)
17
- # sort parameters by its names (keys)
18
- sorted_params = params.sort { |a, b| a.to_s <=> b.to_s}
19
-
20
- # concatenate secret with names, values
21
- concatenated = @api_secret + sorted_params.to_s
22
-
23
- # Calculate md5 of concatenated string
24
- md5 = Digest::MD5.hexdigest(concatenated)
25
-
26
- # Return the calculated value as the signature
27
- md5
28
- end
29
-
30
- # Returns the correct URL to go to to invoke the given api
31
- # path: the path of the API to call. e.g. 'auth'
32
- # params: a hash of parameters that needs to be appended
33
- # options:
34
- # * :secure=>false: Whether a secure connection is required or not.
35
- # * :sign=>true: Whether you need to sign the parameters or not
36
- def get_api_url(path, params = {}, options = {})
37
- options = DEFAULT_OPTIONS.merge(options)
38
- params[:api_sig] = sign(params) if options[:sign]
39
- {:url=>"http#{options[:secure]? 's' : ''}://" + ODESK_API_URL + path + "." + options[:format],
40
- :params=> params, :method=>options[:method]}
41
- end
42
-
43
- # Returns the URL that authenticates the application for the current user
44
- def auth_url
45
- params = {:api_key=>@api_key}
46
- params[:api_sig] = sign(params)
47
- "https://"+ODESK_AUTH_URL+"?"+params.to_a.map{|pair| pair.join '='}.join('&')
48
- end
49
-
50
- # return the URL that logs user out of odesk applications
51
- def logout_url
52
- "https://"+ODESK_AUTH_URL
53
- end
54
-
55
- # Returns an authentication frob.
56
- # Parameters
57
- # * frob
58
- # * api_key
59
- # * api_sig
60
- #
61
- # Return Data
62
- # * token
63
- def get_token_url(frob, options={})
64
- get_api_url 'auth/v1/keys/tokens', {:frob=>frob, :api_key=>@api_key}, options.merge(:method=>:post)
65
- end
66
-
67
- # Returns an authentication frob.
68
- #Parameters
69
- # * api_key
70
- # * api_sig
71
- #
72
- #Return Data
73
- # * frob
74
-
75
- def get_frob_url(options={})
76
- get_api_url 'auth/v1/keys/frobs', {:api_key=>@api_key}, options.merge(:method=>:post)
77
- end
78
-
79
- #Returns the authenticated user associated with the given authorization token.
80
- # Parameters
81
- #
82
- # * api_key
83
- # * api_sig
84
- # * api_token
85
- #
86
- # Return Data
87
- #
88
- # * token
89
- def check_token_url(api_token, options={})
90
- get_api_url 'auth/v1/keys/token', {:api_key=>@api_key, :api_token=>@api_token}, options.merge(:method=>:get)
91
- end
92
-
93
- def revoke_token_url(api_token, options={})
94
- get_api_url 'auth/v1/keys/token', {:api_key=>@api_key, :api_token=>@api_token}, options.merge(:method=>:delete)
95
- end
96
- end
4
+ require 'ruby_desk/connector'
5
+ require 'ruby_desk/team_room'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_desk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmed ElDawy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 +02:00
12
+ date: 2010-01-13 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,13 +23,15 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.rdoc
25
25
  files:
26
- - .document
27
- - .gitignore
28
26
  - LICENSE
29
27
  - README.rdoc
30
28
  - Rakefile
31
29
  - VERSION
32
30
  - lib/ruby_desk.rb
31
+ - lib/ruby_desk/connector.rb
32
+ - lib/ruby_desk/snapshot.rb
33
+ - lib/ruby_desk/team_room.rb
34
+ - lib/ruby_desk/user.rb
33
35
  - test/helper.rb
34
36
  - test/test_ruby_desk.rb
35
37
  has_rdoc: true
@@ -63,4 +65,3 @@ summary: Wrapper for oDesk APIs in Ruby
63
65
  test_files:
64
66
  - test/test_ruby_desk.rb
65
67
  - test/helper.rb
66
- - test/test.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
data/test/test.rb DELETED
@@ -1,46 +0,0 @@
1
- require 'net/http'
2
- require 'net/https'
3
- require 'uri'
4
- require 'open-uri'
5
- require 'ruby_desk'
6
- require 'rubygems'
7
- require 'json'
8
-
9
- rd = RubyDesk.new
10
-
11
- get_frob = rd.get_frob_url(:format=>'json')
12
-
13
- url = URI.parse(get_frob[:url])
14
-
15
- http = Net::HTTP.new(url.host, url.port)
16
- http.use_ssl = true
17
- path = url.path
18
-
19
- data = get_frob[:params].to_a.map{|pair| pair.join '='}.join('&')
20
- headers = {
21
- 'Content-Type' => 'application/x-www-form-urlencoded'
22
- }
23
-
24
- resp, data = http.post(path, data, headers)
25
-
26
- json = JSON.parse(data)
27
- frob = json['frob']
28
-
29
- get_token = rd.get_token_url(frob, :format=>'json')
30
-
31
- url = URI.parse(get_token[:url])
32
-
33
- path = url.path
34
-
35
- data = get_frob[:params].to_a.map{|pair| pair.join '='}.join('&')
36
- headers = {
37
- 'Content-Type' => 'application/x-www-form-urlencoded'
38
- }
39
-
40
- resp, data = http.post(path, data, headers)
41
-
42
- json = JSON.parse(data)
43
-
44
- puts json.inspect
45
-
46
- puts open('https://www.odesk.com/api/team/v1/teamrooms')