monkey-business 1.0.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7f1109a46b4a4a04942c094bde6ce3bbef2683c
4
+ data.tar.gz: 4bffec75ad3beb6d3dbd16b7e46570535ec89321
5
+ SHA512:
6
+ metadata.gz: 2142f874d4d0c8248f0ee5f520fa63dae2b3fd1d6c23bf4e09f8d3a01ef8052ee6267a88e3160b7884077307137738014d99abd1920e0742173a8f722a3d7ddc
7
+ data.tar.gz: 7befab103d4461a98d478a07abc356ab858e6d470f3c0f379f846a3843fcbfddcf6c71d4606308895043dd90cffda3f0a63c6e21282064364a3c97340347554c
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../exceptions.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Base class for all Surveymonkey API resources
7
+ class ApiResource
8
+ attr_accessor :path
9
+
10
+ def initialize(api, options = {}, path_prefix = nil)
11
+ @api = api
12
+ @id = options.delete(:id)
13
+ @options = options
14
+
15
+ resource_name = self.class
16
+ .name
17
+ .gsub(/.+::/, '')
18
+ .gsub(/(.)([A-Z])/, '\1_\2')
19
+ .downcase
20
+
21
+ @path = "#{path_prefix}/#{resource_name}"
22
+ @path += "/#{@id}" if @id
23
+ end
24
+
25
+ def request
26
+ @api.request(path, @options)
27
+ end
28
+
29
+ private
30
+
31
+ def fail_without_id
32
+ return if @id
33
+ raise MonkeyBusiness::ApiMethodError.new(
34
+ <<~MESSAGE
35
+ ID must be provided for the following method:
36
+ #{caller_locations(1, 1)[0].label}
37
+ Expected:
38
+ #{@path}/:id
39
+ Received:
40
+ #{@path}
41
+ MESSAGE
42
+ )
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+ require_relative 'responses.rb'
5
+
6
+ module MonkeyBusiness
7
+ # Abstraction of the Surveymonkey collectors resource and associated methods
8
+ class Collectors < ApiResource
9
+ def responses(options = {})
10
+ Responses.new(@api, @options.merge(options), @path)
11
+ end
12
+ end
13
+ end
data/lib/api/pages.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+ require_relative 'questions.rb'
5
+
6
+ module MonkeyBusiness
7
+ # Abstraction of the Surveymonkey page resource and associated methods
8
+ class Pages < ApiResource
9
+ def questions(options = {})
10
+ fail_without_id
11
+ Questions.new(@api, @options.merge(options), @path)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Abstraction of the Surveymonkey question resource and associated methods
7
+ class Questions < ApiResource
8
+ end
9
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+ require_relative '../exceptions.rb'
7
+
8
+ module MonkeyBusiness
9
+ # Abstraction of an HTTP request sent to the surveymonkey API
10
+ class HttpRequest
11
+ def self.request(access_token, uri, options = {})
12
+ @access_token = access_token
13
+ @uri = URI(uri)
14
+
15
+ @http_method = options[:method] ? options.delete(:method).to_sym : :get
16
+ @options = options
17
+
18
+ raise HttpMethodError, @http_method unless respond_to?(@http_method)
19
+
20
+ @http = Net::HTTP.new(@uri.host, @uri.port)
21
+ @http.use_ssl = true
22
+
23
+ send(@http_method)
24
+ end
25
+
26
+ def self.get
27
+ params = @options.map { |k, v| "#{k}=#{v}" }.join('&')
28
+
29
+ request = Net::HTTP::Get.new("#{@uri.request_uri}?#{params}")
30
+ request['Authorization'] = "bearer #{@access_token}"
31
+ request['Content-Type'] = 'application/json'
32
+
33
+ response = @http.request(request)
34
+
35
+ JSON.parse(response.body)
36
+ end
37
+
38
+ def self.post
39
+ raise NotImplementedError, 'Not Yet Implemented'
40
+ end
41
+
42
+ def self.head
43
+ request = Net::HTTP::Head.new(@uri.request_uri)
44
+ request['Authorization'] = "bearer #{@access_token}"
45
+ request['Content-Type'] = 'application/json'
46
+
47
+ @http.request(request).to_hash
48
+ end
49
+
50
+ def self.options
51
+ request = Net::HTTP::Options.new(@uri.request_uri)
52
+
53
+ request['Authorization'] = "bearer #{@access_token}"
54
+ request['Content-Type'] = 'application/json'
55
+
56
+ response = @http.request(request)
57
+
58
+ response['Allow'].split(',').map { |x| x.downcase.to_sym }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Abstraction of the Surveymonkey responses resource and associated methods
7
+ class Responses < ApiResource
8
+ def details(options = {})
9
+ @options.merge!(options)
10
+ @path += '/details'
11
+ self
12
+ end
13
+
14
+ def bulk(options = {})
15
+ @options.merge!(options)
16
+ @path += '/bulk'
17
+ self
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Abstraction of the Surveymonkey survey_category resource and associated methods
7
+ class SurveyCategories < ApiResource
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Abstraction of the Surveymonkey survey_template resource and associated methods
7
+ class SurveyTemplates < ApiResource
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+ require_relative 'responses.rb'
5
+ require_relative 'collectors.rb'
6
+ require_relative 'pages.rb'
7
+
8
+ module MonkeyBusiness
9
+ # Abstraction of the Surveymonkey survey resource and associated methods
10
+ class Surveys < ApiResource
11
+ def details(options = {})
12
+ fail_without_id
13
+ @options.merge!(options)
14
+ @path += '/details'
15
+ self
16
+ end
17
+
18
+ def responses(options = {})
19
+ fail_without_id
20
+ Responses.new(@api, @options.merge(options), @path)
21
+ end
22
+
23
+ def collectors(options = {})
24
+ fail_without_id
25
+ Collectors.new(@api, @options.merge(options), @path)
26
+ end
27
+
28
+ def pages(options = {})
29
+ fail_without_id
30
+ Pages.new(@api, @options.merge(options), @path)
31
+ end
32
+ end
33
+ end
data/lib/api/users.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_resource.rb'
4
+
5
+ module MonkeyBusiness
6
+ # Abstraction of the Surveymonkey users resource and associated methods
7
+ class Users < ApiResource
8
+ def me(options = {})
9
+ @options.merge!(options)
10
+ @path += '/me'
11
+ self
12
+ end
13
+ end
14
+ end
data/lib/exceptions.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ module MonkeyBusiness
4
+ # Raise this error for malformed API requests
5
+ class ApiMethodError < ArgumentError; end
6
+
7
+ # Raise this error for unsupported HTTP methods
8
+ class HttpMethodError < ArgumentError
9
+ def initialize(method_name)
10
+ super("Unsupported HTTP method: #{method_name}")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api/request.rb'
4
+ require_relative 'api/surveys.rb'
5
+ require_relative 'api/survey_categories.rb'
6
+ require_relative 'api/survey_templates.rb'
7
+ require_relative 'api/users.rb'
8
+
9
+ module MonkeyBusiness
10
+ API_VERSION = 'v3'
11
+ BASE_URI = "https://api.surveymonkey.net/#{API_VERSION}"
12
+
13
+ # This is the class the end-user should be interacting with.
14
+ class API
15
+ def initialize
16
+ @access_token = ENV['SURVEYMONKEY_ACCESS_TOKEN']
17
+ end
18
+
19
+ def surveys(options = {})
20
+ Surveys.new(self, options)
21
+ end
22
+
23
+ def survey_categories(options = {})
24
+ SurveyCategories.new(self, options)
25
+ end
26
+
27
+ def survey_templates(options = {})
28
+ SurveyTemplates.new(self, options)
29
+ end
30
+
31
+ def users(options = {})
32
+ Users.new(self, options)
33
+ end
34
+
35
+ def request(resource_path, options = {})
36
+ HttpRequest.request(
37
+ @access_token,
38
+ BASE_URI + resource_path,
39
+ options
40
+ )
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monkey-business
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Chris Escue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Send requests to the Surveymonkey API from your Ruby scripts!
14
+ email: chris@escue.io
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/api/api_resource.rb
20
+ - lib/api/collectors.rb
21
+ - lib/api/pages.rb
22
+ - lib/api/questions.rb
23
+ - lib/api/request.rb
24
+ - lib/api/responses.rb
25
+ - lib/api/survey_categories.rb
26
+ - lib/api/survey_templates.rb
27
+ - lib/api/surveys.rb
28
+ - lib/api/users.rb
29
+ - lib/exceptions.rb
30
+ - lib/monkey_business.rb
31
+ homepage: https://github.com/cescue/monkey-business
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.5.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: A simple Ruby client for version 3 of the SurveyMonkey API
55
+ test_files: []