fridge_api 0.1
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.
- data/LICENSE.md +20 -0
- data/README.md +13 -0
- data/Rakefile +2 -0
- data/fridge_api.gemspec +21 -0
- data/lib/fridge_api/client.rb +102 -0
- data/lib/fridge_api/error.rb +11 -0
- data/lib/fridge_api/model.rb +89 -0
- data/lib/fridge_api/options.rb +36 -0
- data/lib/fridge_api/version.rb +5 -0
- data/lib/fridge_api.rb +25 -0
- metadata +88 -0
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Ripeworks
|
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.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/fridge_api.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'fridge_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
7
|
+
spec.add_dependency 'sawyer', '~> 0.5.3'
|
8
|
+
spec.authors = ["Mike Kruk"]
|
9
|
+
spec.description = %q{Simple wrapper for the Fridge API}
|
10
|
+
spec.email = ['mike@ripeworks.com']
|
11
|
+
spec.files = %w(Rakefile LICENSE.md README.md fridge_api.gemspec)
|
12
|
+
spec.files += Dir.glob("lib/**/*.rb")
|
13
|
+
spec.homepage = 'https://github.com/fridge-cms/fridge_api.rb'
|
14
|
+
spec.licenses = ['MIT']
|
15
|
+
spec.name = 'fridge_api'
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
spec.required_ruby_version = '>= 1.9.2'
|
18
|
+
spec.required_rubygems_version = '>= 1.3.5'
|
19
|
+
spec.summary = "Ruby toolkit for working with the Fridge API"
|
20
|
+
spec.version = FridgeApi::VERSION.dup
|
21
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'sawyer'
|
2
|
+
require 'fridge_api/options'
|
3
|
+
require 'fridge_api/error'
|
4
|
+
require 'fridge_api/model'
|
5
|
+
|
6
|
+
module FridgeApi
|
7
|
+
|
8
|
+
class Client
|
9
|
+
|
10
|
+
include FridgeApi::Options
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
reset!
|
14
|
+
FridgeApi::Options.keys.each do |key|
|
15
|
+
instance_variable_set(:"@#{key}", options[key] || instance_variable_get(:"@#{key}"))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(url, options = {})
|
20
|
+
request :get, url, options
|
21
|
+
end
|
22
|
+
|
23
|
+
def post(url, options = {})
|
24
|
+
request :post, url, options
|
25
|
+
end
|
26
|
+
|
27
|
+
def put(url, options = {})
|
28
|
+
request :put, url, options
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete(url, options = {})
|
32
|
+
request :delete, url, options
|
33
|
+
end
|
34
|
+
|
35
|
+
def last_response
|
36
|
+
@last_response if defined? @last_response
|
37
|
+
end
|
38
|
+
|
39
|
+
def agent
|
40
|
+
@agent ||= Sawyer::Agent.new(api_endpoint, {}) do |http|
|
41
|
+
http.headers[:content_type] = "application/json"
|
42
|
+
if !!@access_token
|
43
|
+
http.authorization 'token', @access_token
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def reset_agent
|
49
|
+
@agent = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def request(method, path, data, options = {})
|
53
|
+
@last_response = res = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
|
54
|
+
if res.status == 401
|
55
|
+
refresh_token!
|
56
|
+
return request(method, path, data, options)
|
57
|
+
end
|
58
|
+
parse res.data
|
59
|
+
end
|
60
|
+
|
61
|
+
def refresh_token!
|
62
|
+
res = post("oauth/token", application_authentication)
|
63
|
+
if @last_response.status != 200
|
64
|
+
raise FridgeApi::Unauthorized
|
65
|
+
end
|
66
|
+
@access_token = res.access_token
|
67
|
+
reset_agent
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_model(data)
|
71
|
+
if is_fridge_object?(data)
|
72
|
+
data = Model.new(data)
|
73
|
+
end
|
74
|
+
data
|
75
|
+
end
|
76
|
+
|
77
|
+
def is_fridge_object?(obj)
|
78
|
+
obj.key?(:id) || obj.key?(:uuid)
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def application_authentication
|
84
|
+
{
|
85
|
+
:grant_type => "client_credentials",
|
86
|
+
:client_id => @client_id,
|
87
|
+
:client_secret => @client_secret
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
def parse(data)
|
92
|
+
if data.kind_of? Array
|
93
|
+
return data.map do |v|
|
94
|
+
to_model v
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
to_model data
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module FridgeApi
|
4
|
+
class Model
|
5
|
+
|
6
|
+
def self.new_from_part(part, data)
|
7
|
+
# TODO
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(resource)
|
11
|
+
@raw = resource.to_h
|
12
|
+
@attrs = parse
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
p @attrs
|
17
|
+
end
|
18
|
+
|
19
|
+
def commit
|
20
|
+
@raw.each do |key, val|
|
21
|
+
if val.kind_of? Array
|
22
|
+
if val.first[:part_definition_id]
|
23
|
+
val.each do |i, part|
|
24
|
+
part_name = part_name(part)
|
25
|
+
unless part_value(part) == @attrs[part_name]
|
26
|
+
@raw[key][i][:value] = @attrs[part_name]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
return
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@raw[key] = @attrs[key] unless val == @attrs[key]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Allow fields to be retrieved via Hash notation
|
38
|
+
def [](method)
|
39
|
+
send(method.to_sym)
|
40
|
+
rescue NoMethodError
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# Retrieve field value
|
45
|
+
def method_missing(method, *args)
|
46
|
+
@attrs.has_key?(method.to_sym) ? @attrs[method.to_sym] : super
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def parse
|
52
|
+
hash = {}
|
53
|
+
@raw.each do |key, val|
|
54
|
+
if val.kind_of? Array
|
55
|
+
if val.first[:part_definition_id]
|
56
|
+
val.each do |part|
|
57
|
+
hash[part_name(part)] = part_value(part)
|
58
|
+
end
|
59
|
+
else
|
60
|
+
hash[key] = val.map do |v|
|
61
|
+
v[:id] ? Model.new(v) : v
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
hash[key] = val
|
67
|
+
end
|
68
|
+
hash
|
69
|
+
end
|
70
|
+
|
71
|
+
def part_name(part)
|
72
|
+
name = part[:part] ? part[:part][:name] : part[:name]
|
73
|
+
name.to_sym
|
74
|
+
end
|
75
|
+
|
76
|
+
def part_value(part)
|
77
|
+
value ||= part[:value]
|
78
|
+
|
79
|
+
if part[:part]
|
80
|
+
if part[:part][:type] == "file" || part[:part][:type] == "image"
|
81
|
+
value = JSON.parse part[:value]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
value
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module FridgeApi
|
2
|
+
module Options
|
3
|
+
|
4
|
+
DEFAULTS = {
|
5
|
+
:api_endpoint => "http://api.fridgecms.com/v1"
|
6
|
+
}
|
7
|
+
|
8
|
+
attr_accessor :access_token, :client_id, :client_secret
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def keys
|
12
|
+
@keys ||= [
|
13
|
+
:access_token,
|
14
|
+
:api_endpoint,
|
15
|
+
:client_id,
|
16
|
+
:client_secret
|
17
|
+
]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def configure
|
22
|
+
yield self
|
23
|
+
end
|
24
|
+
|
25
|
+
def reset!
|
26
|
+
FridgeApi::Options.keys.each do |key|
|
27
|
+
instance_variable_set(:"@#{key}", DEFAULTS[key] || nil)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def api_endpoint
|
32
|
+
File.join(@api_endpoint, "")
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/fridge_api.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'fridge_api/client'
|
2
|
+
|
3
|
+
module FridgeApi
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def client(options)
|
7
|
+
@client = FridgeApi::Client.new(options) unless defined?(@client)
|
8
|
+
@client
|
9
|
+
end
|
10
|
+
|
11
|
+
# @private
|
12
|
+
def respond_to_missing?(method_name, include_private=false); client.respond_to?(method_name, include_private); end if RUBY_VERSION >= "1.9"
|
13
|
+
|
14
|
+
# @private
|
15
|
+
def respond_to?(method_name, include_private=false); client.respond_to?(method_name, include_private) || super; end if RUBY_VERSION < "1.9"
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def method_missing(method_name, *args, &block)
|
20
|
+
return super unless client.respond_to?(method_name)
|
21
|
+
client.send(method_name, *args, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fridge_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Kruk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sawyer
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.5.3
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.5.3
|
46
|
+
description: Simple wrapper for the Fridge API
|
47
|
+
email:
|
48
|
+
- mike@ripeworks.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- Rakefile
|
54
|
+
- LICENSE.md
|
55
|
+
- README.md
|
56
|
+
- fridge_api.gemspec
|
57
|
+
- lib/fridge_api/client.rb
|
58
|
+
- lib/fridge_api/error.rb
|
59
|
+
- lib/fridge_api/model.rb
|
60
|
+
- lib/fridge_api/options.rb
|
61
|
+
- lib/fridge_api/version.rb
|
62
|
+
- lib/fridge_api.rb
|
63
|
+
homepage: https://github.com/fridge-cms/fridge_api.rb
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.9.2
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 1.3.5
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.23
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Ruby toolkit for working with the Fridge API
|
88
|
+
test_files: []
|