redbooth-ruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +237 -20
  3. data/lib/redbooth-ruby.rb +10 -0
  4. data/lib/redbooth-ruby/base.rb +1 -1
  5. data/lib/redbooth-ruby/client.rb +9 -2
  6. data/lib/redbooth-ruby/client_operations/metadata.rb +32 -0
  7. data/lib/redbooth-ruby/client_operations/search.rb +32 -0
  8. data/lib/redbooth-ruby/comment.rb +34 -0
  9. data/lib/redbooth-ruby/file.rb +68 -0
  10. data/lib/redbooth-ruby/note.rb +25 -0
  11. data/lib/redbooth-ruby/operations/create.rb +1 -1
  12. data/lib/redbooth-ruby/operations/index.rb +0 -12
  13. data/lib/redbooth-ruby/operations/meta.rb +33 -2
  14. data/lib/redbooth-ruby/operations/show.rb +1 -1
  15. data/lib/redbooth-ruby/request/collection.rb +37 -5
  16. data/lib/redbooth-ruby/request/connection.rb +14 -8
  17. data/lib/redbooth-ruby/request/validator.rb +32 -4
  18. data/lib/redbooth-ruby/subtask.rb +18 -0
  19. data/lib/redbooth-ruby/task.rb +1 -1
  20. data/lib/redbooth-ruby/version.rb +1 -1
  21. data/redbooth-ruby.gemspec +1 -0
  22. data/spec/cassettes/RedboothRuby_ClientOperations_Metadata/_metadata/.yml +55 -0
  23. data/spec/cassettes/RedboothRuby_ClientOperations_Metadata/_metadata/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_notes_collection.yml +55 -0
  24. data/spec/cassettes/RedboothRuby_ClientOperations_Search/_index/.yml +271 -0
  25. data/spec/cassettes/RedboothRuby_ClientOperations_Search/_index/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_notes_collection.yml +271 -0
  26. data/spec/cassettes/RedboothRuby_Comment/_create/.yml +47 -0
  27. data/spec/cassettes/RedboothRuby_Comment/_create/makes_a_new_POST_request_using_the_correct_API_endpoint_to_create_a_specific_comment.yml +47 -0
  28. data/spec/cassettes/RedboothRuby_Comment/_delete/makes_a_new_DELETE_request_using_the_correct_API_endpoint_to_delete_a_specific_comment.yml +84 -0
  29. data/spec/cassettes/RedboothRuby_Comment/_index/.yml +94 -0
  30. data/spec/cassettes/RedboothRuby_Comment/_index/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_comments_collection.yml +94 -0
  31. data/spec/cassettes/RedboothRuby_Comment/_initialize/.yml +65 -0
  32. data/spec/cassettes/RedboothRuby_Comment/_show/.yml +65 -0
  33. data/spec/cassettes/RedboothRuby_Comment/_show/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_a_specific_comment.yml +65 -0
  34. data/spec/cassettes/RedboothRuby_Comment/_update/.yml +47 -0
  35. data/spec/cassettes/RedboothRuby_Comment/_update/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_comment.yml +47 -0
  36. data/spec/cassettes/RedboothRuby_File/_create/.yml +65 -0
  37. data/spec/cassettes/RedboothRuby_File/_create/makes_a_new_POST_request_using_the_correct_API_endpoint_to_create_a_specific_file.yml +65 -0
  38. data/spec/cassettes/RedboothRuby_File/_delete/makes_a_new_DELETE_request_using_the_correct_API_endpoint_to_delete_a_specific_comment.yml +102 -0
  39. data/spec/cassettes/RedboothRuby_File/_index/.yml +58 -0
  40. data/spec/cassettes/RedboothRuby_File/_index/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_comments_collection.yml +58 -0
  41. data/spec/cassettes/RedboothRuby_File/_initialize/.yml +44 -0
  42. data/spec/cassettes/RedboothRuby_File/_show/.yml +44 -0
  43. data/spec/cassettes/RedboothRuby_File/_show/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_a_specific_file.yml +44 -0
  44. data/spec/cassettes/RedboothRuby_File/_update/.yml +46 -0
  45. data/spec/cassettes/RedboothRuby_File/_update/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_file.yml +46 -0
  46. data/spec/cassettes/RedboothRuby_Note/_create/.yml +47 -0
  47. data/spec/cassettes/RedboothRuby_Note/_create/makes_a_new_POST_request_using_the_correct_API_endpoint_to_create_a_specific_note.yml +47 -0
  48. data/spec/cassettes/RedboothRuby_Note/_delete/makes_a_new_DELETE_request_using_the_correct_API_endpoint_to_delete_a_specific_note.yml +84 -0
  49. data/spec/cassettes/RedboothRuby_Note/_index/.yml +193 -0
  50. data/spec/cassettes/RedboothRuby_Note/_index/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_notes_collection.yml +193 -0
  51. data/spec/cassettes/RedboothRuby_Note/_initialize/.yml +109 -0
  52. data/spec/cassettes/RedboothRuby_Note/_show/.yml +109 -0
  53. data/spec/cassettes/RedboothRuby_Note/_show/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_a_specific_note.yml +109 -0
  54. data/spec/cassettes/RedboothRuby_Note/_update/.yml +73 -0
  55. data/spec/cassettes/RedboothRuby_Note/_update/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_note.yml +73 -0
  56. data/spec/cassettes/RedboothRuby_Subtask/_create/.yml +47 -0
  57. data/spec/cassettes/RedboothRuby_Subtask/_create/makes_a_new_POST_request_using_the_correct_API_endpoint_to_create_a_specific_subtask.yml +47 -0
  58. data/spec/cassettes/RedboothRuby_Subtask/_delete/makes_a_new_DELETE_request_using_the_correct_API_endpoint_to_delete_a_specific_subtask.yml +84 -0
  59. data/spec/cassettes/RedboothRuby_Subtask/_index/.yml +99 -0
  60. data/spec/cassettes/RedboothRuby_Subtask/_index/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_subtasks_collection.yml +99 -0
  61. data/spec/cassettes/RedboothRuby_Subtask/_initialize/.yml +45 -0
  62. data/spec/cassettes/RedboothRuby_Subtask/_show/.yml +45 -0
  63. data/spec/cassettes/RedboothRuby_Subtask/_show/makes_a_new_GET_request_using_the_correct_API_endpoint_to_receive_a_specific_subtask.yml +45 -0
  64. data/spec/cassettes/RedboothRuby_Subtask/_update/.yml +47 -0
  65. data/spec/cassettes/RedboothRuby_Subtask/_update/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_subtask.yml +47 -0
  66. data/spec/cassettes/RedboothRuby_Task/_medatada/.yml +88 -0
  67. data/spec/cassettes/RedboothRuby_Task/_medatada/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_task.yml +88 -0
  68. data/spec/cassettes/RedboothRuby_Task/_medatada_/.yml +90 -0
  69. data/spec/cassettes/RedboothRuby_Task/_medatada_/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_task.yml +90 -0
  70. data/spec/cassettes/RedboothRuby_Task/_metadata_merge/.yml +133 -0
  71. data/spec/cassettes/RedboothRuby_Task/_metadata_merge/makes_a_new_PUT_request_using_the_correct_API_endpoint_to_receive_a_specific_task.yml +133 -0
  72. data/spec/redbooth-ruby/client_operations/metadata_spec.rb +24 -0
  73. data/spec/redbooth-ruby/client_operations/search_spec.rb +24 -0
  74. data/spec/redbooth-ruby/comment_spec.rb +87 -0
  75. data/spec/redbooth-ruby/file_spec.rb +89 -0
  76. data/spec/redbooth-ruby/note_spec.rb +84 -0
  77. data/spec/redbooth-ruby/subtaks_spec.rb +83 -0
  78. data/spec/redbooth-ruby/task_spec.rb +41 -0
  79. data/spec/spec_helper.rb +2 -0
  80. metadata +134 -2
