cyby 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44e969a7816e2e9fced715ca9e2f8cc58733892b
4
+ data.tar.gz: 55a82382ec95a782a5fee67ad5a6c4d8f8006e50
5
+ SHA512:
6
+ metadata.gz: 9d028e510dc4d51b5daf0c9ffac58d8efdee20fe7deb2d8ec8c6ed786c01617383db753c3a22afe7e31b97b99fe0617369e30fb4874d9d1bb82c56bf7b035dec
7
+ data.tar.gz: 6bd207b96013670275d48d5b80eb9caf31e7c9c3d98a33d431df39c29d4559752a96b56fbbb0a50f8f80c9fed01f9aff578dac66945a0e4de6f31042b806b1fd
@@ -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,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cyby.gemspec
4
+ gemspec
5
+
6
+ gem 'httparty', '~> 0.13.1'
7
+ gem 'activesupport', '~> 4.0'
8
+
9
+ group :development do
10
+ gem 'pry'
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Spiber Inc.
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.
@@ -0,0 +1,58 @@
1
+ ## Cyby
2
+
3
+ Simple Cybozu REST API wrapper
4
+
5
+ ### Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cyby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cyby
18
+
19
+ And then create '.cyby.yml' file in your home dirctory.
20
+
21
+ subdomain: <your subdomain>
22
+ login: <your login>
23
+ password: <your password>
24
+
25
+ ### Usage
26
+
27
+ Require in ruby script.
28
+
29
+ require 'cyby'
30
+
31
+ # Create App object.
32
+ app = Cyby::Kintone::App.new(1) # kintone app id
33
+
34
+ # Filter the records by the various ways.
35
+ record = app.where("id = 1").first
36
+ records = app.all
37
+ app.where("id >= ?", 10).each { |record| puts record.id }
38
+ app.where("name like ?", "Bob").each { |record| puts record.name }
39
+ app.where("create_at < ?", Time.new(2015, 1, 1)).each { |record| puts record.create_at }
40
+ app.where("dropdown in ?", ["a", "b", "c"]).each{ |record| puts record.dropdown }
41
+ app.where("id >= ? and id <= ?", 10, 20).each { |record| puts record.id }
42
+ app.where("id >= ?", 10).and(id <= ?", 20).each { |record| puts record.id }
43
+ app.where("id <= ?", 10).or(id >= ?", 20).each { |record| puts record.id }
44
+
45
+ # Sort the records.
46
+ app.asc("id").each { |record| record.id }
47
+ app.desc("id").each { |record| record.id }
48
+ app.asc("name").desc("id").each { |record| record.id }
49
+
50
+ # Select the fields.
51
+ app.select("id", "name", "create_at").map { |record| [record.id, record.name, record.create_at] }
52
+
53
+ # Use method chain
54
+ app.select("id", "name").where("name like ?, "Bob").asc("id").each { |record| puts record.name }
55
+
56
+ ### Author
57
+
58
+ Hiroyuki Sato <hiroyuki_sato@spiber.jp>
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cyby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cyby"
8
+ spec.version = Cyby::VERSION
9
+ spec.authors = ["Hiroyuki Sato"]
10
+ spec.email = ["hiroyuki_sato@spiber.jp"]
11
+ spec.description = %q(Cybozu API wrapper)
12
+ spec.summary = %q(Cybozu API wrapper)
13
+ spec.homepage = "https://github.com/hiroponz/cyby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($RS)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "httparty", "~> 0.13.1"
25
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
26
+ end
@@ -0,0 +1,14 @@
1
+ require "httparty"
2
+ require "base64"
3
+ require "json"
4
+ require "csv"
5
+ require "active_support/core_ext/string"
6
+
7
+ require "cyby/version"
8
+ require "cyby/response"
9
+ require "cyby/user_api"
10
+ require "cyby/kintone"
11
+
12
+ module Cyby
13
+ # Your code goes here...
14
+ end
@@ -0,0 +1,10 @@
1
+ require "cyby/kintone/app"
2
+ require "cyby/kintone/record"
3
+ require "cyby/kintone/rest_api"
4
+ require "cyby/kintone/query"
5
+ require "cyby/kintone/relation"
6
+
7
+ module Cyby
8
+ module Kintone
9
+ end
10
+ end
@@ -0,0 +1,71 @@
1
+ module Cyby
2
+ module Kintone
3
+ class App
4
+ attr_reader :last_response
5
+ LIMIT = 100
6
+
7
+ def initialize(id)
8
+ @api = RestApi.new(id)
9
+ end
10
+
11
+ def find(params)
12
+ result = []
13
+ page = 0
14
+ begin
15
+ records = find_per_page(params, page)
16
+ result.concat(records)
17
+ page += 1
18
+ end while records.count == LIMIT
19
+ result
20
+ end
21
+
22
+ def find_per_page(params, page)
23
+ params_per_page = params.dup
24
+ queries = [params[:query]]
25
+ if page > 0
26
+ queries << "offset #{LIMIT * page}"
27
+ end
28
+ params_per_page[:query] = queries.join(" ")
29
+ @last_response = @api.get('/records.json', params_per_page)
30
+ unless @last_response.code == 200
31
+ fail @last_response["message"]
32
+ end
33
+ @last_response['records'].map do |record|
34
+ Record.new(record)
35
+ end
36
+ end
37
+
38
+ def relation
39
+ Relation.new(self)
40
+ end
41
+
42
+ def all
43
+ relation.all
44
+ end
45
+
46
+ def where(cond, *params)
47
+ relation.where(cond, *params)
48
+ end
49
+
50
+ def asc(field)
51
+ relation.asc(field)
52
+ end
53
+
54
+ def desc(field)
55
+ relation.desc(field)
56
+ end
57
+
58
+ def select(*fields)
59
+ relation.select(*fields)
60
+ end
61
+
62
+ def id
63
+ @api.app
64
+ end
65
+
66
+ def inspect
67
+ { id: id }.inspect
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ require "cyby/kintone/query/where"
2
+ require "cyby/kintone/query/where_and"
3
+ require "cyby/kintone/query/where_or"
4
+
5
+ module Cyby
6
+ module Kintone
7
+ module Query
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,43 @@
1
+ module Cyby
2
+ module Kintone
3
+ module Query
4
+ class Where
5
+ def initialize(cond = "", *params)
6
+ @cond = cond
7
+ @params = params
8
+ end
9
+
10
+ def to_query
11
+ if @params.empty?
12
+ @cond
13
+ else
14
+ conds = @cond.split("?")
15
+ unless conds.count == @params.count
16
+ fail "Condition params count mismatch!"
17
+ end
18
+ i = 0
19
+ cond = ""
20
+ while i < conds.count
21
+ cond += conds[i] + param_to_s(@params[i])
22
+ i += 1
23
+ end
24
+ cond
25
+ end
26
+ end
27
+
28
+ def param_to_s(param)
29
+ case param
30
+ when String, Date, DateTime
31
+ '"' + param.to_s + '"'
32
+ when Time
33
+ '"' + param.strftime("%Y-%m-%dT%H:%M%:z") + '"'
34
+ when Array
35
+ '("' + param.join('","') + '")'
36
+ else
37
+ param.to_s
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ module Cyby
2
+ module Kintone
3
+ module Query
4
+ class WhereAnd
5
+ def initialize(lhs, rhs)
6
+ @lhs = lhs
7
+ @rhs = rhs
8
+ end
9
+
10
+ def to_query
11
+ if @lhs.to_query.blank?
12
+ @rhs.to_query
13
+ elsif @rhs.to_query.blank?
14
+ @lhs.to_query
15
+ else
16
+ "(#{@lhs.to_query} and #{@rhs.to_query})"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module Cyby
2
+ module Kintone
3
+ module Query
4
+ class WhereOr
5
+ def initialize(lhs, rhs)
6
+ @lhs = lhs
7
+ @rhs = rhs
8
+ end
9
+
10
+ def to_query
11
+ if @lhs.to_query.blank?
12
+ @rhs.to_query
13
+ elsif @rhs.to_query.blank?
14
+ @lhs.to_query
15
+ else
16
+ "(#{@lhs.to_query} or #{@rhs.to_query})"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,45 @@
1
+ module Cyby
2
+ module Kintone
3
+ class Record
4
+ def initialize(raw)
5
+ @raw = raw
6
+ end
7
+
8
+ def [](key)
9
+ if @raw.key?(key)
10
+ convert(@raw[key])
11
+ else
12
+ fail "'#{key}' dose'nt exist"
13
+ end
14
+ end
15
+
16
+ def method_missing(method_name, *args)
17
+ self.[](method_name.to_s.camelize(:lower))
18
+ end
19
+
20
+ def inspect
21
+ hash = {}
22
+ @raw.each do |key, value|
23
+ hash[key] = value["value"]
24
+ end
25
+ hash.inspect
26
+ end
27
+
28
+ private
29
+ def convert(args)
30
+ type = args['type']
31
+ value = args['value']
32
+ case type
33
+ when 'CALC'
34
+ value.to_f
35
+ when 'NUMBER'
36
+ value.to_f
37
+ when 'RECORD_NUMBER'
38
+ value.to_i
39
+ else
40
+ value
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,77 @@
1
+ module Cyby
2
+ module Kintone
3
+ class Relation
4
+ include Enumerable
5
+
6
+ alias_method :all, :to_a
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ @where = Query::Where.new
11
+ @order_by = []
12
+ end
13
+
14
+ def each
15
+ args = {}
16
+ args[:query] = to_query
17
+ args[:fields] = @fields if @fields
18
+ records = @app.find(args)
19
+ records.map do |record|
20
+ yield record
21
+ end
22
+ end
23
+
24
+ def where(cond, *params)
25
+ @where = Query::Where.new(cond, *params)
26
+ self
27
+ end
28
+
29
+ def and(cond, *params)
30
+ case cond
31
+ when String
32
+ @where = Query::WhereAnd.new(@where, Query::Where.new(cond, *params))
33
+ else
34
+ @where = Query::WhereAnd.new(@where, cond)
35
+ end
36
+ self
37
+ end
38
+
39
+ def or(cond, *params)
40
+ case cond
41
+ when String
42
+ @where = Query::WhereOr.new(@where, Query::Where.new(cond, *params))
43
+ else
44
+ @where = Query::WhereOr.new(@where, cond)
45
+ end
46
+ self
47
+ end
48
+
49
+ def asc(field)
50
+ @order_by << "#{field} asc"
51
+ self
52
+ end
53
+
54
+ def desc(field)
55
+ @order_by << "#{field} desc"
56
+ self
57
+ end
58
+
59
+ def select(*fields)
60
+ @fields = fields
61
+ self
62
+ end
63
+
64
+ def to_query
65
+ query = @where.to_query
66
+ if @order_by.any?
67
+ query += " order by #{@order_by.join(',')}"
68
+ end
69
+ query
70
+ end
71
+
72
+ def inspect
73
+ { app: @app.id, query: to_query, select: @fields }.inspect
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,46 @@
1
+ module Cyby
2
+ module Kintone
3
+ class RestApi
4
+ attr_reader :app
5
+
6
+ AUTH = 'X-Cybozu-Authorization'
7
+
8
+ include HTTParty
9
+
10
+ def initialize(app)
11
+ config = YAML.load_file("#{ENV['HOME']}/.cyby.yml")
12
+ self.class.base_uri "https://#{config['subdomain']}.cybozu.com/k/v1"
13
+ @auth = Base64.encode64("#{config['login']}:#{config['password']}").chomp
14
+ @app = app
15
+ end
16
+
17
+ def headers
18
+ { AUTH => @auth, 'Content-Type' => 'application/json' }
19
+ end
20
+
21
+ def get(path, body = {})
22
+ body.merge!(app: @app)
23
+ options = { headers: headers, body: body.to_json }
24
+ self.class.get(path, options)
25
+ end
26
+
27
+ def post(path, body = {})
28
+ body.merge!(app: @app)
29
+ options = { headers: headers, body: body.to_json }
30
+ self.class.post(path, options)
31
+ end
32
+
33
+ def put(path, body = {})
34
+ body.merge!(app: @app)
35
+ options = { headers: headers, body: body.to_json }
36
+ self.class.put(path, options)
37
+ end
38
+
39
+ def delete(path, body = {})
40
+ body.merge!(app: @app)
41
+ options = { headers: headers, body: body.to_json }
42
+ self.class.delete(path, options)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ module HTTParty
2
+ class Response
3
+ def body
4
+ @body.force_encoding("UTF-8")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ module Cyby
2
+ class UserApi
3
+ AUTH = 'X-Cybozu-Authorization'
4
+
5
+ include HTTParty
6
+
7
+ def initialize
8
+ config = YAML.load_file("#{ENV['HOME']}/.cyby.yml")
9
+ self.class.base_uri "https://#{config['subdomain']}.cybozu.com/v1/csv"
10
+ @auth = Base64.encode64("#{config['login']}:#{config['password']}").chomp
11
+ end
12
+
13
+ def get(path)
14
+ options = { headers: { AUTH => @auth } }
15
+ self.class.get(path, options)
16
+ end
17
+
18
+ def user
19
+ get("/user.csv")
20
+ end
21
+
22
+ def organization
23
+ get("/organization.csv")
24
+ end
25
+
26
+ def title
27
+ get("/title.csv")
28
+ end
29
+
30
+ def user_organizations
31
+ get("/userOrganizations.csv")
32
+ end
33
+
34
+ def group
35
+ get("/group.csv")
36
+ end
37
+
38
+ def user_groups
39
+ get("/userGroups.csv")
40
+ end
41
+
42
+ def user_services
43
+ get("/userServices.csv")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Cyby
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cyby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Sato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description: Cybozu API wrapper
70
+ email:
71
+ - hiroyuki_sato@spiber.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - cyby.gemspec
83
+ - lib/cyby.rb
84
+ - lib/cyby/kintone.rb
85
+ - lib/cyby/kintone/app.rb
86
+ - lib/cyby/kintone/query.rb
87
+ - lib/cyby/kintone/query/where.rb
88
+ - lib/cyby/kintone/query/where_and.rb
89
+ - lib/cyby/kintone/query/where_or.rb
90
+ - lib/cyby/kintone/record.rb
91
+ - lib/cyby/kintone/relation.rb
92
+ - lib/cyby/kintone/rest_api.rb
93
+ - lib/cyby/response.rb
94
+ - lib/cyby/user_api.rb
95
+ - lib/cyby/version.rb
96
+ homepage: https://github.com/hiroponz/cyby
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.2.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Cybozu API wrapper
120
+ test_files: []