mashery_rails 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/.travis.yml +0 -8
- data/README.md +69 -7
- data/Rakefile +6 -3
- data/lib/mashery/exceptions.rb +1 -1
- data/lib/mashery/generator.rb +10 -0
- data/lib/mashery/rails.rb +2 -2
- data/lib/mashery/rpc_client/base.rb +10 -4
- data/lib/mashery/version.rb +3 -0
- data/lib/mashery.rb +3 -5
- data/mashery_rails.gemspec +3 -1
- data/spec/fixtures/config.no_key.yml +1 -1
- data/spec/fixtures/config.no_secret.yml +1 -1
- data/spec/fixtures/config.no_site_id.yml +1 -1
- data/spec/fixtures/config.yml +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +6 -4
- data/VERSION +0 -1
- data/masheri.gemspec +0 -26
- data/tasks/mashery.thor +0 -200
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# mashery_rails
|
2
2
|
|
3
|
-
[![Coverage Status](https://coveralls.io/repos/farleyknight/
|
4
|
-
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/farleyknight/mashery_rails/badge.png)](https://coveralls.io/r/farleyknight/mashery_rails)
|
5
4
|
[![Build Status](https://travis-ci.org/farleyknight/mashery_rails.png?branch=master)](https://travis-ci.org/farleyknight/mashery_rails)
|
6
|
-
|
5
|
+
[![Code Climate](https://codeclimate.com/github/farleyknight/mashery_rails.png)](https://codeclimate.com/github/farleyknight/mashery_rails)
|
7
6
|
|
8
7
|
A Rails library for the [Mashery API](http://support.mashery.com/docs/mashery_api).
|
9
8
|
|
@@ -41,23 +40,86 @@ host: "api.mashery.com"
|
|
41
40
|
|
42
41
|
Note this doesn't distinguish between development, testing, production, etc. If you really need that functionality, open a new issue.
|
43
42
|
|
44
|
-
##
|
43
|
+
## JSON-RPC API
|
45
44
|
|
46
45
|
The central data objects are all listed on the [RPC API documentation page](http://support.mashery.com/docs/read/mashery_api/20/API_Objects), and should be supported. However, [fetching objects](http://support.mashery.com/docs/read/mashery_api/20/Fetching_Objects) are the main & typically only supported method for each of these objects. If you feel you want to support more methods, feel free to create a pull request.
|
47
46
|
|
48
|
-
|
49
47
|
### `all`
|
50
48
|
|
51
|
-
Fetch
|
49
|
+
Fetch the first 100 objects of a particular type. The 100 objects constraint is caused by [Mashery](http://support.mashery.com/docs/read/mashery_api/20/Query_Language):
|
50
|
+
|
51
|
+
> Query results are always paginated. There is a limit of fetching 100 records at one time. By default, only the first 100 records are returned.
|
52
52
|
|
53
53
|
```ruby
|
54
54
|
Mashery::Member.all #=>
|
55
55
|
```
|
56
56
|
|
57
|
+
### `find_each`
|
58
|
+
|
59
|
+
Much like `ActiveRecord::Base.find_each`, this will take a block and auto-paginate the object set, working around the 100 items per page limitation stated above.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
Mashery::Member.find_each {|m| m.do_something! }
|
63
|
+
```
|
64
|
+
|
57
65
|
### `first`
|
58
66
|
|
59
|
-
Fetch the first object of a particular type.
|
67
|
+
Fetch the first object of a particular type. This is effectively setting "ITEMS 1" and reifying that object.
|
60
68
|
|
61
69
|
```ruby
|
62
70
|
Mashery::Member.first #=>
|
63
71
|
```
|
72
|
+
|
73
|
+
### `count`
|
74
|
+
|
75
|
+
The total number of objects.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
Mashery::Member.count #=>
|
79
|
+
```
|
80
|
+
|
81
|
+
## Query Language
|
82
|
+
|
83
|
+
Mashery has a basic SQL-like Query Language. This library provides basic AREL-like functionality to build those queries. One small, but important difference is lack of lazy loading. These methods produce queries, but you have to call `all` in order to retrieve the objects.
|
84
|
+
|
85
|
+
### `select`
|
86
|
+
|
87
|
+
Change the SELECT fragment of the query.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
Mashery::Member.select("name") #=>
|
91
|
+
```
|
92
|
+
|
93
|
+
### `where`
|
94
|
+
|
95
|
+
Change the WHERE fragment of the query.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
Mashery::Member.where(name: "User Name")
|
99
|
+
|
100
|
+
#=> SELECT * FROM members WHERE name = 'User Name' ITEMS 100
|
101
|
+
```
|
102
|
+
|
103
|
+
### `items`
|
104
|
+
|
105
|
+
Change the ITEMS fragment of the query.
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
Mashery::Member.items(10)
|
109
|
+
|
110
|
+
#=> => SELECT * FROM members ITEMS 10
|
111
|
+
```
|
112
|
+
|
113
|
+
### `page`
|
114
|
+
|
115
|
+
Change the PAGE fragment of the query.
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
Mashery::Member.page(10)
|
119
|
+
|
120
|
+
#=> SELECT * FROM members PAGE 10 ITEMS 100
|
121
|
+
```
|
122
|
+
|
123
|
+
## REST API
|
124
|
+
|
125
|
+
Mashery's REST API are mostly calls based on service objects.
|
data/Rakefile
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
1
4
|
# Added by devtools
|
2
5
|
require 'devtools'
|
3
6
|
Devtools.init_rake_tasks
|
4
7
|
|
5
8
|
Bundler::GemHelper.install_tasks
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
task :default => :spec
|
12
|
+
RSpec::Core::RakeTask.new
|
data/lib/mashery/exceptions.rb
CHANGED
@@ -21,7 +21,7 @@ module Mashery
|
|
21
21
|
|
22
22
|
class ParamMissing < Exception
|
23
23
|
def initialize(param)
|
24
|
-
super "Couldn't determine Mashery #{param.inspect}! Please check your config/
|
24
|
+
super "Couldn't determine Mashery #{param.inspect}! Please check your config/mashery.yml"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Mashery
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
def create_initializer_file
|
6
|
+
config = %Q{---\nsite_id: '123'\nkey: "abc"\nsecret: "xyz"\nhost: "api.mashery.com"}
|
7
|
+
create_file "config/mashery.yml", config
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/mashery/rails.rb
CHANGED
@@ -3,7 +3,7 @@ module Mashery
|
|
3
3
|
if File.exists? rails_config
|
4
4
|
load_config! rails_config
|
5
5
|
else
|
6
|
-
raise MissingConfig.new
|
6
|
+
# raise MissingConfig.new
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -12,7 +12,7 @@ module Mashery
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class Engine < ::Rails::Engine
|
15
|
-
initializer "
|
15
|
+
initializer "load_mashery_config" do
|
16
16
|
Mashery.load_rails_config!
|
17
17
|
end
|
18
18
|
end
|
@@ -15,6 +15,10 @@ module Mashery
|
|
15
15
|
default_query.all
|
16
16
|
end
|
17
17
|
|
18
|
+
def self.select(argument)
|
19
|
+
default_query.select(argument)
|
20
|
+
end
|
21
|
+
|
18
22
|
def self.items(argument)
|
19
23
|
default_query.items(argument)
|
20
24
|
end
|
@@ -31,6 +35,12 @@ module Mashery
|
|
31
35
|
default_query.find_each(&block)
|
32
36
|
end
|
33
37
|
|
38
|
+
def self.first
|
39
|
+
new(standard_query.items(1).all.items[0])
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
34
44
|
def self.standard_query
|
35
45
|
Mashery.rpc.query_builder(self).from(object_type).items(100)
|
36
46
|
end
|
@@ -39,10 +49,6 @@ module Mashery
|
|
39
49
|
standard_query
|
40
50
|
end
|
41
51
|
|
42
|
-
def self.first
|
43
|
-
new(standard_query.items(1).all.items[0])
|
44
|
-
end
|
45
|
-
|
46
52
|
def self.object_type
|
47
53
|
self.name.underscore.split('/').last.pluralize
|
48
54
|
end
|
data/lib/mashery.rb
CHANGED
@@ -16,11 +16,13 @@ require 'mashery/rest_client'
|
|
16
16
|
require 'mashery/query_builder'
|
17
17
|
require 'mashery/rest_client/query'
|
18
18
|
|
19
|
-
|
20
19
|
require 'mashery/member'
|
21
20
|
require 'mashery/key'
|
22
21
|
require 'mashery/service'
|
23
22
|
|
23
|
+
require 'mashery/rails'
|
24
|
+
require 'mashery/generator'
|
25
|
+
|
24
26
|
module Mashery
|
25
27
|
@@config = nil
|
26
28
|
|
@@ -49,7 +51,3 @@ module Mashery
|
|
49
51
|
Mashery.rest.activity(activity_type, service_id, options)
|
50
52
|
end
|
51
53
|
end
|
52
|
-
|
53
|
-
if defined?(Rails)
|
54
|
-
require 'mashery/rails'
|
55
|
-
end
|
data/mashery_rails.gemspec
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
require File.expand_path("../lib/mashery/version", __FILE__)
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
4
|
s.name = "mashery_rails"
|
3
|
-
s.version =
|
5
|
+
s.version = Mashery::VERSION
|
4
6
|
|
5
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
8
|
s.authors = ["Farley Knight"]
|
data/spec/fixtures/config.yml
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mashery_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- LICENSE.txt
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
|
-
- VERSION
|
83
82
|
- config/devtools.yml
|
84
83
|
- config/flay.yml
|
85
84
|
- config/flog.yml
|
@@ -90,6 +89,7 @@ files:
|
|
90
89
|
- lib/mashery.rb
|
91
90
|
- lib/mashery/config.rb
|
92
91
|
- lib/mashery/exceptions.rb
|
92
|
+
- lib/mashery/generator.rb
|
93
93
|
- lib/mashery/key.rb
|
94
94
|
- lib/mashery/local.rb
|
95
95
|
- lib/mashery/member.rb
|
@@ -101,7 +101,7 @@ files:
|
|
101
101
|
- lib/mashery/rpc_client/base.rb
|
102
102
|
- lib/mashery/rpc_client/response.rb
|
103
103
|
- lib/mashery/service.rb
|
104
|
-
-
|
104
|
+
- lib/mashery/version.rb
|
105
105
|
- mashery_rails.gemspec
|
106
106
|
- spec/fixtures/config.no_host.yml
|
107
107
|
- spec/fixtures/config.no_key.yml
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- spec/mashery/service_spec.rb
|
118
118
|
- spec/mashery_spec.rb
|
119
119
|
- spec/spec_helper.rb
|
120
|
-
- tasks/mashery.thor
|
121
120
|
homepage: http://github.com/farleyknight/mashery_rails
|
122
121
|
licenses:
|
123
122
|
- MIT
|
@@ -131,6 +130,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
130
|
- - ! '>='
|
132
131
|
- !ruby/object:Gem::Version
|
133
132
|
version: '0'
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
hash: 967152834943881282
|
134
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
137
|
none: false
|
136
138
|
requirements:
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.6.2
|
data/masheri.gemspec
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
Gem::Specification.new do |s|
|
2
|
-
s.name = "masheri"
|
3
|
-
s.version = File.read("VERSION")
|
4
|
-
|
5
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
-
s.authors = ["Farley Knight"]
|
7
|
-
s.date = "2013-09-23"
|
8
|
-
s.email = "farleyknight@gmail.com"
|
9
|
-
s.extra_rdoc_files = [
|
10
|
-
"LICENSE.txt",
|
11
|
-
"README.md",
|
12
|
-
"README.textile"
|
13
|
-
]
|
14
|
-
|
15
|
-
s.files = `git ls-files`.split("\n")
|
16
|
-
|
17
|
-
s.homepage = "http://github.com/farleyknight/masheri"
|
18
|
-
s.licenses = ["MIT"]
|
19
|
-
s.require_paths = ["lib"]
|
20
|
-
s.rubygems_version = "1.8.25"
|
21
|
-
s.summary = "Rails gem for Mashery. Clean config and includes tests with decent test coverage"
|
22
|
-
|
23
|
-
s.add_dependency(%q<activesupport>, [">= 3.1.0"])
|
24
|
-
s.add_dependency(%q<rest-client>, [">= 0"])
|
25
|
-
s.add_dependency(%q<json>, [">= 0"])
|
26
|
-
end
|
data/tasks/mashery.thor
DELETED
@@ -1,200 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler'
|
3
|
-
Bundler.setup
|
4
|
-
|
5
|
-
# XXX: only do this when the task has not been installed
|
6
|
-
$: << 'lib'
|
7
|
-
|
8
|
-
require 'mashery'
|
9
|
-
|
10
|
-
module Mashery
|
11
|
-
class CLI < Thor
|
12
|
-
namespace :mashery
|
13
|
-
|
14
|
-
desc "echo VALUE", "Echo the provided value (tests connectivity and authentication)"
|
15
|
-
def echo(value)
|
16
|
-
run { ok(::Mashery.client.echo(value)) }
|
17
|
-
end
|
18
|
-
|
19
|
-
protected
|
20
|
-
def run(&block)
|
21
|
-
site_id = ENV['MASHERY_SITE_ID'] or
|
22
|
-
raise Exception, "Please set the MASHERY_SITE_ID environment variable."
|
23
|
-
key = ENV['MASHERY_API_KEY'] or
|
24
|
-
raise Exception, "Please set the MASHERY_API_KEY environment variable."
|
25
|
-
secret = ENV['MASHERY_SHARED_SECRET'] or
|
26
|
-
raise Exception, "Please set the MASHERY_SHARED_SECRET environment variable."
|
27
|
-
::Mashery.test_mode = ENV['MASHERY_PRODUCTION_MODE'].blank?
|
28
|
-
::Mashery.logger.level = Logger::DEBUG
|
29
|
-
::Mashery.client = ::Mashery::Client.new(site_id, key, secret)
|
30
|
-
begin
|
31
|
-
yield
|
32
|
-
rescue ::Mashery::HttpException => e
|
33
|
-
error("HTTP error: #{e.message}")
|
34
|
-
rescue ::Mashery::JsonRpcException => e
|
35
|
-
error(e.message)
|
36
|
-
rescue ::Mashery::ValidationException => e
|
37
|
-
e.errors.each {|err| warn("#{err['field']}: #{err['message']}")}
|
38
|
-
error("Unable to execute method due to validation errors")
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def warn(msg)
|
43
|
-
say_status :WARN, msg, :yellow
|
44
|
-
end
|
45
|
-
|
46
|
-
def ok(msg)
|
47
|
-
say_status :OK, msg
|
48
|
-
end
|
49
|
-
|
50
|
-
def error(msg)
|
51
|
-
say_status :ERROR, msg, :red
|
52
|
-
end
|
53
|
-
|
54
|
-
def debug(msg)
|
55
|
-
say_status :DEBUG, msg, :cyan
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class MemberCLI < CLI
|
60
|
-
namespace 'mashery:member'
|
61
|
-
|
62
|
-
desc "create USERNAME DISPLAY_NAME EMAIL [--fields ...]", "Create a member"
|
63
|
-
method_option :fields, :type => :hash
|
64
|
-
def create(username, display_name, email)
|
65
|
-
run do
|
66
|
-
begin
|
67
|
-
member = ::Mashery::Member.create(username, display_name, email, options[:fields])
|
68
|
-
ok("Member #{member.username} created")
|
69
|
-
rescue ::Mashery::DuplicateObjectException
|
70
|
-
error("The username #{username} has already been claimed")
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
desc "fetch USERNAME", "Fetch a member"
|
76
|
-
def fetch(username)
|
77
|
-
run do
|
78
|
-
member = ::Mashery::Member.fetch(username)
|
79
|
-
if member
|
80
|
-
ok("Member #{username} found")
|
81
|
-
say(member.to_yaml)
|
82
|
-
else
|
83
|
-
warn("Member #{username} not found")
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
desc "addrole USERNAME ROLE_ID", "Assign a role to a member"
|
89
|
-
def addrole(username, role_id)
|
90
|
-
run do
|
91
|
-
member = ::Mashery::Member.fetch(username)
|
92
|
-
if member
|
93
|
-
role = ::Mashery::Role.fetch(role_id.to_i)
|
94
|
-
if role
|
95
|
-
member.add_role(role)
|
96
|
-
ok("Role #{role.name} assigned to member #{username}")
|
97
|
-
else
|
98
|
-
warn("Role #{role_id} not found")
|
99
|
-
end
|
100
|
-
else
|
101
|
-
warn("Member #{username} not found")
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
desc "rmrole USERNAME ROLE_ID", "Revoke a role from a member"
|
107
|
-
def rmrole(username, role_id)
|
108
|
-
run do
|
109
|
-
member = ::Mashery::Member.fetch(username)
|
110
|
-
if member
|
111
|
-
role = ::Mashery::Role.fetch(role_id.to_i)
|
112
|
-
if role
|
113
|
-
member.remove_role(role)
|
114
|
-
ok("Role #{role.name} revoked from member #{username}")
|
115
|
-
else
|
116
|
-
warn("Role #{role_id} not found")
|
117
|
-
end
|
118
|
-
else
|
119
|
-
warn("Member #{username} not found")
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
desc "delete USERNAME", "Delete a member"
|
125
|
-
def delete(username)
|
126
|
-
run do
|
127
|
-
::Mashery::Member.delete(username)
|
128
|
-
ok("Member #{username} deleted")
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
class KeyCLI < CLI
|
134
|
-
namespace 'mashery:key'
|
135
|
-
|
136
|
-
desc "create SERVICE_KEY USERNAME [--fields ...]", "Create a key"
|
137
|
-
method_option :fields, :type => :hash
|
138
|
-
def create(service_key, username)
|
139
|
-
run do
|
140
|
-
key = ::Mashery::Key.create(service_key, username, options[:fields])
|
141
|
-
ok("Key #{key.id} created for member #{username} and service #{service_key}")
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
desc "fetch ID", "Fetch a key"
|
146
|
-
def fetch(id)
|
147
|
-
run do
|
148
|
-
key = ::Mashery::Key.fetch(id.to_i)
|
149
|
-
if key
|
150
|
-
ok("Key #{id} found")
|
151
|
-
say(key.to_yaml)
|
152
|
-
else
|
153
|
-
warn("Key #{id} not found")
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
desc "delete ID", "Delete a key"
|
159
|
-
def delete(id)
|
160
|
-
run do
|
161
|
-
::Mashery::Key.delete(id.to_i)
|
162
|
-
ok("Key #{id} deleted")
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
class RoleCLI < CLI
|
168
|
-
namespace 'mashery:role'
|
169
|
-
|
170
|
-
desc "create NAME [--fields ...]", "Create a role"
|
171
|
-
method_option :fields, :type => :hash
|
172
|
-
def create(name)
|
173
|
-
run do
|
174
|
-
role = ::Mashery::Role.create(name, options[:fields])
|
175
|
-
ok("Role #{role.id} created with name #{name}")
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
desc "fetch ID", "Fetch a role"
|
180
|
-
def fetch(id)
|
181
|
-
run do
|
182
|
-
role = ::Mashery::Role.fetch(id.to_i)
|
183
|
-
if role
|
184
|
-
ok("Role #{id} found")
|
185
|
-
say(role.to_yaml)
|
186
|
-
else
|
187
|
-
warn("Role #{id} not found")
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
desc "delete ID", "Delete a role"
|
193
|
-
def delete(id)
|
194
|
-
run do
|
195
|
-
::Mashery::Role.delete(id.to_i)
|
196
|
-
ok("Role #{id} deleted")
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|