@@ -0,0 +1,32 @@
1
+ module RedboothRuby
2
+ module ClientOperations
3
+ module Search
4
+
5
+ # Retrieves all available objects from the Redbooth API by searching for the
6
+ # given parameters and query
7
+ #
8
+ # @param [Hash] options Options to pass to the API
9
+ # @return [Array] The available objects
10
+ def search(attributes = {})
11
+ response = RedboothRuby.request(:get, nil, 'search' , attributes, { session: session })
12
+ collection_from attributes, response, session
13
+ end
14
+
15
+ private
16
+
17
+ # Creates a collection object from the request and response params
18
+ #
19
+ # @param params [Hash] given request params
20
+ # @param response [RedboothRuby::Request::Response] response object
21
+ # @param session [RedboothRuby::Session] session Object
22
+ # @return [RedboothRuby::Request::Collection]
23
+ def collection_from(params, response, session)
24
+ RedboothRuby::Request::Collection.new(response: response,
25
+ resource: self,
26
+ session: session,
27
+ params: params,
28
+ method: :search)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ module RedboothRuby
2
+ class Comment < Base
3
+ include RedboothRuby::Operations::Index
4
+ include RedboothRuby::Operations::Create
5
+ include RedboothRuby::Operations::Update
6
+ include RedboothRuby::Operations::Show
7
+ include RedboothRuby::Operations::Delete
8
+
9
+ attr_accessor :id,
10
+ :body,
11
+ :body_html,
12
+ :project_id,
13
+ :user_id,
14
+ :target_id,
15
+ :target_type,
16
+ :minutes,
17
+ :upload_ids,
18
+ :assigned_id,
19
+ :previous_assigned_id,
20
+ :due_on,
21
+ :previous_due_on,
22
+ :is_private,
23
+ :previous_is_private,
24
+ :urgent,
25
+ :previous_urgent,
26
+ :email_id,
27
+ :time_tracking_on,
28
+ :status,
29
+ :previous_status,
30
+ :created_at,
31
+ :updated_at
32
+
33
+ end
34
+ end
@@ -0,0 +1,68 @@
1
+ module RedboothRuby
2
+ class File < Base
3
+ include RedboothRuby::Operations::Index
4
+ include RedboothRuby::Operations::Create
5
+ include RedboothRuby::Operations::Update
6
+ include RedboothRuby::Operations::Show
7
+ include RedboothRuby::Operations::Delete
8
+
9
+ attr_accessor :id,
10
+ :name,
11
+ :backend,
12
+ :project_id,
13
+ :parent_id,
14
+ :backend_id,
15
+ :is_dir,
16
+ :is_downloadable,
17
+ :is_previewable,
18
+ :is_private,
19
+ :mime_type,
20
+ :public_token,
21
+ :pinned,
22
+ :size,
23
+ :user_id,
24
+ :created_at,
25
+ :updated_at
26
+
27
+ class << self
28
+ # Create operation overwrite to parse file first
29
+ def create(attrs)
30
+ super(parse_file(attrs))
31
+ end
32
+
33
+ protected
34
+
35
+ # Parses the uploaded file to make the correct api request
36
+ #
37
+ # @param attributes [Hash] attributes to parse
38
+ # @return [Hash] parsed attributes hash
39
+ def parse_file(attrs)
40
+ return attrs unless attrs[:asset]
41
+ attrs[:asset_attrs] = {}
42
+ if attrs[:asset].kind_of?(::File) or attrs[:asset].kind_of?(::Tempfile) then
43
+ attrs[:asset_attrs][:name] = attrs[:asset].respond_to?(:original_filename) ? attrs[:asset].original_filename : ::File.basename(attrs[:asset].path)
44
+ attrs[:asset_attrs][:local_path] = attrs[:asset].path
45
+ elsif attrs[:asset].kind_of?(String) then
46
+ attrs[:asset_attrs][:local_path] = attrs[:asset]
47
+ attrs[:asset] = ::File.new(attrs[:file])
48
+ attrs[:asset_attrs][:name] = ::File.basename(attrs[:asset_attrs][:local_path])
49
+ elsif attrs[:asset].kind_of?(StringIO) then
50
+ raise(ArgumentError, "Must specify the :as option when uploading from StringIO") unless attrs[:as]
51
+ attrs[:asset_attrs][:local_path] = attrs[:as]
52
+
53
+ # hack for bug in UploadIO
54
+ class << file
55
+ attr_accessor :path
56
+ end
57
+ file.path = attrs[:asset]
58
+ else
59
+ raise ArgumentError, "local_file must be a File, StringIO, or file path"
60
+ end
61
+
62
+ attrs[:asset_attrs][:name] = ::File.basename(attrs.delete(:as)) if attrs[:as]
63
+
64
+ attrs
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ module RedboothRuby
2
+ class Note < Base
3
+ include RedboothRuby::Operations::Index
4
+ include RedboothRuby::Operations::Create
5
+ include RedboothRuby::Operations::Update
6
+ include RedboothRuby::Operations::Show
7
+ include RedboothRuby::Operations::Delete
8
+
9
+ attr_accessor :id,
10
+ :name,
11
+ :project_id,
12
+ :content,
13
+ :user_id,
14
+ :position,
15
+ :permalink,
16
+ :is_private,
17
+ :shared,
18
+ :token,
19
+ :updated_by_id,
20
+ :deleted,
21
+ :created_at,
22
+ :updated_at
23
+
24
+ end
25
+ end
@@ -13,7 +13,7 @@ module RedboothRuby
13
13
  attributes,
