cliskip2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cliskip2.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cliskip2 (0.0.1)
5
+ faraday_middleware (~> 0.8.7)
6
+ json (~> 1.7.3)
7
+ oauth (~> 0.4)
8
+ simple_oauth (~> 0.1.8)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ diff-lcs (1.1.3)
14
+ faraday (0.8.0)
15
+ multipart-post (~> 1.1)
16
+ faraday_middleware (0.8.7)
17
+ faraday (>= 0.7.4, < 0.9)
18
+ json (1.7.3)
19
+ multipart-post (1.1.5)
20
+ oauth (0.4.6)
21
+ rake (0.9.2.2)
22
+ rake-compiler (0.8.1)
23
+ rake
24
+ rspec (2.10.0)
25
+ rspec-core (~> 2.10.0)
26
+ rspec-expectations (~> 2.10.0)
27
+ rspec-mocks (~> 2.10.0)
28
+ rspec-core (2.10.1)
29
+ rspec-expectations (2.10.0)
30
+ diff-lcs (~> 1.1.3)
31
+ rspec-mocks (2.10.1)
32
+ simple_oauth (0.1.8)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ cliskip2!
39
+ rake
40
+ rake-compiler
41
+ rspec (~> 2)
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ cliskip2
2
+ ========
3
+
4
+ A Ruby wrapper for the SKIP2 REST APIs
5
+
6
+ ## Installation
7
+ gem install cliskip2
8
+
9
+ ## Configration
10
+ Cliskip2.configure do |config|
11
+ config.endpoint = 'http://localhost:3000'
12
+ config.consumer_key = YOUR_CONSUMER_KEY
13
+ config.consumer_secret = YOUR_CONSUMER_SECRET
14
+ config.xauth_username = SKIP2_ADMIN_USER_NAME
15
+ config.xauth_password = SKIP2_ADMIN_USER_PASSWORD
16
+ end
17
+
18
+ ## Examples
19
+ ### get the authorized user
20
+ client = Cliskip2::Client.new
21
+ client.current_user
22
+
23
+ ### create new user
24
+ client = Cliskip2::Client.new
25
+ client.post_user :user => {:name => 'hoge', :email => 'hoge@hoge.com'}
26
+
27
+ ### get the user by email
28
+ client = Cliskip2::Client.new
29
+ client.get_user :email => 'hoge@hoge.com'
30
+
31
+ ### update the user by email
32
+ client = Cliskip2::Client.new
33
+ client.put_user :user => {:name => 'foobar', :email => 'hoge@hoge.com'}
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/cliskip2.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cliskip2/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cliskip2"
7
+ s.version = Cliskip2::VERSION
8
+ s.authors = ["Naoki Maeda"]
9
+ s.email = ["maeda.na@gmail.com"]
10
+ s.homepage = "http://wiki.github.com/maedana/cliskip2"
11
+ s.summary = %q{A Ruby wrapper for the SKIP2 REST APIs}
12
+ s.description = %q{A Ruby wrapper for the SKIP2 REST APIs}
13
+
14
+ s.rubyforge_project = "cliskip2"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'faraday_middleware', '~> 0.8.7'
22
+ s.add_dependency 'simple_oauth', '~> 0.1.8'
23
+ s.add_dependency 'json', '~> 1.7.3'
24
+ s.add_dependency 'oauth', '~> 0.4'
25
+ s.add_development_dependency('rake')
26
+ s.add_development_dependency('rake-compiler')
27
+ s.add_development_dependency('rspec', '~> 2')
28
+ end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Cliskip2
3
+ class Base
4
+ attr_accessor :attrs
5
+
6
+ # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
7
+ #
8
+ # @overload self.lazy_attr_reader(attr)
9
+ # @param attr [Symbol]
10
+ # @overload self.lazy_attr_reader(attrs)
11
+ # @param attrs [Array<Symbol>]
12
+ def self.lazy_attr_reader(*attrs)
13
+ attrs.each do |attribute|
14
+ class_eval do
15
+ define_method attribute do
16
+ @attrs[attribute.to_s]
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ # Initializes a new Base object
23
+ #
24
+ # @param attrs [Hash]
25
+ # @return [Twitter::Base]
26
+ def initialize(attrs={})
27
+ @attrs = attrs.dup
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cliskip2/connection'
3
+ require 'cliskip2/request'
4
+ require 'cliskip2/user'
5
+ require 'cliskip2/config'
6
+
7
+ module Cliskip2
8
+ class Client
9
+ include Cliskip2::Connection
10
+ include Cliskip2::Request
11
+
12
+ attr_accessor *Config::VALID_OPTIONS_KEYS
13
+
14
+ # Initializes a new API object
15
+ #
16
+ # @param attrs [Hash]
17
+ # @return [Cliskip2::Client]
18
+ def initialize(attrs={})
19
+ attrs = Cliskip2.options.merge(attrs)
20
+ Config::VALID_OPTIONS_KEYS.each do |key|
21
+ instance_variable_set("@#{key}".to_sym, attrs[key])
22
+ end
23
+ end
24
+
25
+ def current_user
26
+ user_attr = get("/api/get_current_user")
27
+ @current_user ||= Cliskip2::User.new(user_attr['user'])
28
+ end
29
+
30
+ # Create new user by params
31
+ # @return [Cliskip2::User]
32
+ def post_user params
33
+ user_attr = post("/admin/tenants/#{current_user.tenant_id}/users.json", params)
34
+ Cliskip2::User.new(user_attr['user'])
35
+ end
36
+
37
+ # Get the user by params
38
+ # @return [Cliskip2::User]
39
+ def get_user params
40
+ user_attr = get("/admin/tenants/#{current_user.tenant_id}/users/show_by_params.json", params)
41
+ Cliskip2::User.new(user_attr['user'])
42
+ end
43
+
44
+ # Update the user by params
45
+ # @return [Cliskip2::User]
46
+ def put_user params
47
+ user = get_user :email => params[:user][:email]
48
+ user_attr = put("/admin/tenants/#{current_user.tenant_id}/users/#{user.id}.json", params)
49
+ Cliskip2::User.new(user_attr['user'])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,75 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "cliskip2/version"
3
+
4
+ module Cliskip2
5
+ module Config
6
+ # The HTTP connection adapter that will be used to connect if none is set
7
+ DEFAULT_ADAPTER = :net_http
8
+
9
+ # The Faraday connection options if none is set
10
+ DEFAULT_CONNECTION_OPTIONS = {}
11
+
12
+ # The consumer key if none is set
13
+ DEFAULT_CONSUMER_KEY = nil
14
+
15
+ # The consumer secret if none is set
16
+ DEFAULT_CONSUMER_SECRET = nil
17
+
18
+ # The endpoint that will be used to connect if none is set
19
+ DEFAULT_ENDPOINT = nil
20
+
21
+ # The xauth username if none is set
22
+ DEFAULT_XAUTH_USERNAME = nil
23
+
24
+ # The xauth password if none is set
25
+ DEFAULT_XAUTH_PASSWORD = nil
26
+
27
+ # The value sent in the 'User-Agent' header if none is set
28
+ DEFAULT_USER_AGENT = "Cliskip2 Ruby Gem #{Cliskip2::VERSION}"
29
+
30
+ # An array of valid keys in the options hash when configuring a {Twitter::Client}
31
+ VALID_OPTIONS_KEYS = [
32
+ :adapter,
33
+ :connection_options,
34
+ :consumer_key,
35
+ :consumer_secret,
36
+ :endpoint,
37
+ :xauth_username,
38
+ :xauth_password,
39
+ :user_agent
40
+ ]
41
+
42
+ attr_accessor *VALID_OPTIONS_KEYS
43
+
44
+ # When this module is extended, set all configuration options to their default values
45
+ def self.extended(base)
46
+ base.reset
47
+ end
48
+
49
+ # Convenience method to allow configuration options to be set in a block
50
+ def configure
51
+ yield self
52
+ self
53
+ end
54
+
55
+ # Create a hash of options and their values
56
+ def options
57
+ options = {}
58
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
59
+ options
60
+ end
61
+
62
+ # Reset all configuration options to defaults
63
+ def reset
64
+ self.adapter = DEFAULT_ADAPTER
65
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
66
+ self.consumer_key = DEFAULT_CONSUMER_KEY
67
+ self.consumer_secret = DEFAULT_CONSUMER_SECRET
68
+ self.endpoint = DEFAULT_ENDPOINT
69
+ self.xauth_username = DEFAULT_XAUTH_USERNAME
70
+ self.xauth_password = DEFAULT_XAUTH_PASSWORD
71
+ self.user_agent = DEFAULT_USER_AGENT
72
+ self
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'oauth'
5
+ require 'cliskip2/request/oauth'
6
+
7
+ module Cliskip2
8
+ module Connection
9
+ private
10
+
11
+ # Returns a Faraday::Connection object
12
+ #
13
+ # @return [Faraday::Connection]
14
+ def connection options = {}
15
+ connection_options = {
16
+ :headers => {
17
+ :accept => 'application/json',
18
+ :user_agent => user_agent,
19
+ },
20
+ :url => endpoint
21
+ }
22
+ credentials = {
23
+ :consumer_key => consumer.key,
24
+ :consumer_secret => consumer.secret,
25
+ :token => access_token.token,
26
+ :token_secret => access_token.secret
27
+ }
28
+ @connection ||= Faraday.new(connection_options) do |builder|
29
+ builder.response :raise_error
30
+ builder.response :json
31
+ builder.request :url_encoded
32
+ builder.use Cliskip2::Request::Cliskip2OAuth, credentials
33
+ builder.adapter adapter
34
+ end
35
+ end
36
+
37
+ def consumer
38
+ default_options = {
39
+ :site => endpoint,
40
+ :access_token_path => '/api/oauth/access_token'
41
+ }
42
+ @consumer ||= ::OAuth::Consumer.new(consumer_key, consumer_secret, default_options)
43
+ end
44
+
45
+ def access_token
46
+ @access_token ||= consumer.get_access_token(nil, {}, {
47
+ :x_auth_mode => 'client_auth',
48
+ :x_auth_username => xauth_username,
49
+ :x_auth_password => xauth_password
50
+ })
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'faraday'
3
+ require 'simple_oauth'
4
+
5
+ module Cliskip2
6
+ module Request
7
+ class Cliskip2OAuth < FaradayMiddleware::OAuth
8
+ # Override Faraday::Utils#parse_nested_query for fixing base-query-string
9
+ # body部分のパラメタ解析方法がskip2のSPで利用しているoauth(0.4.1)とfaradayのoauthモジュールとで異なってしまい認証がうまくいかないためfaraday側のbody部分解析をoauth(0.4.1)相当に合わせている
10
+ # See Faraday::Utils, FaradayMiddleware::OAuth
11
+ def parse_nested_query(qs)
12
+ params = {}
13
+ (qs || '').split(/[&;] */n).each do |p|
14
+ k, v = p.split('=', 2).map { |s| unescape(s) }
15
+ params[k] = v
16
+ end
17
+ params
18
+ end
19
+ def unescape(s) CGI.unescape s.to_s end
20
+
21
+ # Override Faraday::Utils#include_body_params?
22
+ # oauth(0.4.1)でpostの場合のみbodyをsignature-base-stringに含めているので
23
+ def include_body_params?(env)
24
+ # see RFC 5489, section 3.4.1.3.1 for details
25
+ env[:method] == :post && (!(type = env[:request_headers][CONTENT_TYPE]) or type == TYPE_URLENCODED)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Cliskip2
3
+ # Defines HTTP request methods
4
+ module Request
5
+ # Perform an HTTP DELETE request
6
+ def delete(path, params={}, options={})
7
+ request(:delete, path, params, options)
8
+ end
9
+
10
+ # Perform an HTTP GET request
11
+ def get(path, params={}, options={})
12
+ request(:get, path, params, options)
13
+ end
14
+
15
+ # Perform an HTTP POST request
16
+ def post(path, params={}, options={})
17
+ request(:post, path, params, options)
18
+ end
19
+
20
+ # Perform an HTTP PUT request
21
+ def put(path, params={}, options={})
22
+ request(:put, path, params, options)
23
+ end
24
+
25
+ private
26
+
27
+ # Perform an HTTP request
28
+ def request(method, path, params, options)
29
+ response = connection(options).run_request(method, nil, nil, nil) do |request|
30
+ request.options[:raw] = true if options[:raw]
31
+ case method.to_sym
32
+ when :delete, :get
33
+ request.url(path, params)
34
+ when :post, :put
35
+ request.path = path
36
+ request.body = params unless params.empty?
37
+ end
38
+ end
39
+ options[:raw] ? response : response.body
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cliskip2/base'
3
+
4
+ module Cliskip2
5
+ class User < Cliskip2::Base
6
+ lazy_attr_reader :id, :tenant_id, :email
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Cliskip2
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cliskip2.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "cliskip2/version"
2
+ require 'cliskip2/client'
3
+ require 'cliskip2/config'
4
+
5
+ module Cliskip2
6
+ extend Config
7
+ class << self
8
+ # Alias for Cliskip2::Client.new
9
+ #
10
+ # @return [Cliskip2::Client]
11
+ def new(options={})
12
+ Cliskip2::Client.new(options)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Cliskip2" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'cliskip2'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cliskip2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Naoki Maeda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 49
28
+ segments:
29
+ - 0
30
+ - 8
31
+ - 7
32
+ version: 0.8.7
33
+ type: :runtime
34
+ name: faraday_middleware
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 11
44
+ segments:
45
+ - 0
46
+ - 1
47
+ - 8
48
+ version: 0.1.8
49
+ type: :runtime
50
+ name: simple_oauth
51
+ prerelease: false
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 13
60
+ segments:
61
+ - 1
62
+ - 7
63
+ - 3
64
+ version: 1.7.3
65
+ type: :runtime
66
+ name: json
67
+ prerelease: false
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ - 4
79
+ version: "0.4"
80
+ type: :runtime
81
+ name: oauth
82
+ prerelease: false
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ name: rake
96
+ prerelease: false
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ type: :development
109
+ name: rake-compiler
110
+ prerelease: false
111
+ version_requirements: *id006
112
+ - !ruby/object:Gem::Dependency
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ hash: 7
119
+ segments:
120
+ - 2
121
+ version: "2"
122
+ type: :development
123
+ name: rspec
124
+ prerelease: false
125
+ version_requirements: *id007
126
+ description: A Ruby wrapper for the SKIP2 REST APIs
127
+ email:
128
+ - maeda.na@gmail.com
129
+ executables: []
130
+
131
+ extensions: []
132
+
133
+ extra_rdoc_files: []
134
+
135
+ files:
136
+ - .gitignore
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - README.md
140
+ - Rakefile
141
+ - cliskip2.gemspec
142
+ - lib/cliskip2.rb
143
+ - lib/cliskip2/base.rb
144
+ - lib/cliskip2/client.rb
145
+ - lib/cliskip2/config.rb
146
+ - lib/cliskip2/connection.rb
147
+ - lib/cliskip2/request.rb
148
+ - lib/cliskip2/request/oauth.rb
149
+ - lib/cliskip2/user.rb
150
+ - lib/cliskip2/version.rb
151
+ - spec/cliskip2_spec.rb
152
+ - spec/spec_helper.rb
153
+ has_rdoc: true
154
+ homepage: http://wiki.github.com/maedana/cliskip2
155
+ licenses: []
156
+
157
+ post_install_message:
158
+ rdoc_options: []
159
+
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
169
+ - 0
170
+ version: "0"
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ none: false
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ hash: 3
177
+ segments:
178
+ - 0
179
+ version: "0"
180
+ requirements: []
181
+
182
+ rubyforge_project: cliskip2
183
+ rubygems_version: 1.3.7
184
+ signing_key:
185
+ specification_version: 3
186
+ summary: A Ruby wrapper for the SKIP2 REST APIs
187
+ test_files: []
188
+