orchestrate-api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4939042aeb2ca40acfdd9733ac806a16790c0573
4
+ data.tar.gz: 1f301d012d8d27cb0963c32d0c791d400df29af6
5
+ SHA512:
6
+ metadata.gz: fc2c245de2d11356de25f1f5bb38f43a55aaf12fa7c33e3eb58d4fe228243741c86e5a45e99dd870124dc62ee6de613375d885f2f23892e5d121106cc7e7dc03
7
+ data.tar.gz: fc80afd5dad3b27e43aedd09349f4ba4c144cc15a51413a25f17173878b3b2a4ed263b45a573cd0a1487d6ef95c44d1f642518af3eb923a5e887f482805ca5a2
@@ -0,0 +1,22 @@
1
+ module Orchestrate
2
+
3
+ =begin rdoc
4
+ ==== orchestrate-api
5
+
6
+ Ruby gem <tt>orchestrate-api</tt> provides an
7
+ interface to the <b>Orchestrate.io</b> *API*.
8
+
9
+ The <b>Wrapper[API/Wrapper.html]</b> class is used to setup the client and make HTTP requests.
10
+ =end
11
+
12
+ module API
13
+
14
+ # require "active_support/core_ext"
15
+
16
+ require "orchestrate_api/procedural"
17
+ require "orchestrate_api/wrapper"
18
+ require "orchestrate_api/request"
19
+ require "orchestrate_api/response"
20
+
21
+ end
22
+ end
@@ -0,0 +1,107 @@
1
+ module Orchestrate::API
2
+
3
+ # ==== *Procedural* interface methods (optional).
4
+ #
5
+ # This file serves as useful documentation for the <b>Orchestrate.io REST</b> *API*.
6
+ # Each method illustrates an example for the corresponding HTTP request,
7
+ # and documents the required keys for the send_request argument hash.
8
+ #
9
+ module Procedural
10
+
11
+ # -------------------------------------------------------------------------
12
+ # collection
13
+
14
+ # * required: { collection }
15
+ #
16
+ def list(args)
17
+ send_request :get, args
18
+ end
19
+
20
+ # * required: { collection }
21
+ #
22
+ def search(args)
23
+ send_request :get, args.merge(path: "/?query=#{args[:query].gsub(/\s/, '%20')}")
24
+ end
25
+
26
+ # * required: { collection }
27
+ #
28
+ def delete_collection(args)
29
+ send_request :delete, args.merge(path: "?force=true")
30
+ end
31
+
32
+ # -------------------------------------------------------------------------
33
+ # collection/key
34
+
35
+ # * required: { collection, key }
36
+ #
37
+ def get_key(args)
38
+ send_request :get, args
39
+ end
40
+
41
+ # * required: { collection, key, json }
42
+ #
43
+ def put_key(args)
44
+ send_request :put, args
45
+ end
46
+
47
+ # * required: { collection, key }
48
+ #
49
+ def delete_key(args)
50
+ send_request :delete, args
51
+ end
52
+
53
+ # -------------------------------------------------------------------------
54
+ # collection/key/ref
55
+
56
+ # * required: { collection, key, ref }
57
+ #
58
+ def get_by_ref(args)
59
+ send_request :get, args
60
+ end
61
+
62
+ # * required: { collection, key, json, ref }
63
+ #
64
+ def put_key_if_match(args)
65
+ send_request :put, args
66
+ end
67
+
68
+ # * required: { collection, key, json }
69
+ #
70
+ def put_key_if_none_match(args)
71
+ send_request :put, args.merge(ref: '"*"')
72
+ end
73
+
74
+ # -------------------------------------------------------------------------
75
+ # collection/key/events
76
+
77
+ # * required: { collection, key, event_type }
78
+ # * optional: { timestamp }, where timestamp = { :start => start, :end => end }
79
+ #
80
+ def get_events(args)
81
+ send_request :get, args
82
+ end
83
+
84
+ # * required: { collection, key, event_type, json }
85
+ # * optional: { timestamp }, where timestamp is a scalar value
86
+ #
87
+ def put_event(args)
88
+ send_request :put, args
89
+ end
90
+
91
+ # -------------------------------------------------------------------------
92
+ # collection/key/graph
93
+
94
+ # * required: { collection, key, kind }
95
+ #
96
+ def get_graph(args)
97
+ send_request :get, args
98
+ end
99
+
100
+ # * required: { collection, key, kind, to_collection, to_key }
101
+ #
102
+ def put_graph(args)
103
+ send_request :put, args
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,63 @@
1
+ module Orchestrate::API
2
+
3
+ require 'httparty'
4
+
5
+ # Uses HTTParty to make the HTTP calls,
6
+ # and returns a Response object.
7
+ #
8
+ class Request < Object
9
+
10
+ # The HTTP method - must be one of: [ :get, :put, :delete ].
11
+ attr_accessor :method
12
+
13
+ # The url for the HTTP request.
14
+ attr_accessor :url
15
+
16
+ # The api key for HTTP Basic Authentication over SSL.
17
+ attr_accessor :user
18
+
19
+ # The json document for creating or updating a key.
20
+ attr_accessor :data
21
+
22
+ # The ref value associated with a key.
23
+ attr_accessor :ref
24
+
25
+ # Boolean
26
+ attr_accessor :verbose
27
+
28
+ # Sets the universal attributes from the params; any additional
29
+ # attributes are set from the block.
30
+ #
31
+ def initialize(method, url, user)
32
+ @method, @url, @user = method, url, user
33
+ @data = {}
34
+ yield self if block_given?
35
+ end
36
+
37
+ # Sends the HTTP request and returns a Response object.
38
+ def perform
39
+ puts "\n------- #{method.to_s.upcase} \"#{url}\" ------" if verbose?
40
+ Response.new HTTParty.send(method, url, options)
41
+ end
42
+
43
+ private
44
+
45
+ # Sets up the HTTParty options hash.
46
+ def options
47
+ options = { basic_auth: { username: user, password: nil }}
48
+ if method == :put
49
+ headers = { 'Content-Type' => 'application/json' }
50
+ if ref
51
+ header = ref == '"*"' ? 'If-None-Match' : 'If-Match'
52
+ headers.merge!(header => ref)
53
+ end
54
+ options.merge!(headers: headers, body: data)
55
+ end
56
+ options
57
+ end
58
+
59
+ def verbose?
60
+ verbose == true
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,101 @@
1
+ module Orchestrate::API
2
+
3
+ # Transforms the HTTParty response into a structure that provides ready
4
+ # access to relevant orchestrate.io response data.
5
+ #
6
+ class Response
7
+ # Header
8
+ attr_reader :header
9
+
10
+ # ResponseBody
11
+ attr_reader :body
12
+
13
+ # Creates Header and ResponseBody objects from the HTTParty response.
14
+ # Let's Partay!
15
+ #
16
+ def initialize(partay)
17
+ @success = partay.success?
18
+ @header = Header.new partay.headers, partay.code, partay.msg
19
+ @body = ResponseBody.new partay.body
20
+ end
21
+
22
+ # Returns the value of HTTParty.success?
23
+ def success?
24
+ @success
25
+ end
26
+
27
+ # Provides methods for ready access to relevant information extracted
28
+ # from the HTTParty response headers.
29
+ #
30
+ class Header
31
+
32
+ # Original response headers from HTTParty.
33
+ attr_reader :content
34
+
35
+ # HTTP response code.
36
+ attr_reader :code
37
+
38
+ # HTTP response status.
39
+ attr_reader :status
40
+
41
+ attr_reader :timestamp
42
+
43
+ # ETag value, also known as the 'ref' value.
44
+ attr_reader :etag
45
+
46
+ # Link to the next url in a series of list requests.
47
+ attr_reader :link
48
+
49
+ def initialize(headers, code, msg)
50
+ @content = headers
51
+ @code, @status = code, msg
52
+ @timestamp = headers['date']
53
+ @etag = headers['etag'] if headers['etag']
54
+ @link = headers['link'] if headers['link']
55
+ end
56
+ end
57
+ end
58
+
59
+
60
+ # Decodes body from json into a hash; provides the original body content
61
+ # via the 'content' method.
62
+ #
63
+ class ResponseBody
64
+
65
+ # Original response body from HTTParty.
66
+ attr_reader :content
67
+
68
+ # The json response body converted to a hash.
69
+ attr_reader :to_hash
70
+
71
+ # Set for all results.
72
+ attr_reader :results
73
+
74
+ # Set for list, search, get_events, and get_graph results.
75
+ attr_reader :count
76
+
77
+ # Set for search results.
78
+ attr_reader :total_count
79
+
80
+ # Set for list results.
81
+ attr_reader :next
82
+
83
+ # Error message from the orchestrate.io api.
84
+ attr_reader :message
85
+
86
+ # Error code from the orchestrate.io api.
87
+ attr_reader :code
88
+
89
+ # Initialize instance variables, based on response body contents.
90
+ def initialize(body)
91
+ @content = body
92
+ @to_hash = body.blank? ? {} : ActiveSupport::JSON.decode(body)
93
+ to_hash.each { |k,v| instance_variable_set "@#{k}", v }
94
+ end
95
+
96
+ def result_keys
97
+ results.map { |result| result['path']['key'] } if results
98
+ end
99
+ end
100
+
101
+ end
@@ -0,0 +1,83 @@
1
+ module Orchestrate::API
2
+
3
+ require "active_support/core_ext"
4
+
5
+ # ==== Ruby wrapper for the Orchestrate.io REST *API*.
6
+ #
7
+ # The primary entry point is the <b> #send_request</b> method, which
8
+ # generates a <b> Request</b>, and returns the
9
+ # <b> Response</b> to the caller.
10
+ # <b>{Usage examples for each HTTP request}[Procedural.html]</b>
11
+ # are documented in the <b> Procedural</b> interface module.
12
+ #
13
+ class Wrapper < Object
14
+ include Orchestrate::API::Procedural
15
+
16
+ attr_accessor :verbose
17
+
18
+ # Loads settings from the configuration file and returns the client
19
+ # handle to be used for subsequent requests.
20
+ #
21
+ # Example config file: "./lib/orch_config.json"
22
+ #
23
+ # {
24
+ # "base_url":"https://api.orchestrate.io/v0",
25
+ # "user":"<user-key-from-orchestrate.io>",
26
+ # "verbose":"false"
27
+ # }
28
+ #
29
+ def initialize(config_file)
30
+ orch_config = ::ActiveSupport::JSON.decode open(config_file).read
31
+ @base_url = orch_config['base_url']
32
+ @user = orch_config['user']
33
+ @verbose = orch_config['verbose'] ? orch_config['verbose'] : false
34
+ end
35
+
36
+ # Creates the Request object and sends it via the perform method,
37
+ # which generates and returns the Response object.
38
+ #
39
+ def send_request(method, args)
40
+ request = Orchestrate::API::Request.new(method, build_url(method, args), @user) do |r|
41
+ r.data = args[:json] if args[:json]
42
+ r.ref = args[:ref] if args[:ref]
43
+ r.verbose = verbose
44
+ end
45
+ request.perform
46
+ end
47
+
48
+ private
49
+
50
+ # Builds the URL for each HTTP request to the orchestrate.io api.
51
+ #
52
+ def build_url(method, args)
53
+ uri = "#{@base_url}/#{args[:collection]}"
54
+ if args[:key]
55
+ uri << "/#{args[:key]}"
56
+ if args[:ref]
57
+ uri << "/refs/#{args[:ref].gsub(/"/, '')}" if method == :get
58
+ elsif args[:event_type]
59
+ uri << "/events/#{args[:event_type]}"
60
+ if args[:timestamp]
61
+ if method == :put && args[:timestamp][:start]
62
+ uri << "?start=#{args[:timestamp][:start]}&end=#{args[:timestamp][:end]}"
63
+ elsif method == :get && !args[:timestamp].blank?
64
+ uri << "?timestamp=#{timestamp}"
65
+ end
66
+ end
67
+ elsif args[:kind]
68
+ if method == :put
69
+ uri << "/relation/#{args[:kind]}/#{args[:to_collection]}/#{args[:to_key]}"
70
+ else
71
+ uri << "/relations/#{args[:kind]}"
72
+ end
73
+ end
74
+ elsif args[:path]
75
+ uri << args[:path]
76
+ end
77
+ uri
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orchestrate-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Carrasquer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Gem to interface with orchestrate.io api
42
+ email: jimcar@aracnet.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/orchestrate-api.rb
48
+ - lib/orchestrate_api/procedural.rb
49
+ - lib/orchestrate_api/wrapper.rb
50
+ - lib/orchestrate_api/request.rb
51
+ - lib/orchestrate_api/response.rb
52
+ homepage: https://github.com/jimcar/orchestrate-api
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.0.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Summary for orchestrate-api
76
+ test_files: []