oahu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oahu.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Oahu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Oahu
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'oahu'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install oahu
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ require 'oahu/core_ext/hash'
2
+ require 'oahu/client'
3
+ require 'oahu/config'
4
+
5
+ module Oahu
6
+ extend Config
7
+ class << self
8
+ # Alias for Oahu::Client.new
9
+ #
10
+ # @return [Oahu::Client]
11
+ def new(options={})
12
+ Oahu::Client.new(options)
13
+ end
14
+
15
+ # Delegate to Oahu::Client
16
+ def method_missing(method, *args, &block)
17
+ return super unless new.respond_to?(method)
18
+ new.send(method, *args, &block)
19
+ end
20
+
21
+ def respond_to?(method, include_private=false)
22
+ new.respond_to?(method, include_private) || super(method, include_private)
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'oahu/config'
2
+ require 'oahu/connection'
3
+ require 'oahu/request'
4
+
5
+ module Oahu
6
+ class Client
7
+
8
+ attr_accessor *Config::VALID_OPTIONS_KEYS
9
+
10
+ include Oahu::Connection
11
+ include Oahu::Request
12
+
13
+ # Initializes a new API object
14
+ #
15
+ # @param attrs [Hash]
16
+ # @return [Oahu::Client]
17
+ def initialize(attrs={})
18
+ attrs = Oahu.options.merge(attrs)
19
+ Config::VALID_OPTIONS_KEYS.each do |key|
20
+ instance_variable_set("@#{key}".to_sym, attrs[key])
21
+ end
22
+ end
23
+
24
+ def credentials
25
+ {
26
+ :client_id => client_id,
27
+ :consumer_id => consumer_id,
28
+ :consumer_secret => consumer_secret
29
+ }
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,85 @@
1
+ require 'oahu/version'
2
+
3
+ module Oahu
4
+ # Defines constants and methods related to configuration
5
+ module Config
6
+
7
+ # The HTTP connection adapter that will be used to connect if none is set
8
+ DEFAULT_ADAPTER = :net_http
9
+
10
+ # The Faraday connection options if none is set
11
+ DEFAULT_CONNECTION_OPTIONS = {}
12
+
13
+
14
+ # The client ID if none is set
15
+ DEFAULT_CLIENT_ID = nil
16
+
17
+ # The consumer ID if none is set
18
+ DEFAULT_CONSUMER_ID = nil
19
+
20
+ # The consumer secret if none is set
21
+ DEFAULT_CONSUMER_SECRET = nil
22
+
23
+ # The endpoint that will be used to connect if none is set
24
+ #
25
+ DEFAULT_ENDPOINT = 'https://app.oahu.fr'
26
+
27
+ # The oauth token if none is set
28
+ DEFAULT_OAUTH_TOKEN = nil
29
+
30
+ # The oauth token secret if none is set
31
+ DEFAULT_OAUTH_TOKEN_SECRET = nil
32
+
33
+ DEFAULT_CACHE_STORE = nil
34
+
35
+
36
+ # The value sent in the 'User-Agent' header if none is set
37
+ DEFAULT_USER_AGENT = "Oahu Ruby Gem #{Oahu::VERSION}"
38
+
39
+ # An array of valid keys in the options hash when configuring a {Oahu::Client}
40
+ VALID_OPTIONS_KEYS = [
41
+ :adapter,
42
+ :connection_options,
43
+ :client_id,
44
+ :consumer_id,
45
+ :consumer_secret,
46
+ :endpoint,
47
+ :user_agent,
48
+ :cache_store
49
+ ]
50
+
51
+ attr_accessor *VALID_OPTIONS_KEYS
52
+
53
+ # When this module is extended, set all configuration options to their default values
54
+ def self.extended(base)
55
+ base.reset
56
+ end
57
+
58
+ # Convenience method to allow configuration options to be set in a block
59
+ def configure
60
+ yield self
61
+ self
62
+ end
63
+
64
+ # Create a hash of options and their values
65
+ def options
66
+ options = {}
67
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
68
+ options
69
+ end
70
+
71
+ # Reset all configuration options to defaults
72
+ def reset
73
+ self.adapter = DEFAULT_ADAPTER
74
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
75
+ self.client_id = DEFAULT_CLIENT_ID
76
+ self.consumer_id = DEFAULT_CONSUMER_ID
77
+ self.consumer_secret = DEFAULT_CONSUMER_SECRET
78
+ self.endpoint = DEFAULT_ENDPOINT
79
+ self.user_agent = DEFAULT_USER_AGENT
80
+ self.cache_store = DEFAULT_CACHE_STORE
81
+ self
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday'
2
+ require 'oahu/request/auth'
3
+ require 'faraday_middleware/response/parse_json'
4
+ require 'faraday_middleware/response/caching'
5
+
6
+ module Oahu
7
+ module Connection
8
+ private
9
+
10
+ # Returns a Faraday::Connection object
11
+ #
12
+ # @param options [Hash] A hash of options
13
+ # @return [Faraday::Connection]
14
+ def connection(options={})
15
+ default_options = {
16
+ :headers => {
17
+ :accept => 'application/json',
18
+ :user_agent => user_agent,
19
+ },
20
+ :ssl => {:verify => false},
21
+ :url => options.fetch(:endpoint, endpoint),
22
+ }
23
+ @connection ||= Faraday.new(default_options.deep_merge(connection_options)) do |builder|
24
+ builder.use Oahu::Request::Auth, credentials
25
+ builder.use FaradayMiddleware::Caching, cache_store unless cache_store.nil?
26
+ builder.use FaradayMiddleware::ParseJson
27
+ builder.adapter(adapter)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ class Hash
2
+
3
+ # Merges self with another hash, recursively
4
+ #
5
+ # @param hash [Hash] The hash to merge
6
+ # @return [Hash]
7
+ def deep_merge(hash)
8
+ target = self.dup
9
+ hash.keys.each do |key|
10
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
11
+ target[key] = target[key].deep_merge(hash[key])
12
+ next
13
+ end
14
+ target[key] = hash[key]
15
+ end
16
+ target
17
+ end
18
+
19
+ end
@@ -0,0 +1,42 @@
1
+ module Oahu
2
+ # Defines HTTP request methods
3
+ module Request
4
+ # Perform an HTTP DELETE request
5
+ def delete(path, params={}, options={})
6
+ request(:delete, api_path(path), params, options)
7
+ end
8
+
9
+ # Perform an HTTP GET request
10
+ def get(path, params={}, options={})
11
+ request(:get, api_path(path), params, options)
12
+ end
13
+
14
+ # Perform an HTTP POST request
15
+ def post(path, params={}, options={})
16
+ request(:post, api_path(path), params, options)
17
+ end
18
+
19
+ private
20
+
21
+ def api_path path
22
+ "/api/v1/clients/#{credentials[:client_id]}/#{path.gsub(/^\//, '')}"
23
+ end
24
+
25
+ # Perform an HTTP request
26
+ def request(method, path, params, options)
27
+ response = connection(options).run_request(method, nil, nil, nil) do |request|
28
+ # request.options[:phoenix] = true if options[:phoenix]
29
+ request.options[:raw] = true if options[:raw]
30
+ case method.to_sym
31
+ when :delete, :get
32
+ request.url(path, params)
33
+ when :post
34
+ request.path = path
35
+ request.body = params unless params.empty?
36
+ end
37
+ end
38
+ options[:raw] ? response : response.body
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ require 'digest/md5'
2
+
3
+ module Oahu
4
+ module Request
5
+ class Auth < Faraday::Middleware
6
+
7
+ def call(env)
8
+ sig_time = Time.now.to_i
9
+ signature = Digest::MD5.hexdigest [sig_time, @credentials[:client_id], @credentials[:consumer_secret]].join("-")
10
+ env[:request_headers]['Oahu-Consumer-Id'] = @credentials[:consumer_id]
11
+ env[:request_headers]['Oahu-Consumer-Sig'] = [sig_time, @credentials[:client_id], signature].join("|")
12
+ @app.call(env)
13
+ end
14
+
15
+ def initialize(app, credentials)
16
+ @app, @credentials = app, credentials
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Oahu
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/oahu/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Stephane Bellity", "Oahu"]
6
+ gem.email = ["sbellity@gmail.com"]
7
+ gem.description = %q{Oahu API Ruby Client}
8
+ gem.summary = %q{Oahu API Ruby Client}
9
+ gem.homepage = "http://oahu.fr"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "oahu"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Oahu::VERSION
17
+
18
+ # Dependencies
19
+ gem.add_dependency 'faraday', '~> 0.7'
20
+ gem.add_dependency 'faraday_middleware', '~> 0.7'
21
+ gem.add_dependency 'multi_json', '~> 1.0'
22
+
23
+ # Development Dependencies
24
+ gem.add_development_dependency 'activesupport', ['>= 2.3.9', '< 4']
25
+ gem.add_development_dependency 'redis-activesupport', '~> 3.2.3'
26
+
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oahu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephane Bellity
9
+ - Oahu
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faraday
17
+ requirement: &70327220899760 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '0.7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70327220899760
26
+ - !ruby/object:Gem::Dependency
27
+ name: faraday_middleware
28
+ requirement: &70327220899260 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70327220899260
37
+ - !ruby/object:Gem::Dependency
38
+ name: multi_json
39
+ requirement: &70327220898780 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '1.0'
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70327220898780
48
+ - !ruby/object:Gem::Dependency
49
+ name: activesupport
50
+ requirement: &70327220914480 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.9
56
+ - - <
57
+ - !ruby/object:Gem::Version
58
+ version: '4'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: *70327220914480
62
+ - !ruby/object:Gem::Dependency
63
+ name: redis-activesupport
64
+ requirement: &70327220911800 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: *70327220911800
73
+ description: Oahu API Ruby Client
74
+ email:
75
+ - sbellity@gmail.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - .gitignore
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - lib/oahu.rb
86
+ - lib/oahu/client.rb
87
+ - lib/oahu/config.rb
88
+ - lib/oahu/connection.rb
89
+ - lib/oahu/core_ext/hash.rb
90
+ - lib/oahu/request.rb
91
+ - lib/oahu/request/auth.rb
92
+ - lib/oahu/version.rb
93
+ - oahu.gemspec
94
+ homepage: http://oahu.fr
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.17
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Oahu API Ruby Client
118
+ test_files: []