logan 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6693a0249ee8419b0677c7eb5aeed071f775dfca
4
- data.tar.gz: 1f91aa04d498e44f07fca9f3eb12820e4c48a415
3
+ metadata.gz: dd4e95b42c7d708e8d8b11a93afe0c65aedc1e12
4
+ data.tar.gz: aa891ac5706e4d2cb58543ea4f33ee67a891512d
5
5
  SHA512:
6
- metadata.gz: a49cc959c5bd8247ee7658be881789c1b4c3fbec3ccc96c991759856c86de2e34f8e788ca03872610a20ebf3755011fb0c5bec7e4c34e1c9dd543300ff0c1fb1
7
- data.tar.gz: 0e7d6419b8c64962eb9fb06610a0546d05e52a0558958467c1b0d9e80454dc27ed1d518e0481a7324625d9227b6ae60395a42cfbb63379ecc9e2d54c0621b389
6
+ metadata.gz: 19c3d366443abf9573cfa8def7ae49dca190ef345585fedd4391061aa97a8e0e871b7121d6313d9dd396aca517837b7a828cf17038f80a7f655517ccf4eda635
7
+ data.tar.gz: 018fdb52e55f5606d2e281d9a7efa942999b02d19f0ca5e4848aa5a1d46fc8a33c51511eb8cfe4c2d48b0a5203683638a862213f7a32aa0b75a083134923e7bd
data/lib/logan/client.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'logan/project'
2
-
2
+ require 'logan/response_handler'
3
3
  module Logan
4
4
  class Client
5
5
  include HTTParty
6
-
6
+ include ResponseHandler
7
+
7
8
  # Initializes the Logan shared client with information required to communicate with Basecamp
8
9
  #
9
10
  # @param basecamp_id [String] the Basecamp company ID
@@ -14,68 +15,67 @@ module Logan
14
15
  self.class.headers 'User-Agent' => user_agent
15
16
  self.auth = auth_hash
16
17
  end
17
-
18
+
18
19
  # Updates authorization information for Logan shared client
19
- #
20
+ #
20
21
  # @param auth_hash [Hash] authorization hash consisting of a username and password combination (:username, :password) or an access_token (:access_token)
21
22
  def auth=(auth_hash)
22
23
  # symbolize the keys
23
24
  new_auth_hash = {}
24
25
  auth_hash.each {|k, v| new_auth_hash[k.to_sym] = v}
25
26
  auth_hash = new_auth_hash
26
-
27
+
27
28
  if auth_hash.has_key? :access_token
28
29
  # clear the basic_auth, if it's set
29
30
  self.class.default_options.reject!{ |k| k == :basic_auth }
30
-
31
+
31
32
  # set the Authorization headers
32
33
  self.class.headers.merge!("Authorization" => "Bearer #{auth_hash[:access_token]}")
33
34
  elsif auth_hash.has_key?(:username) && auth_hash.has_key?(:password)
34
35
  self.class.basic_auth auth_hash[:username], auth_hash[:password]
35
-
36
+
36
37
  # remove the access_token from the headers, if it exists
37
38
  self.class.headers.reject!{ |k, v| k == "Authorization" }
38
39
  else
39
40
  raise """
40
- Incomplete authorization information passed in authorization hash.
41
+ Incomplete authorization information passed in authorization hash.
41
42
  You must have either an :access_token or a username password combination (:username, :password).
42
43
  """
43
44
  end
44
45
  end
45
-
46
+
46
47
  # get projects from Basecamp API
47
- #
48
+ #
48
49
  # @return [Array<Logan::Project>] array of {Logan::Project} instances
49
50
  def projects
50
51
  response = self.class.get '/projects.json'
51
- response.parsed_response.map { |h| p = Logan::Project.new(h) }
52
+ handle_response(response, Proc.new {|h| Logan::Project.new(h) })
52
53
  end
53
-
54
+
54
55
  # get active Todo lists for all projects from Basecamp API
55
- #
56
+ #
56
57
  # @return [Array<Logan::TodoList>] array of {Logan::TodoList} instances
