camp3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Camp3
4
+ # Wrapper class of paginated response.
5
+ class PaginatedResponse
6
+ attr_accessor :client
7
+
8
+ def initialize(array)
9
+ @array = array
10
+ end
11
+
12
+ def ==(other)
13
+ @array == other
14
+ end
15
+
16
+ def inspect
17
+ @array.inspect
18
+ end
19
+
20
+ def method_missing(name, *args, &block)
21
+ if @array.respond_to?(name)
22
+ @array.send(name, *args, &block)
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ def respond_to_missing?(method_name, include_private = false)
29
+ super || @array.respond_to?(method_name, include_private)
30
+ end
31
+
32
+ def parse_headers!(headers)
33
+ @links = PageLinks.new headers
34
+ end
35
+
36
+ def each_page
37
+ current = self
38
+ yield current
39
+ while current.has_next_page?
40
+ current = current.next_page
41
+ yield current
42
+ end
43
+ end
44
+
45
+ def lazy_paginate
46
+ to_enum(:each_page).lazy.flat_map(&:to_ary)
47
+ end
48
+
49
+ def auto_paginate(&block)
50
+ return lazy_paginate.to_a unless block_given?
51
+
52
+ lazy_paginate.each(&block)
53
+ end
54
+
55
+ def paginate_with_limit(limit, &block)
56
+ return lazy_paginate.take(limit).to_a unless block_given?
57
+
58
+ lazy_paginate.take(limit).each(&block)
59
+ end
60
+
61
+ def last_page?
62
+ !(@links.nil? || @links.last.nil?)
63
+ end
64
+ alias has_last_page? last_page?
65
+
66
+ def last_page
67
+ return nil if @client.nil? || !has_last_page?
68
+
69
+ @client.get(client_relative_path(@links.last))
70
+ end
71
+
72
+ def first_page?
73
+ !(@links.nil? || @links.first.nil?)
74
+ end
75
+ alias has_first_page? first_page?
76
+
77
+ def first_page
78
+ return nil if @client.nil? || !has_first_page?
79
+
80
+ @client.get(client_relative_path(@links.first))
81
+ end
82
+
83
+ def next_page?
84
+ !(@links.nil? || @links.next.nil?)
85
+ end
86
+ alias has_next_page? next_page?
87
+
88
+ def next_page
89
+ return nil if @client.nil? || !has_next_page?
90
+
91
+ @client.get(client_relative_path(@links.next))
92
+ end
93
+
94
+ def prev_page?
95
+ !(@links.nil? || @links.prev.nil?)
96
+ end
97
+ alias has_prev_page? prev_page?
98
+
99
+ def prev_page
100
+ return nil if @client.nil? || !has_prev_page?
101
+
102
+ @client.get(client_relative_path(@links.prev))
103
+ end
104
+
105
+ def client_relative_path(link)
106
+ client_endpoint_path = URI.parse(@client.endpoint).request_uri # api/v4
107
+ URI.parse(link).request_uri.sub(client_endpoint_path, '')
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ module Camp3
6
+ # @private
7
+ class Request
8
+ include HTTParty
9
+ format :plain
10
+ headers 'Accept' => 'application/json'
11
+
12
+ attr_accessor :access_token
13
+
14
+ # Converts the response body to an ObjectifiedHash.
15
+ def self.parse(body)
16
+ body = decode(body)
17
+
18
+ if body.is_a? Hash
19
+ Resource.create(body)
20
+ elsif body.is_a? Array
21
+ PaginatedResponse.new(body.collect! { |e| Resource.create(e) })
22
+ elsif body
23
+ true
24
+ elsif !body
25
+ false
26
+ elsif body.nil?
27
+ false
28
+ else
29
+ raise Error::Parsing, "Couldn't parse a response body"
30
+ end
31
+ end
32
+
33
+ # Decodes a JSON response into Ruby object.
34
+ def self.decode(response)
35
+ response ? JSON.parse(response) : {}
36
+ rescue JSON::ParserError
37
+ raise Error::Parsing, 'The response is not a valid JSON'
38
+ end
39
+
40
+ %w[get post put delete].each do |method|
41
+ define_method method do |path, options = {}|
42
+ override_path = options.delete(:override_path)
43
+ params = options.dup
44
+
45
+ params[:headers] ||= {}
46
+ params[:headers].merge!(authorization_header)
47
+
48
+ full_endpoint = override_path ? path : Camp3.api_endpoint + path
49
+
50
+ validate self.class.send(method, full_endpoint, params)
51
+ end
52
+ end
53
+
54
+ # Checks the response code for common errors.
55
+ # Returns parsed response for successful requests.
56
+ def validate(response)
57
+ error_klass = Error::STATUS_MAPPINGS[response.code]
58
+ raise error_klass, response if error_klass
59
+
60
+ parsed = self.class.parse(response)
61
+ parsed.client = self if parsed.respond_to?(:client=)
62
+ parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
63
+ parsed
64
+ end
65
+
66
+ private
67
+
68
+ # Returns an Authorization header hash
69
+ #
70
+ # @raise [Error::MissingCredentials] if access_token and auth_token are not set.
71
+ def authorization_header
72
+ raise Error::MissingCredentials, 'Please provide a access_token' unless @access_token
73
+
74
+ { 'Authorization' => "Bearer #{@access_token}" }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,22 @@
1
+ module Camp3
2
+ class Resource < ObjectifiedHash
3
+
4
+ Dir[File.expand_path('resources/*.rb', __dir__)].each { |f| require f }
5
+
6
+ def self.create(hash)
7
+ klass = detect_type(hash["url"])
8
+
9
+ return klass.new(hash)
10
+ end
11
+
12
+ def self.detect_type(url)
13
+ Camp3.logger.debug "Request URL: #{url}"
14
+ case url
15
+ when /#{Camp3.api_endpoint}\/projects\/\d+\.json/
16
+ return Project
17
+ else
18
+ return Resource
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Camp3
2
+ class Project < Resource
3
+
4
+ attr_reader :message_board, :todoset, :schedule
5
+
6
+ def initialize(hash)
7
+ super
8
+
9
+ @message_board = dock.find { |payload| payload.name == 'message_board' }
10
+ @todoset = dock.find { |payload| payload.name == 'todoset' }
11
+ @schedule = dock.find { |payload| payload.name == 'schedule' }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Camp3
4
+ VERSION = "0.0.1"
5
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camp3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - renehernandez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ description:
70
+ email:
71
+ - renehr9102@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".editorconfig"
77
+ - ".github/workflows/ci.yml"
78
+ - ".github/workflows/gem-push.yml"
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".rubocop.yml"
82
+ - ".rubocop_todo.yml"
83
+ - ".ruby-version"
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - camp3.gemspec
92
+ - examples/messages.rb
93
+ - examples/oauth.rb
94
+ - examples/obtain_acces_token.rb
95
+ - examples/todos.rb
96
+ - lib/camp3.rb
97
+ - lib/camp3/api/message.rb
98
+ - lib/camp3/api/project.rb
99
+ - lib/camp3/api/resource.rb
100
+ - lib/camp3/api/todo.rb
101
+ - lib/camp3/authorization.rb
102
+ - lib/camp3/client.rb
103
+ - lib/camp3/configuration.rb
104
+ - lib/camp3/error.rb
105
+ - lib/camp3/logging.rb
106
+ - lib/camp3/objectified_hash.rb
107
+ - lib/camp3/page_links.rb
108
+ - lib/camp3/paginated_response.rb
109
+ - lib/camp3/request.rb
110
+ - lib/camp3/resource.rb
111
+ - lib/camp3/resources/project.rb
112
+ - lib/camp3/version.rb
113
+ homepage: https://github.com/renehernandez/camp3
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ allowed_push_host: https://rubygems.org
118
+ homepage_uri: https://github.com/renehernandez/camp3
119
+ source_code_uri: https://github.com/renehernandez/camp3
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 2.5.0
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.1.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Ruby client for Basecamp 3 API
139
+ test_files: []