14
14
  options_for_request(session: session)
15
15
  )
16
- new(response.data)
16
+ new(response.data.merge(session: session))
17
17
  end
18
18
  end
19
19
 
@@ -27,18 +27,6 @@ module RedboothRuby
27
27
  params: params,
28
28
  method: :index)
29
29
  end
30
-
31
- # Returns the collection object build from the received response
32
- #
33
- # @param response [Array || Hash] parsed json response
34
- # @return [RedboothRuby::Collection]
35
- def results_from(response)
36
- results = []
37
- response.data.each do |obj|
38
- results << self.new(obj)
39
- end
40
- results
41
- end
42
30
  end
43
31
 
44
32
  def self.included(base)
@@ -6,10 +6,41 @@ module RedboothRuby
6
6
  # @return [Hash] the object metadata
7
7
  def metadata
8
8
  response = RedboothRuby.request(:get, nil, 'metadata',
9
- { target_id: id, target_type: self.class.to_s },
10
- options_for_request({}))
9
+ { target_id: id, target_type: klass_name },
10
+ { session: session })
11
11
  response.data
12
12
  end
13
+
14
+ # Sets the given hash as the desired metadata
15
+ #
16
+ # @return [Hash] the object metadata
17
+ def metadata=(hash)
18
+ response = RedboothRuby.request(:post, nil, 'metadata',
19
+ { target_id: id, target_type: klass_name, metadata: hash },
20
+ { session: session })
21
+
22
+ response.data
23
+ end
24
+
25
+ # Merges the given hash with the exiting metadata and set the result
26
+ #
27
+ # @return [Hash] the object metadata
28
+ def metadata_merge(hash)
29
+ response = RedboothRuby.request(:put, nil, 'metadata',
30
+ { target_id: id, target_type: klass_name, metadata: hash },
31
+ { session: session })
32
+
33
+ response.data
34
+ end
35
+
36
+ protected
37
+
38
+ # Return redbooth class name
39
+ #
40
+ # @return [String]
41
+ def klass_name
42
+ self.class.to_s.split('::').last
43
+ end
13
44
  end
