orchestrate.io 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +4 -0
  6. data/Guardfile +9 -0
  7. data/LICENSE +20 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +108 -0
  10. data/Rakefile +5 -0
  11. data/bin/orchestrate.io +6 -0
  12. data/lib/orchestrate.io/client.rb +58 -0
  13. data/lib/orchestrate.io/configuration.rb +43 -0
  14. data/lib/orchestrate.io/core_ext/string.rb +19 -0
  15. data/lib/orchestrate.io/core_ext.rb +5 -0
  16. data/lib/orchestrate.io/events.rb +47 -0
  17. data/lib/orchestrate.io/graph.rb +52 -0
  18. data/lib/orchestrate.io/helper.rb +20 -0
  19. data/lib/orchestrate.io/key_value.rb +46 -0
  20. data/lib/orchestrate.io/logging.rb +34 -0
  21. data/lib/orchestrate.io/request.rb +51 -0
  22. data/lib/orchestrate.io/search.rb +46 -0
  23. data/lib/orchestrate.io/version.rb +5 -0
  24. data/lib/orchestrate.io.rb +21 -0
  25. data/orchestrate.io.gemspec +33 -0
  26. data/spec/orchestrate.io/client_spec.rb +111 -0
  27. data/spec/orchestrate.io/events_spec.rb +51 -0
  28. data/spec/orchestrate.io/graph_spec.rb +51 -0
  29. data/spec/orchestrate.io/key_value_spec.rb +67 -0
  30. data/spec/orchestrate.io/request_spec.rb +54 -0
  31. data/spec/orchestrate.io/search_spec.rb +25 -0
  32. data/spec/orchestrate.io_spec.rb +47 -0
  33. data/spec/spec_helper.rb +61 -0
  34. data/spec/support/fixtures/get_events_by_key.json +12 -0
  35. data/spec/support/fixtures/get_graph.json +30 -0
  36. data/spec/support/fixtures/get_value_by_key.json +18 -0
  37. data/spec/support/fixtures/get_values_by_key.json +18 -0
  38. data/spec/support/fixtures/search_collection.json +32 -0
  39. data/spec/support/pseudo_orchestrate.io.rb +61 -0
  40. metadata +251 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f161128ca77ed5865e374e9706709583f4a7b363
