crowdskout 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/README.md +53 -0
- data/crowdskout.gemspec +31 -0
- data/lib/crowdskout.rb +65 -0
- data/lib/crowdskout/api.rb +57 -0
- data/lib/crowdskout/auth/oauth2.rb +81 -0
- data/lib/crowdskout/components/attributes/attribute.rb +31 -0
- data/lib/crowdskout/components/attributes/option.rb +22 -0
- data/lib/crowdskout/components/component.rb +42 -0
- data/lib/crowdskout/components/fields/field_options.rb +31 -0
- data/lib/crowdskout/components/profiles/gender.rb +40 -0
- data/lib/crowdskout/components/profiles/name.rb +22 -0
- data/lib/crowdskout/components/profiles/profile.rb +44 -0
- data/lib/crowdskout/components/result_set.rb +15 -0
- data/lib/crowdskout/exceptions/oauth2_exception.rb +5 -0
- data/lib/crowdskout/exceptions/service_exception.rb +5 -0
- data/lib/crowdskout/services/attribute_service.rb +66 -0
- data/lib/crowdskout/services/base_service.rb +50 -0
- data/lib/crowdskout/services/field_service.rb +17 -0
- data/lib/crowdskout/services/profile_service.rb +76 -0
- data/lib/crowdskout/util/config.rb +75 -0
- data/lib/crowdskout/util/helpers.rb +21 -0
- data/lib/crowdskout/version.rb +23 -0
- data/spec/crowdskout/auth/oauth2_spec.rb +114 -0
- data/spec/crowdskout/services/attribute_service_spec.rb +83 -0
- data/spec/crowdskout/services/field_service_spec.rb +27 -0
- data/spec/crowdskout/services/profile_service_spec.rb +116 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67d2e0750c8be6e9368412b8fd03f60664fa9c9f
|
4
|
+
data.tar.gz: 8ab86a1381952837ee0a1fd3ab4dd186afb3ee05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 382153bfdb158fe5d8c4fc6d6bfee0f457fdbbc093e505ebc06bd67fc1c44347d7aa34340db802af541cc837e895d167403f177500857bc94a1b0434c237eba2
|
7
|
+
data.tar.gz: 64842c469da953513a9e23f99e31968594f7f45d572def406380843f09111a2c4fb7c1e423ca300ecc6918280c8f266ce045559f4877aecd279449358c305c33
|
data/.rspec
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Crowdskout
|
2
|
+
|
3
|
+
It is a Ruby wrapper for the Crowdskout API.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/revvco/crowdskout.png?branch=master)](https://travis-ci.org/revvco/crowdskout.png?branch=master)
|
6
|
+
|
7
|
+
Visit also a Crowdskout API documentation: [here](http://docs.crowdskout.com/api/#get-the-options-for-a-field)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'crowdskout'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install crowdskout
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
**Example for OAuth provider:**
|
26
|
+
|
27
|
+
require 'crowdskout'
|
28
|
+
|
29
|
+
oauth_provider = Crowdskout::Auth::OAuth2.new(
|
30
|
+
:api_key => client_id,
|
31
|
+
:api_secret => client_secret,
|
32
|
+
:redirect_url => redirect_uri
|
33
|
+
)
|
34
|
+
|
35
|
+
url = oauth_provider.get_authorization_url("a_state_param")
|
36
|
+
|
37
|
+
# after post to URL and granting access
|
38
|
+
access_token = oauth_provider.get_access_token(params[:code])
|
39
|
+
|
40
|
+
**Example for Crowdskout API:**
|
41
|
+
|
42
|
+
require 'crowdskout'
|
43
|
+
|
44
|
+
api = Crowdskout::Api.new(api_key, access_token)
|
45
|
+
|
46
|
+
# Fetching profiles
|
47
|
+
profile = api.get_profile(164)
|
48
|
+
|
49
|
+
# Fetching field options
|
50
|
+
field_options = api.get_options_for_a_field("AddressCity")
|
51
|
+
|
52
|
+
# Fetching attributes
|
53
|
+
attributes = api.get_attributes
|
data/crowdskout.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "crowdskout"
|
8
|
+
s.version = '0.0.4'
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["Crowdskout", "Revv","Kyle Schutt"]
|
11
|
+
s.homepage = "https://github.com/revvco/crowdskout"
|
12
|
+
s.summary = %q{Crowdskout SDK for Ruby}
|
13
|
+
s.email = "kyle@revv.co"
|
14
|
+
s.description = "Ruby library for interactions with Crowdskout v1 API"
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.files = [
|
18
|
+
'.rspec',
|
19
|
+
'crowdskout.gemspec',
|
20
|
+
'README.md'
|
21
|
+
]
|
22
|
+
s.files += Dir['lib/**/*.rb']
|
23
|
+
s.executables = []
|
24
|
+
s.require_paths = [ "lib" ]
|
25
|
+
s.test_files = Dir['spec/**/*_spec.rb']
|
26
|
+
|
27
|
+
s.add_runtime_dependency("rest-client", '~> 1.6', '>= 1.6.7')
|
28
|
+
s.add_runtime_dependency("json", '~> 1.8', '>= 1.8.1')
|
29
|
+
s.add_runtime_dependency('mime-types', '~> 1.25', '>= 1.25.1')
|
30
|
+
s.add_development_dependency("rspec", '~> 2.14')
|
31
|
+
end
|
data/lib/crowdskout.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Copyright, 2016, by Kyle Schutt.
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'rubygems'
|
22
|
+
require 'rest_client'
|
23
|
+
require 'json'
|
24
|
+
require 'cgi'
|
25
|
+
require 'cgi/session'
|
26
|
+
require 'cgi/session/pstore'
|
27
|
+
require 'openssl'
|
28
|
+
require 'base64'
|
29
|
+
|
30
|
+
module Crowdskout
|
31
|
+
autoload :Api, 'crowdskout/api'
|
32
|
+
autoload :VERSION, 'crowdskout/version'
|
33
|
+
|
34
|
+
module Auth
|
35
|
+
autoload :OAuth2, 'crowdskout/auth/oauth2'
|
36
|
+
end
|
37
|
+
|
38
|
+
module Services
|
39
|
+
autoload :BaseService, 'crowdskout/services/base_service'
|
40
|
+
autoload :ProfileService, 'crowdskout/services/profile_service'
|
41
|
+
autoload :FieldService, 'crowdskout/services/field_service'
|
42
|
+
autoload :AttributeService, 'crowdskout/services/attribute_service'
|
43
|
+
end
|
44
|
+
|
45
|
+
module Components
|
46
|
+
autoload :Component, 'crowdskout/components/component'
|
47
|
+
autoload :ResultSet, 'crowdskout/components/result_set'
|
48
|
+
autoload :Attribute, 'crowdskout/components/attributes/attribute'
|
49
|
+
autoload :Option, 'crowdskout/components/attributes/option'
|
50
|
+
autoload :Profile, 'crowdskout/components/profiles/profile'
|
51
|
+
autoload :Name, 'crowdskout/components/profiles/name'
|
52
|
+
autoload :Gender, 'crowdskout/components/profiles/gender'
|
53
|
+
autoload :FieldOptions, 'crowdskout/components/fields/field_options'
|
54
|
+
end
|
55
|
+
|
56
|
+
module Exceptions
|
57
|
+
autoload :OAuth2Exception, 'crowdskout/exceptions/oauth2_exception'
|
58
|
+
autoload :ServiceException, 'crowdskout/exceptions/service_exception'
|
59
|
+
end
|
60
|
+
|
61
|
+
module Util
|
62
|
+
autoload :Config, 'crowdskout/util/config'
|
63
|
+
autoload :Helpers, 'crowdskout/util/helpers'
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Crowdskout
|
2
|
+
class Api
|
3
|
+
# Class constructor
|
4
|
+
# @param [String] api_key - Crowdskout API Key
|
5
|
+
# @param [String] access_token - Crowdskout OAuth2 access token
|
6
|
+
# @return
|
7
|
+
def initialize(api_key = nil, access_token = nil)
|
8
|
+
Services::BaseService.api_key = api_key || Util::Config.get('auth.api_key')
|
9
|
+
Services::BaseService.access_token = access_token
|
10
|
+
if Services::BaseService.api_key.nil? || Services::BaseService.api_key == ''
|
11
|
+
raise ArgumentError.new(Util::Config.get('errors.api_key_missing'))
|
12
|
+
end
|
13
|
+
if Services::BaseService.access_token.nil? || Services::BaseService.access_token == ''
|
14
|
+
raise ArgumentError.new(Util::Config.get('errors.access_token_missing'))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Profile Service Methods
|
19
|
+
def get_profile(profile_id, collections)
|
20
|
+
Services::ProfileService.get_profile(profile_id, collections)
|
21
|
+
end
|
22
|
+
def create_profile(profile, params = {})
|
23
|
+
Services::ProfileService.create_profile(profile, params)
|
24
|
+
end
|
25
|
+
def create_profiles_bulk(profiles)
|
26
|
+
Services::ProfileService.create_profiles_bulk(profiles)
|
27
|
+
end
|
28
|
+
def update_profile(profile)
|
29
|
+
Services::ProfileService.update_profile(profile)
|
30
|
+
end
|
31
|
+
def update_profiles_bulk(profiles)
|
32
|
+
Services::ProfileService.update_profiles_bulk(profiles)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Fields Service Methods
|
36
|
+
def get_options_for_a_field(field_name, params = {})
|
37
|
+
Services::FieldService.get_options_for_a_field(field_name, params)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Attribute Service Methods
|
41
|
+
def get_attributes(params = {})
|
42
|
+
Services::AttributeService.get_attributes(params)
|
43
|
+
end
|
44
|
+
def get_attribute(attribute_id)
|
45
|
+
Services::AttributeService.get_attribute(attribute_id)
|
46
|
+
end
|
47
|
+
def create_attribute(name, type, options = nil)
|
48
|
+
Services::AttributeService.create_attribute(name, type, options)
|
49
|
+
end
|
50
|
+
def update_attribute(attribute_id, params = {})
|
51
|
+
Services::AttributeService.update_attribute(attribute_id, params)
|
52
|
+
end
|
53
|
+
def delete_attribute(attribute_id)
|
54
|
+
Services::AttributeService.delete_attribute(attribute_id)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Crowdskout
|
2
|
+
module Auth
|
3
|
+
class OAuth2
|
4
|
+
attr_accessor :client_id, :client_secret, :redirect_uri, :props
|
5
|
+
|
6
|
+
|
7
|
+
# Class constructor
|
8
|
+
# @param [Hash] opts - the options to create an OAuth2 object with
|
9
|
+
# @option opts [String] :api_key - the Crowdskout API Key
|
10
|
+
# @option opts [String] :api_secret - the Crowdskout secret key
|
11
|
+
# @option opts [String] :redirect_url - the URL where Crowdskout is returning the authorization code
|
12
|
+
# @return
|
13
|
+
def initialize(opts = {})
|
14
|
+
@client_id = opts[:api_key] || Util::Config.get('auth.api_key')
|
15
|
+
@client_secret = opts[:api_secret] || Util::Config.get('auth.api_secret')
|
16
|
+
@redirect_uri = opts[:redirect_url] || Util::Config.get('auth.redirect_uri')
|
17
|
+
if @client_id.nil? || @client_id == '' || @client_secret.nil? || @client_secret.nil? || @redirect_uri.nil? || @redirect_uri == ''
|
18
|
+
raise ArgumentError.new "Either api_key, api_secret or redirect_uri is missing in explicit call or configuration."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# Get the URL at which the user can authenticate and authorize the requesting application
|
24
|
+
# @param [String] state - an optional value used by the client to maintain state between the request and callback
|
25
|
+
# @return [String] the authorization URL
|
26
|
+
def get_authorization_url(state = nil)
|
27
|
+
response_type = Util::Config.get('auth.response_type_code')
|
28
|
+
params = {
|
29
|
+
:response_type => response_type,
|
30
|
+
:client_id => @client_id,
|
31
|
+
:redirect_uri => @redirect_uri
|
32
|
+
}
|
33
|
+
if state
|
34
|
+
params[:state] = state
|
35
|
+
end
|
36
|
+
[
|
37
|
+
Util::Config.get('auth.base_url'),
|
38
|
+
Util::Config.get('auth.authorization_endpoint'),
|
39
|
+
'?',
|
40
|
+
Util::Helpers.http_build_query(params)
|
41
|
+
].join
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# Obtain an access token
|
46
|
+
# @param [String] code - the code returned from Crowdskout after a user has granted access to his account
|
47
|
+
# @return [String] the access token
|
48
|
+
def get_access_token(code)
|
49
|
+
params = {
|
50
|
+
:grant_type => Util::Config.get('auth.authorization_code_grant_type'),
|
51
|
+
:client_id => @client_id,
|
52
|
+
:client_secret => @client_secret,
|
53
|
+
:code => code,
|
54
|
+
:redirect_uri => @redirect_uri
|
55
|
+
}
|
56
|
+
|
57
|
+
url = [
|
58
|
+
Util::Config.get('auth.base_url'),
|
59
|
+
Util::Config.get('auth.token_endpoint')
|
60
|
+
].join
|
61
|
+
|
62
|
+
response_body = ''
|
63
|
+
begin
|
64
|
+
response = RestClient.post(url, params)
|
65
|
+
response_body = JSON.parse(response)
|
66
|
+
rescue => e
|
67
|
+
response_body = e.respond_to?(:response) && e.response ?
|
68
|
+
JSON.parse(e.response) :
|
69
|
+
{'error' => '', 'error_description' => e.message}
|
70
|
+
end
|
71
|
+
|
72
|
+
if response_body['error_description']
|
73
|
+
error = response_body['error_description']
|
74
|
+
raise Exceptions::OAuth2Exception, error
|
75
|
+
end
|
76
|
+
|
77
|
+
response_body
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Crowdskout
|
2
|
+
module Components
|
3
|
+
class Attribute < Component
|
4
|
+
attr_accessor :id, :type, :locked, :name, :options
|
5
|
+
|
6
|
+
def self.create(props)
|
7
|
+
obj = Attribute.new
|
8
|
+
if props
|
9
|
+
props.each do |key, value|
|
10
|
+
if key.downcase == 'options'
|
11
|
+
if value
|
12
|
+
obj.options = []
|
13
|
+
value.each do |option|
|
14
|
+
obj.options << Components::Option.create(option)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
else
|
18
|
+
obj.send("#{key}=", value) if obj.respond_to? key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_options(option)
|
26
|
+
@options = [] if @options.nil?
|
27
|
+
@options << option
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2016 Kyle Schutt. All rights reserved.require
|
2
|
+
|
3
|
+
module Crowdskout
|
4
|
+
module Components
|
5
|
+
class Option < Component
|
6
|
+
attr_accessor :id, :value
|
7
|
+
|
8
|
+
# Factory method to create an Address object from a json string
|
9
|
+
# @param [Hash] props - properties to create object from
|
10
|
+
# @return [Address]
|
11
|
+
def self.create(props)
|
12
|
+
obj = Option.new
|
13
|
+
if props
|
14
|
+
props.each do |key, value|
|
15
|
+
obj.send("#{key}=", value) if obj.respond_to? key
|
16
|
+
end
|
17
|
+
end
|
18
|
+
obj
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Crowdskout
|
2
|
+
module Components
|
3
|
+
class Component
|
4
|
+
|
5
|
+
def to_hash
|
6
|
+
hash = Hash.new
|
7
|
+
self.instance_variables.collect do |var|
|
8
|
+
hash[var.to_s[1..-1]] = self.class.to_hash_value(self.instance_variable_get(var))
|
9
|
+
end
|
10
|
+
hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.to_hash_value(val)
|
14
|
+
if val.is_a? Crowdskout::Components::Component
|
15
|
+
return val.to_hash
|
16
|
+
elsif val.is_a? Array
|
17
|
+
return val.collect{|subval| Component.to_hash_value(subval) }
|
18
|
+
elsif val.is_a? DateTime
|
19
|
+
return val.to_s
|
20
|
+
else
|
21
|
+
return val
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_json(val = nil)
|
26
|
+
self.to_hash.to_json
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
# Get the requested value from a hash, or return the default
|
32
|
+
# @param [Hash] hsh - the hash to search for the provided hash key
|
33
|
+
# @param [String] key - hash key to look for
|
34
|
+
# @param [String] default - value to return if the key is not found, default is null
|
35
|
+
# @return [String]
|
36
|
+
def self.get_value(hsh, key, default = nil)
|
37
|
+
hsh.has_key?(key) and hsh[key] ? hsh[key] : default
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Crowdskout
|
2
|
+
module Components
|
3
|
+
class FieldOptions < Component
|
4
|
+
attr_accessor :id, :collection, :options
|
5
|
+
|
6
|
+
def self.create(props)
|
7
|
+
obj = FieldOptions.new
|
8
|
+
if props
|
9
|
+
props.each do |key, value|
|
10
|
+
if key.downcase == 'options'
|
11
|
+
if value
|
12
|
+
obj.options = []
|
13
|
+
value.each do |option|
|
14
|
+
obj.options << Components::Option.create(option)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
else
|
18
|
+
obj.send("#{key}=", value) if obj.respond_to? key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_options(option)
|
26
|
+
@options = [] if @options.nil?
|
27
|
+
@options << option
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Copyright (c) 2016 Kyle Schutt. All rights reserved.require
|
2
|
+
|
3
|
+
module Crowdskout
|
4
|
+
module Components
|
5
|
+
class Gender < Component
|
6
|
+
attr_accessor :id, :gender
|
7
|
+
|
8
|
+
# Factory method to create an Address object from a json string
|
9
|
+
# @param [Hash] props - properties to create object from
|
10
|
+
# @return [Address]
|
11
|
+
def self.create(props)
|
12
|
+
obj = Gender.new
|
13
|
+
if props
|
14
|
+
props.each do |key, value|
|
15
|
+
if key.downcase == 'gender'
|
16
|
+
obj.gender = GenderInfo.create(value)
|
17
|
+
else
|
18
|
+
obj.send("#{key}=", value) if obj.respond_to? key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Detailed gender information
|
27
|
+
class GenderInfo
|
28
|
+
attr_accessor :id, :value
|
29
|
+
def self.create(props)
|
30
|
+
obj = GenderInfo.new
|
31
|
+
if props
|
32
|
+
props.each do |key, value|
|
33
|
+
obj.send("#{key}=", value) if obj.respond_to? key
|
34
|
+
end
|
35
|
+
end
|
36
|
+
obj
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|