14
45
  end
15
46
  end
@@ -13,7 +13,7 @@ module RedboothRuby
13
13
  {},
14
14
  options_for_request(attributes)
15
15
  )
16
- new(response.data)
16
+ new(response.data.merge(session: attributes[:session]))
17
17
  end
18
18
  end
19
19
 
@@ -15,11 +15,7 @@ module RedboothRuby
15
15
  #
16
16
  # @return [Array(resource)]
17
17
  def all
18
- results = []
19
- response.data.each do |obj|
20
- results << resource.new(obj)
21
- end
22
- results
18
+ results_from(response)
23
19
  end
24
20
 
25
21
  # Returns total pages
@@ -75,6 +71,42 @@ module RedboothRuby
75
71
 
76
72
  protected
77
73
 
74
+ # Returns the collection object build from the received response
75
+ #
76
+ # @param response [Array || Hash] parsed json response
77
+ # @return [RedboothRuby::Collection]
78
+ def results_from(response)
79
+ response.data.collect do |obj|
80
+ case resource
81
+ when RedboothRuby::Client
82
+ next unless resource_form_hash(obj.merge(session: session))
83
+ resource_form_hash(obj.merge(session: session))
84
+ else
85
+ resource.new(obj.merge(session: session))
86
+ end
87
+ end.compact
88
+ end
89
+
90
+ # Builds a resource ruby object based on the given hash
91
+ # it need to contain a 'type' key defining the object type
92
+ #
93
+ # @return [Redbooth::Base]
94
+ def resource_form_hash(hash)
95
+ return unless hash['type']
96
+ klass_name = hash['type']
97
+ klass = resource_klass(klass_name)
98
+ return unless klass
99
+ klass.new(hash)
100
+ end
101
+
102
+ # Gest the api resource model class by his name
103
+ #
104
+ # @param [String||Symbol] name name of the resource
105
+ # @return [Copy::Base] resource to use the api
106
+ def resource_klass(name)
107
+ eval('RedboothRuby::' + name.to_s.capitalize) rescue nil
108
+ end
109
+
78
110
  # Whenever the response is paginated or not
79
111
  def paginated?
80
112
  return false unless current_page
@@ -18,16 +18,22 @@ module RedboothRuby
18
18
  ::File.open(body_file_attrs[:local_path]) do |file|
19
19
  req = Net::HTTP::Post::Multipart.new(
20
20
  api_url,
21
- 'file' => UploadIO.new(file,
22
- 'application/octet-stream',
23
- body_file_attrs[:name]
24
- )
21
+ body_hash.merge(
22
+ 'asset' => UploadIO.new(file,
23
+ 'application/octet-stream',
24
+ body_file_attrs[:name]
25
+ )
26
+ )
25
27
  )