4
+ data.tar.gz: 2c396a67015c8778d56bdad2f017774c4a55ae4e
5
+ SHA512:
6
+ metadata.gz: 594f3cd3c7f64758d4b4be901a0fc41da5022eb0543a2481844a0c22638d6b0250fd74c6a14b639568d62b972086baa9c008fab7bb6a8a5def5b3616f47184e6
7
+ data.tar.gz: 12231b8ddb65055fafb08fad97872b754b69d4cc889be8b475ff3ae9d39d0cea63318de3217ae99c01b05e5ce6d650ba4dd6f67f61ab80b39701932ea2d1b8bf
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ coverage
12
+ InstalledFiles
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ .DS_Store
21
+ application.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+
6
+ branches:
7
+ only:
8
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in orchestrate.io.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 azukiwasher
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 azukiwasher
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,108 @@
1
+ # ruby-orchestrate.io
2
+
3
+ A Ruby interface to the [Orchestrate.io](https://orchestrate.io/) API
4
+
5
+ [![Build
6
+ Status](https://travis-ci.org/azukiwasher/ruby-orchestrate.io.png?branch=master)](https://travis-ci.org/azukiwasher/ruby-orchestrate.io)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```
13
+ gem 'orchestrate.io'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```
25
+ $ gem install orchestrate.io
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```
31
+ require 'orchestrate.io'
32
+ require 'json'
33
+
34
+ @io = OrchestrateIo.new(api_key: 'abc')
35
+ @json_data = JSON.dump({a: 1})
36
+ @search_query = "hello dolly"
37
+ ```
38
+
39
+ #### Key/Value
40
+
41
+ ```
42
+ @io.key_value :put do
43
+ collection "foo"
44
+ key "bar"
45
+  timestamp 1384224213
46
+ data @json_data
47
+ end.perform
48
+
49
+ @io.key_value :get do
50
+ collection "foo"
51
+ key "bar"
52
+ end.perform
53
+ ```
54
+
55
+ #### Search
56
+
57
+ ```
58
+ @io.search do
59
+ collection "foo"
60
+ query @query_string
61
+ end.perform
62
+ ```
63
+
64
+ #### Events
65
+
66
+ ```
67
+ @io.events :put do
68
+ collection "foo"
69
+ key "bar"
70
+ type "log"
71
+  timestamp 1384224213
72
+ data @json_data
73
+ end.perform
74
+
75
+ @io.events :get do
76
+ collection "foo"
77
+ key "bar"
78
+ type "log"
79
+  start 1384224210
80
+  end 1384224213
81
+ end.perform
82
+ ```
83
+
84
+ #### Graph
85
+
86
+ ```
87
+ @io.graph :put do
88
+ collection "foo"
89
+ key "bar1"
90
+ relation "friends"
91
+ to_collection "hoge"
92
+ to_key "bar2"
93
+ end.perform
94
+
95
+ @io.graph :get do
96
+ collection "foo"
97
+ key "bar1"
98
+ relation "friends"
99
+ end.perform
100
+ ```
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path("../../lib", __FILE__)
4
+ require 'orchestrate.io'
5
+
6
+ # Write CLI ...
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ require 'httparty'
3
+ require 'orchestrate.io/helper'
4
+ require 'orchestrate.io/configuration'
5
+ require 'orchestrate.io/key_value'
6
+ require 'orchestrate.io/search'
7
+ require 'orchestrate.io/events'
8
+ require 'orchestrate.io/graph'
9
+ require 'orchestrate.io/request'
10
+
11
+ module OrchestrateIo
12
+ class Client
13
+ include HTTParty
14
+ include Helper
15
+
16
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
17
+
18
+ def initialize(attrs={})
19
+ attrs = OrchestrateIo.options.merge(attrs)
20
+
21
+ Configuration::VALID_OPTIONS_KEYS.each do |k|
22
+ instance_variable_set("@#{k}".to_sym, attrs[k])
23
+ end
24
+
25
+ initialize_client
26
+ logger.level = verbose ? ::Logger::DEBUG : ::Logger::INFO
27
+ end
28
+
29
+ ##
30
+ # Updates HTTParty default settings
31
+ #
32
+ def initialize_client
33
+ self.class.format :json
34
+ self.class.base_uri endpoint
35
+ self.class.headers request_headers
36
+ end
37
+
38
+ ##
39
+ # Entry point to HTTP request
40
+ # Set the username of basic auth to the API Key attribute.
41
+ #
42
+ def request(http_method, uri, options={})
43
+ response = self.class.__send__(http_method, uri, options.merge(basic_auth))
44
+ # Add some logger.debug here ...
45
+ response
46
+ end
47
+
48
+ def method_missing(name, *args, &block)
49
+ OrchestrateIo.const_get(name.to_s.camelize!).new(self, args.first, &block)
50
+ end
51
+
52
+ protected
53
+
54
+ def basic_auth
55
+ { basic_auth: { username: api_key, password: "" } }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ require 'orchestrate.io/version'
3
+
4
+ module OrchestrateIo
5
+ module Configuration
6
+
7
+ DEFAULT_REQUEST_HEADERS = {
8
+ "User-Agent" => "Orchestrate.io Ruby Gem #{OrchestrateIo::Version}",
9
+ "Content-Type" => "application/json; charset=utf-8",
10
+ "Accept-Language" => "ja,en"
11
+ }
12
+
13
+ DEFAULT_ENDPOINT = "https://api.orchestrate.io"
14
+
15
+ VALID_OPTIONS_KEYS = [
16
+ :request_headers,
17
+ :endpoint,
18
+ :api_key,
19
+ :version,
20
+ :verbose
21
+ ]
22
+
23
+ attr_accessor *VALID_OPTIONS_KEYS
24
+
25
+ def self.extended(base)
26
+ base.reset
27
+ end
28
+
29
+ def options
30
+ options = {}
31
+ VALID_OPTIONS_KEYS.each {|k| options[k] = send(k)}
32
+ options
33
+ end
34
+
35
+ def reset
36
+ self.request_headers = DEFAULT_REQUEST_HEADERS
37
+ self.endpoint = DEFAULT_ENDPOINT
38
+ self.api_key = ""
39
+ self.version = "v0"
40
+ self.verbose = false
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ class String
4
+ def camelize
5
+ dup.tap &:camelize!
6
+ end
7
+
8
+ def camelize!
9
+ self.replace(self.split("_").each {|s| s.capitalize! }.join(""))
10
+ end
11
+
12
+ def underscore
13
+ dup.tap &:underscore!
14
+ end
15
+
16
+ def underscore!
17
+ self.replace(self.scan(/[A-Z][a-z]*/).join("_").downcase)
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].each do |path|
4
+ require path
5
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ class Events
5
+
6
+ attr_reader :request
7
+
8
+ # == Usage
9
+ #
10
+ # io = OrchestrateIo.new(api_key: "abc")
11
+ # request = io.events :get do
12
+ # collection 'films'
13
+ # key 'the_godfather'
14
+ # type 'comments'
15
+ # end
16
+ #
17
+ # request.perform
18
+ # => HTTParty::Response
19
+ #
20
+ def initialize(client, method, &block)
21
+ args = {
22
+ client: client,
23
+ http_method: method,
24
+ uri: uri,
25
+ options: options
26
+ }
27
+
28
+ @request = OrchestrateIo::Request.new(args, &block)
29
+ end
30
+
31
+ def perform
32
+ request.perform
33
+ end
34
+
35
+ protected
36
+
37
+ def uri
38
+ "/:version/:collection/:key/events/:type"
39
+ end
40
+
41
+ def options
42
+ options = {}
43
+ options[:body] = :data
44
+ options
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ class Graph
5
+
6
+ attr_reader :request, :http_method
7
+
8
+ # == Usage
9
+ #
10
+ # io = OrchestrateIo.new(api_key: "abc")
11
+ # request = io.graph :get do
12
+ # collection 'films'
13
+ # key 'the_godfather'
14
+ # relation 'sequel'
15
+ # end
16
+ #
17
+ # request.perform
18
+ # => HTTParty::Response
19
+ #
20
+ def initialize(client, method, &block)
21
+ @http_method = method
22
+ args = {
23
+ client: client,
24
+ http_method: method,
25
+ uri: uri,
26
+ options: options
27
+ }
28
+
29
+ @request = OrchestrateIo::Request.new(args, &block)
30
+ end
31
+
32
+ def perform
33
+ request.perform
34
+ end
35
+
36
+ protected
37
+
38
+ def uri
39
+ case http_method
40
+ when :get
41
+ "/:version/:collection/:key/relations/:relation"
42
+ when :put
43
+ "/:version/:collection/:key/relations/:relation/:to_collection/:to_key"
44
+ end
45
+ end
46
+
47
+ def options
48
+ options = {}
49
+ options
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ module Helper
5
+
6
+ ##
7
+ # Returns current time in millisecond
8
+ #
9
+ def current_time
10
+ Time.now.instance_eval { self.to_i * 1000 + (usec/1000) }
11
+ end
12
+
13
+ ##
14
+ # Alias for ::Logging.logger
15
+ #
16
+ def logger
17
+ OrchestrateIo.logger
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ class KeyValue
5
+
6
+ attr_reader :request
7
+
8
+ # == Usage
9
+ #
10
+ # io = OrchestrateIo.new(api_key: "abc")
11
+ # request = io.key_value :get do
12
+ # collection 'films'
13
+ # key 'kurosawa'
14
+ # end
15
+ #
16
+ # request.perform
17
+ # => HTTParty::Response
18
+ #
19
+ def initialize(client, method, &block)
20
+ args = {
21
+ client: client,
22
+ http_method: method,
23
+ uri: uri,
24
+ options: options
25
+ }
26
+
27
+ @request = OrchestrateIo::Request.new(args, &block)
28
+ end
29
+
30
+ def perform
31
+ request.perform
32
+ end
33
+
34
+ protected
35
+
36
+ def uri
37
+ "/:version/:collection/:key"
38
+ end
39
+
40
+ def options
41
+ options = {}
42
+ options[:body] = :data
43
+ options
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require 'time'
3
+ require 'logger'
4
+
5
+ module OrchestrateIo
6
+ module Logging
7
+ class Pretty < Logger::Formatter
8
+ def call(severity, time, program_name, message)
9
+ "#{time.utc.iso8601} #{Process.pid} #{severity}: #{message}\n"
10
+ end
11
+ end
12
+
13
+ class << self
14
+ def initialize_logger(log_target = STDOUT)
15
+ @logger = Logger.new(log_target)
16
+ @logger.level = Logger::INFO
17
+ @logger.formatter = Pretty.new
18
+ @logger
19
+ end
20
+
21
+ def logger
22
+ @logger || initialize_logger
23
+ end
24
+
25
+ def logger=(log)
26
+ @logger = (log ? log : Logger.new('/dev/null'))
27
+ end
28
+ end
29
+
30
+ def logger
31
+ OrchestrateIo::Logging.logger
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ class Request
5
+ attr_reader :client, :version, :http_method
6
+
7
+ def initialize(args, &block)
8
+ @client = args[:client]
9
+ @http_method = args[:http_method]
10
+ @uri = args[:uri]
11
+ @options = args[:options]
12
+ @version = client.version
13
+
14
+ instance_eval &block if block_given?
15
+ end
16
+
17
+ def perform
18
+ client.request(http_method, uri, parse_options(@options))
19
+ end
20
+
21
+ def uri
22
+ @uri.split(/\//).map {|str|
23
+ str =~ /^:/ ? instance_variable_get("@" + str.gsub!(/:/,'')) : str
24
+ }.join("/")
25
+ end
26
+
27
+ def parse_options(options)
28
+ options.each {|k,v|
29
+ if v.is_a? Hash
30
+ parse_options(v)
31
+ else
32
+ if value = instance_variable_get("@#{v}")
33
+ options[k] = value
34
+ else
35
+ options.delete(k)
36
+ end
37
+ end
38
+ }
39
+ end
40
+
41
+ def method_missing(method, *args, &block)
42
+ args = args.first
43
+ self.class.class_eval do
44
+ define_method method do |value|
45
+ instance_variable_set("@#{method}", value)
46
+ end
47
+ end
48
+ self.__send__(method, args)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ class Search
5
+
6
+ attr_reader :request
7
+
8
+ # == Usage
9
+ #
10
+ # io = OrchestrateIo.new(api_key: "abc")
11
+ # request = io.search :get do
12
+ # collection 'films'
13
+ # query 'Genre:crime'
14
+ # end
15
+ #
16
+ # request.perform
17
+ # => HTTParty::Response
18
+ #
19
+ def initialize(client, method, &block)
20
+ args = {
21
+ client: client,
22
+ http_method: method,
23
+ uri: uri,
24
+ options: options
25
+ }
26
+
27
+ @request = OrchestrateIo::Request.new(args, &block)
28
+ end
29
+
30
+ def perform
31
+ request.perform
32
+ end
33
+
34
+ protected
35
+
36
+ def uri
37
+ "/:version/:collection"
38
+ end
39
+
40
+ def options
41
+ options = {}
42
+ options[:query] = { query: :query }
43
+ options
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module OrchestrateIo
4
+ Version = VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'orchestrate.io/client'
3
+ require 'orchestrate.io/configuration'
4
+ require 'orchestrate.io/logging'
5
+ require 'orchestrate.io/core_ext'
6
+
7
+ module OrchestrateIo
8
+ extend Configuration
9
+
10
+ def self.new(attrs={})
11
+ OrchestrateIo::Client.new(attrs)
12
+ end
13
+
14
+ def self.logger
15
+ OrchestrateIo::Logging.logger
16
+ end
17
+
18
+ def self.logger=(log)
19
+ OrchestrateIo::Logging.logger = log
20
+ end
21
+ end