57
58
  def todolists
58
59
  response = self.class.get '/todolists.json'
59
-
60
- response.parsed_response.map do |h|
61
- list = Logan::TodoList.new(h)
62
-
63
- # grab the project ID for this list from the url
64
- list.project_id = list.url.scan( /projects\/(\d+)/).last.first
65
-
66
- # return the list so this method returns an array of lists
67
- list
68
- end
60
+ handle_response(response,
61
+ Proc.new { |h|
62
+ list = Logan::TodoList.new(h)
63
+ # grab the project ID for this list from the url
64
+ list.project_id = list.url.scan( /projects\/(\d+)/).last.first
65
+ # return the list so this method returns an array of lists
66
+ return list
67
+ }
68
+ )
69
69
  end
70
-
70
+
71
71
  def events(since_time, page = 1)
72
72
  response = self.class.get "/events.json?since=#{since_time.to_s}&page=#{page}"
73
- response.map { |h| e = Logan::Event.new(h) }
73
+ handle_response(response, Proc.new {|h| Logan::Event.new(h) })
74
74
  end
75
75
 
76
76
  def people
77
77
  response = self.class.get "/people.json"
78
- response.parsed_response.map { |h| p = Logan::Person.new(h)}
78
+ handle_response(response, Proc.new {|h| Logan::Person.new(h) })
79
79
  end
80
80
  end
81
81
  end
@@ -0,0 +1,28 @@
1
+ module Logan
2
+ class Error < StandardError
3
+ attr_reader :code
4
+ attr_reader :message
5
+
6
+ def initialize(code, message)
7
+ @code = code.to_sym
8
+ @message = message
9
+ end
10
+
11
+ def to_json
12
+ JSON.generate({error: @code.to_s, error_description: @message})
13
+ end
14
+
15
+ end
16
+
17
+ class AccessDeniedError < Error
18
+ def initialize
19
+ super :access_denied, "You are not allowed to access this resource."
20
+ end
21
+ end
22
+
23
+ class InvalidRequestError < Error
24
+ def initialize
25
+ super :invalid_request, "The request has wrong parameters."
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ module Logan
2
+ module ResponseHandler
3
+ def handle_response(response, block)
4
+ if success?(response.response)
5
+ response.parsed_response.map { |h| block.call(h) }
6
+ else
7
+ handle_error(response.response)
8
+ end
9
+ end
10
+
11
+ def handle_error(response)
12
+ if response.kind_of? Net::HTTPUnauthorized
13
+ raise Logan::AccessDeniedError
14
+ else
15
+ raise Logan::InvalidRequestError
16
+ end
17
+ end
18
+
19
+ def success?(response)
20
+ response.kind_of? Net::HTTPSuccess
21
+ end
22
+ end
23
+ end
data/lib/logan.rb CHANGED
@@ -7,4 +7,6 @@ require 'logan/event'
7
7
  require 'logan/person'
8
8
  require 'logan/project'
9
9
  require 'logan/todo'
10
- require 'logan/todolist'
10
+ require 'logan/todolist'
11
+ require 'logan/errors'
12
+ require 'logan/response_handler'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Birarda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-03 00:00:00.000000000 Z
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -76,9 +76,11 @@ files:
76
76
  - lib/logan/HashConstructed.rb
77
77
  - lib/logan/client.rb
78
78
  - lib/logan/comment.rb
79
+ - lib/logan/errors.rb
79
80
  - lib/logan/event.rb
80
81
  - lib/logan/person.rb
81
82
  - lib/logan/project.rb
83
+ - lib/logan/response_handler.rb
82
84
  - lib/logan/todo.rb
83
85
  - lib/logan/todolist.rb
84
86
  homepage: https://github.com/birarda/logan
@@ -93,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
95
  requirements:
94
96
  - - ">="
95
97
  - !ruby/object:Gem::Version
96
- version: '0'
98
+ version: 1.8.7
97
99
  required_rubygems_version: !ruby/object:Gem::Requirement
98
100
  requirements:
99
101
  - - ">="