26
- access_token.sign! req
28
+ req['Authorization'] = "Bearer #{access_token.token}"
29
+ # access_token.sign! req
27
30
  if RedboothRuby.configuration[:use_ssl]
28
- http = Net::HTTP.new('redbooth.com' , Net::HTTP.https_default_port)
31
+ http = Net::HTTP.new(RedboothRuby.configuration[:api_base] , Net::HTTP.https_default_port)
29
32
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
30
33
  http.use_ssl = RedboothRuby.configuration[:use_ssl]
34
+ else
35
+ domain, port = RedboothRuby.configuration[:api_base].split(':')
36
+ http = Net::HTTP.new(domain, port || Net::HTTP.http_default_port)
31
37
  end
32
38
  http.start do |inner_http|
33
39
  inner_http.request(req)
@@ -66,7 +72,7 @@ module RedboothRuby
66
72
  end
67
73
 
68
74
  def body_file_attrs
69
- body_hash[:file_attrs] || {}
75
+ body_hash[:asset_attrs] || {}
70
76
  end
71
77
 
72
78
  # Body params url encoded
@@ -79,7 +85,7 @@ module RedboothRuby
79
85
 
80
86
  def use_body_file?
81
87
  return false if use_url_params?
82
- body_hash.key?(:file)
88
+ body_hash.key?(:asset)
83
89
  end
84
90
 
85
91
  def use_url_params?
@@ -15,9 +15,7 @@ module RedboothRuby
15
15
  # return [RedboothRuby::Request::Response]
16
16
  def validated_response_for(incoming_response)
17
17
  self.raw_response = incoming_response
18
- @response = RedboothRuby::Request::Response.new(headers: raw_response.headers,
19
- body: raw_response.body,
20
- status: raw_response.status.to_i)
18
+ @response = response_from_raw
21
19
  verify_response_code
22
20
  info.data = response.data
23
21
  validate_response_data
@@ -30,7 +28,7 @@ module RedboothRuby
30
28
  # and message
31
29
  #
32
30
  def verify_response_code
33
- status = raw_response.status.to_i
31
+ status = response.status
34
32
  case
35
33
  when status == 401
36
34
  verify_authentication_header
@@ -80,6 +78,36 @@ module RedboothRuby
80
78
  end
81
79
  fail error
82
80
  end
81
+
82
+ # Builds response object from raw received response
83
+ #
84
+ # @return [RedboothRuby::Request::Response]
85
+ def response_from_raw
86
+ case raw_response
87
+ when Net::HTTPResponse
88
+ response_from_http
89
+ else
90
+ response_from_rest_client
91
+ end
92
+ end
93
+
94
+ # Builds response object form RestClient::Response object
95
+ #
96
+ # @return [RedboothRuby::Request::Response]
97
+ def response_from_rest_client
98
+ RedboothRuby::Request::Response.new(headers: raw_response.headers,
99
+ body: raw_response.body,
100
+ status: raw_response.status.to_i)
101
+ end
102
+
103
+ # Builds response object form Http::Response object
104
+ #
105
+ # @return [RedboothRuby::Request::Response]
106
+ def response_from_http
107
+ RedboothRuby::Request::Response.new(headers: raw_response.to_hash,
108
+ body: raw_response.body,
109
+ status: raw_response.code.to_i)
110
+ end
83
111
  end
84
112
  end
85
113
  end
@@ -0,0 +1,18 @@
1
+ module RedboothRuby
2
+ class Subtask < Base
3
+ include RedboothRuby::Operations::Index
4
+ include RedboothRuby::Operations::Create
5
+ include RedboothRuby::Operations::Update
6
+ include RedboothRuby::Operations::Show
7
+ include RedboothRuby::Operations::Delete
8
+
9
+ attr_accessor :id,
10
+ :name,
11
+ :task_id,
12
+ :resolved,
13
+ :position,
14
+ :created_at,
15
+ :updated_at
16
+
17
+ end
18
+ end
@@ -5,6 +5,7 @@ module RedboothRuby
5
5
  include RedboothRuby::Operations::Update
6
6
  include RedboothRuby::Operations::Show
7
7
  include RedboothRuby::Operations::Delete
8
+ include RedboothRuby::Operations::Meta
8
9
 
9
10
  attr_accessor :id,
10
11
  :name,
@@ -19,7 +20,6 @@ module RedboothRuby
19
20
  :last_activity_id,
20
21
  :record_conversion_type,
21
22
  :record_conversion_id,
22
- :metadata,
23
23
  :subtasks_count,
24
24
  :resolved_subtasks_count,
25
25
  :watcher_ids,