kindrid_client 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kindrid_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Anderson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # KindridClient
2
+
3
+ A Ruby wrapper around the Kindrid API that lets you lookup donations and donors.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'kindrid_client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install kindrid_client
18
+
19
+ ## Usage
20
+
21
+ To set up the gem, add keys:
22
+
23
+ KindridClient.key = "key"
24
+ KindridClient.secret = "secret"
25
+
26
+
27
+ Now you may run the following commands:
28
+
29
+ Donations
30
+
31
+ KindridClient.donations # optional limit and offset params (ex: {limit: 15, offset: 2})
32
+ KindridClient.donations(donor_id) # optional limit and offset params (ex: {limit: 15, offset: 2})
33
+ KindridClient.donation(donation_id)
34
+
35
+ Donors
36
+
37
+ KindridClient.donors # optional limit and offset params (ex: {limit: 15, offset: 2})
38
+ KindridClient.donor(donor_id)
39
+
40
+ Tags
41
+
42
+ KindridClient.tags(donor_id)
43
+ KindridClient.tags(donor_id).put({tag_key: "value"}) # add tags
44
+ KindridClient.tags(donor_id).delete("tag_key") # remove tag
45
+
46
+
47
+ Also, to consume requests from Kindrid and validate the HMAC with your secret key, use the following:
48
+
49
+ KindridClient.consume(request)
50
+
51
+
52
+ For more information about the Kindrid API, you can view their api documentation here: http://kindrid.com/dashboard/settings/api/docs
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kindrid_client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "kindrid_client"
8
+ gem.version = KindridClient::VERSION
9
+ gem.authors = ["David Anderson"]
10
+ gem.email = ["davidmartinanderson@gmail.com"]
11
+ gem.description = %q{Client gem for the Kindrid service}
12
+ gem.summary = %q{Client gem for the Kindrid service}
13
+ gem.homepage = ""
14
+
15
+ gem.add_runtime_dependency('rash', '~> 0.3')
16
+ gem.add_runtime_dependency("faraday")
17
+ gem.add_runtime_dependency('multi_json', '~> 1.0')
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'faraday'
2
+ require "kindrid_client/version"
3
+ require 'kindrid_client/client'
4
+ require 'kindrid_client/configuration'
5
+
6
+ module KindridClient
7
+ extend Configuration
8
+
9
+ mattr_accessor :key, :secret
10
+
11
+ # Alias for KindridClient::Client.new
12
+ #
13
+ # @return [KindridClient::Client]
14
+ def self.new(options={})
15
+ KindridClient::Client.new(options)
16
+ end
17
+
18
+ # Delegate to KindridClient::Client
19
+ def self.method_missing(method, *args, &block)
20
+ return super unless new.respond_to?(method)
21
+ new.send(method, *args, &block)
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ require 'kindrid_client/configuration'
2
+ require 'kindrid_client/validate'
3
+ require File.expand_path('../request', __FILE__)
4
+
5
+ module KindridClient
6
+ class Client
7
+
8
+ # @private
9
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
10
+
11
+ # Creates a new Client
12
+ def initialize(options={})
13
+ options = KindridClient.options.merge(options)
14
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
15
+ send("#{key}=", options[key])
16
+ end
17
+ end
18
+
19
+ def consume(request, options = {})
20
+ unless options[:validate] == false
21
+ if !validate(request)
22
+ puts "Incorrect signature"
23
+ return
24
+ end
25
+ end
26
+
27
+ result = Hashie::Mash.new(request.params)
28
+ result[result.keys.first] unless result.blank?
29
+ end
30
+
31
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
32
+
33
+ alias :api_endpoint :endpoint
34
+
35
+ include Validate
36
+ include Request
37
+ include Donation
38
+ include Donor
39
+ include Tag
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ module KindridClient
2
+ module Donation
3
+
4
+ def donations(donor_id = nil, options={})
5
+ params = {}
6
+ params.merge!(options)
7
+
8
+ # clear empty key/value pairs
9
+ params.reject! { |key, value| value.nil? }
10
+
11
+ if donor_id.blank?
12
+ get("donations", params).results
13
+ else
14
+ get("donors/#{donor_id}/donations", params).results
15
+ end
16
+ end
17
+
18
+ def donation(id, options={})
19
+ params = {}
20
+ params.merge!(options)
21
+
22
+ # clear empty key/value pairs
23
+ params.reject! { |key, value| value.nil? }
24
+
25
+ get("donations/#{id}", params).results.first
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module KindridClient
2
+ module Donor
3
+
4
+ def donors(options={})
5
+ params = {}
6
+ params.merge!(options)
7
+
8
+ # clear empty key/value pairs
9
+ params.reject! { |key, value| value.nil? }
10
+
11
+ get("donors", params).results
12
+ end
13
+
14
+ def donor(id, options={})
15
+ params = {}
16
+ params.merge!(options)
17
+
18
+ # clear empty key/value pairs
19
+ params.reject! { |key, value| value.nil? }
20
+
21
+ get("donors/#{id}", params).results.first
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ module KindridClient
2
+ module Tag
3
+
4
+ module Extensions
5
+
6
+ def put(data)
7
+ KindridClient.put("donors/#{self.donor_id}/tags", data)
8
+ end
9
+
10
+ def delete(tag_key)
11
+ KindridClient.delete("donors/#{self.donor_id}/tags/#{tag_key}")
12
+ end
13
+
14
+ end
15
+
16
+ def tags(donor_id, options={})
17
+ params = {}
18
+ params.merge!(options)
19
+
20
+ # clear empty key/value pairs
21
+ params.reject! { |key, value| value.nil? }
22
+
23
+ # add donor_id to results and allow put and delete methods
24
+ get("donors/#{donor_id}/tags", params).tap { |response|
25
+ response.class.instance_eval do
26
+ define_method(:donor_id) do
27
+ response.metadata.resultset.donorID
28
+ end
29
+ end
30
+ }.results.extend(Extensions)
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,61 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module KindridClient
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+
8
+ # An array of valid keys in the options hash when configuring
9
+ VALID_OPTIONS_KEYS = [:adapter, :endpoint, :limit, :user_agent, :request_options].freeze
10
+
11
+ # The adapter that will be used to connect if none is set
12
+ #
13
+ # @note The default faraday adapter is Net::HTTP.
14
+ DEFAULT_ADAPTER = Faraday.default_adapter
15
+
16
+ # The endpoint that will be used to connect if none is set
17
+ DEFAULT_ENDPOINT = 'https://kindrid.com/api/v1'.freeze
18
+
19
+ # The user agent that will be sent to the API endpoint if none is set
20
+ DEFAULT_USER_AGENT = "Kindrid Client Gem #{KindridClient::VERSION}".freeze
21
+
22
+ # The default number of results to return from the API
23
+ DEFAULT_LIMIT = nil
24
+
25
+ # The default request options for Faraday
26
+ DEFAULT_REQUEST_OPTIONS = {
27
+ :timeout => 5,
28
+ :open_timeout => 5
29
+ }
30
+
31
+ # @private
32
+ attr_accessor *VALID_OPTIONS_KEYS
33
+
34
+ # When this module is extended, set all configuration options to their default values
35
+ def self.extended(base)
36
+ base.reset
37
+ end
38
+
39
+ # Convenience method to allow configuration options to be set in a block
40
+ def configure
41
+ yield self
42
+ end
43
+
44
+ # Create a hash of options and their values
45
+ def options
46
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
47
+ option.merge!(key => send(key))
48
+ end
49
+ end
50
+
51
+ # Reset all configuration options to defaults
52
+ def reset
53
+ self.adapter = DEFAULT_ADAPTER
54
+ self.endpoint = DEFAULT_ENDPOINT
55
+ self.user_agent = DEFAULT_USER_AGENT
56
+ self.request_options = DEFAULT_REQUEST_OPTIONS
57
+ self.limit = DEFAULT_LIMIT
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,67 @@
1
+ module KindridClient
2
+ module Request
3
+
4
+ # Perform an HTTP GET request
5
+ def get(resource_path, params = {})
6
+ request = construct_request(resource_path, params)
7
+
8
+ response = connection.get do |req|
9
+ req.url request[:url], request[:params]
10
+ req.options = request_options
11
+ end
12
+ Hashie::Mash.new(response.body)
13
+ end
14
+
15
+ def post(resource_path, data)
16
+ request = construct_request(resource_path)
17
+
18
+ response = connection.post do |req|
19
+ req.url request[:url]
20
+ req.body = data.to_json
21
+ req.options = request_options
22
+ end
23
+ Hashie::Mash.new(response.body)
24
+ end
25
+
26
+ def put(resource_path, data)
27
+ request = construct_request(resource_path)
28
+
29
+ response = connection.put do |req|
30
+ req.url request[:url]
31
+ req.body = data.to_json
32
+ req.options = request_options
33
+ end
34
+ Hashie::Mash.new(response.body)
35
+ end
36
+
37
+ def delete(resource_path)
38
+ request = construct_request(resource_path)
39
+
40
+ response = connection.delete do |req|
41
+ req.url request[:url]
42
+ req.options = request_options
43
+ end
44
+ Hashie::Mash.new(response.body)
45
+ end
46
+
47
+ def construct_request(resource_path, params={})
48
+ request = {}
49
+ request[:url] = "#{Configuration::DEFAULT_ENDPOINT}/#{resource_path}"
50
+ request[:params] = params.merge({client_id: KindridClient.key})
51
+ request
52
+ end
53
+
54
+ def connection
55
+ options = {
56
+ :headers => {'Accept' => 'application/json', 'User-Agent' => user_agent},
57
+ :url => api_endpoint,
58
+ }
59
+
60
+ Faraday.new(options) do |builder|
61
+ builder.basic_auth(KindridClient.key, KindridClient.secret)
62
+ builder.request :json
63
+ builder.adapter(adapter)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,16 @@
1
+ module KindridClient
2
+ module Validate
3
+
4
+ private
5
+
6
+ def validate(request)
7
+ message = "#{request.url}#{request.body.read}"
8
+ digest = OpenSSL::Digest::Digest.new('sha1')
9
+ hmac = OpenSSL::HMAC.hexdigest(digest, KindridClient.secret, message)
10
+ request_hmac = request.headers["X-Kindrid-Signature"]
11
+
12
+ hmac == request_hmac
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module KindridClient
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kindrid_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rash
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: multi_json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ description: Client gem for the Kindrid service
63
+ email:
64
+ - davidmartinanderson@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - kindrid_client.gemspec
75
+ - lib/kindrid_client.rb
76
+ - lib/kindrid_client/client.rb
77
+ - lib/kindrid_client/client/donation.rb
78
+ - lib/kindrid_client/client/donor.rb
79
+ - lib/kindrid_client/client/tag.rb
80
+ - lib/kindrid_client/configuration.rb
81
+ - lib/kindrid_client/request.rb
82
+ - lib/kindrid_client/validate.rb
83
+ - lib/kindrid_client/version.rb
84
+ homepage: ''
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.23
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Client gem for the Kindrid service
108
+ test_files: []
109
+ has_rdoc: