masheri 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'http://rubygems.org'
2
+ gem 'activesupport', "3.0.0"
2
3
  gem 'thor'
3
4
  gem 'httparty'
4
- gem 'activesupport'
5
5
  gem 'json'
data/Gemfile.lock CHANGED
@@ -12,7 +12,7 @@ PLATFORMS
12
12
  ruby
13
13
 
14
14
  DEPENDENCIES
15
- activesupport
15
+ activesupport (= 3.0.0)
16
16
  httparty
17
17
  json
18
18
  thor
data/Rakefile CHANGED
@@ -1,16 +1,15 @@
1
1
  begin
2
2
  require 'jeweler'
3
- require './lib/masheri/version.rb'
3
+ Jeweler::RubygemsDotOrgTasks.new
4
+
4
5
  Jeweler::Tasks.new do |gemspec|
5
6
  gemspec.name = "masheri"
6
7
  gemspec.summary = "Rails gem for Mashery. Clean config and includes tests."
7
8
  gemspec.email = "farleyknight@gmail.com"
8
9
  gemspec.homepage = "http://github.com/farleyknight/masheri"
9
10
  gemspec.authors = ["Farley Knight"]
10
- gemspec.version = Masheri::Version::STRING
11
11
  gemspec.license = "MIT"
12
12
  end
13
- Jeweler::RubygemsDotOrgTasks.new
14
13
  rescue LoadError => error
15
14
  puts error
16
15
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/lib/masheri.rb CHANGED
@@ -9,14 +9,24 @@ require 'masheri/key'
9
9
  require 'masheri/role'
10
10
  require 'masheri/query'
11
11
 
12
+ if defined?(Rails)
13
+ require 'masheri/rails'
14
+ end
15
+
12
16
  module Masheri
13
17
  mattr_accessor :client
14
18
  @@client = nil
15
19
 
16
20
  mattr_accessor :logger, :instance_writer => false
21
+ # TODO: Handle possible Rails logger.
17
22
  @@logger = Logger.new(STDOUT)
18
23
  @@logger.level = Logger::WARN
19
24
 
20
25
  mattr_accessor :test_mode, :instance_writer => false
21
- @@test_mode = true
26
+ @@test_mode = false
27
+
28
+ def self.load_config!(yaml_file)
29
+ Masheri.client = Masheri::Client.from_yaml(yaml_file)
30
+ end
22
31
  end
32
+
@@ -1,17 +1,24 @@
1
1
  require 'active_support'
2
2
  require 'json'
3
3
  require 'httparty'
4
- require 'md5'
4
+ require 'digest/md5'
5
5
 
6
6
  module Masheri
7
7
  class Client
8
8
  TEST_HOST = 'api.sandbox.mashery.com'
9
9
  PRODUCTION_HOST = 'api.mashery.com'
10
10
 
11
+ def self.from_yaml(yaml_file)
12
+ config = YAML.load_file(yaml_file)
13
+ new(config["site_id"], config["key"], config["secret"])
14
+ end
15
+
16
+ attr_accessor :uri, :key, :secret
17
+
11
18
  def initialize(site_id, key, secret)
12
- host = Masheri.test_mode ? TEST_HOST : PRODUCTION_HOST
13
- @uri = "http://#{host}/v2/json-rpc/#{site_id}"
14
- @key = key
19
+ host = Masheri.test_mode ? TEST_HOST : PRODUCTION_HOST
20
+ @uri = "http://#{host}/v2/json-rpc/#{site_id}"
21
+ @key = key
15
22
  @secret = secret
16
23
  end
17
24
 
@@ -19,22 +26,36 @@ module Masheri
19
26
  call_remote('test.echo', value)
20
27
  end
21
28
 
22
- def call_remote(method, *params)
29
+ def perform(params)
23
30
  # all calls are synchronous, so id in request and response will always be 1
24
31
  if Masheri.logger
25
- Masheri.logger.debug("Calling method #{method} with params #{params.inspect} on URI #{signed_uri}")
32
+ Masheri.logger.debug("Calling query with params #{params.inspect} on URI #{full_url}")
26
33
  end
27
- req = ::JSON[{:version => '1.1', :method => method, :params => params, :id => 1}]
28
- response = HTTParty.post(signed_uri, :body => req)
29
- raise HttpException.new(response.headers['x-mashery-error-code']) unless response.code < 300
30
- res = ::JSON[response.body]
31
- raise Exception.create(res['error']) if res.include?('error')
32
- res['result']
34
+
35
+ response = RestClient.post(full_url, params.to_json, {
36
+ "Content-Type" => "application/json",
37
+ "Accept" => "text/plain",
38
+ "Content-Length" => params.size
39
+ })
40
+
41
+ JSON.parse response
42
+ end
43
+
44
+ def query(string)
45
+ call_remote("object.query", params: string)
46
+ end
47
+
48
+ def call_remote(method, opts = {})
49
+ perform({'method' => method, 'params'=> [opts[:params]], 'id' => 1})
50
+ end
51
+
52
+ def create(type, params)
53
+ call_remote("#{type}.create")
33
54
  end
34
55
 
35
56
  protected
36
- def signed_uri
37
- "#{@uri}?apikey=#{@key}&sig=#{MD5.new(@key + @secret + Time.now.to_i.to_s).hexdigest}"
57
+ def full_url
58
+ "#{@uri}?apikey=#{@key}&sig=#{Digest::MD5.hexdigest(@key + @secret + Time.now.to_f.to_i.to_s)}"
38
59
  end
39
60
  end
40
61
  end
@@ -0,0 +1,2 @@
1
+
2
+ Masheri.load_rails_config!
@@ -1,9 +1,11 @@
1
1
  module Masheri
2
2
  class Member < ApiObjectBase
3
3
  attr_reader :created, :updated
4
+
4
5
  attr_accessor(:username, :email, :display_name, :uri, :blog, :im, :imsvc, :phone,
5
- :company, :address1, :address2, :locality, :region, :postal_code, :country_code, :first_name, :last_name,
6
- :registration_ipaddr, :area_status, :external_id, :passwd_new)
6
+ :company, :address1, :address2, :locality, :region, :postal_code,
7
+ :country_code, :first_name, :last_name, :registration_ipaddr, :area_status,
8
+ :external_id, :passwd_new)
7
9
 
8
10
  def self.create(username, display_name, email, fields = {})
9
11
  fields ||= {}
@@ -11,6 +13,14 @@ module Masheri
11
13
  super(our_fields)
12
14
  end
13
15
 
16
+ def self.fetch_all
17
+ Masheri.client.call_remote("object.query", params: "SELECT * FROM members")
18
+ end
19
+
20
+ def self.fetch_by_username(username)
21
+ Masheri.client.call_remote("object.query", params: "SELECT * FROM members WHERE username = '#{username}'")
22
+ end
23
+
14
24
  def add_role(role_or_id)
15
25
  role_id = role_or_id.is_a?(Role) ? role_or_id.id : role_or_id
16
26
  Masheri.client.call_remote('member.addRole', {'username' => username}, {'id' => role_id})
data/lib/masheri/query.rb CHANGED
@@ -45,7 +45,7 @@ module Masheri
45
45
 
46
46
  # Page through the results. Due heavy use of the API, this method
47
47
  # takes a qps parameter to control how often the API is called.
48
- def fetch_all(qps=DEFAULT_QUERIES_PER_SECOND)
48
+ def fetch_all(qps = DEFAULT_QUERIES_PER_SECOND)
49
49
  response = execute
50
50
  items = response['items']
51
51
 
@@ -0,0 +1,21 @@
1
+ module Masheri
2
+ class MissingConfig < Exception; end
3
+
4
+ def self.load_rails_config!
5
+ if File.exists? rails_config
6
+ load_config! rails_config
7
+ else
8
+ raise MissingConfig.new("Couldn't find config/masheri.yml! Please run rails g masheri:install to create one and populate it with the necessary API credentials.")
9
+ end
10
+ end
11
+
12
+ def self.rails_config
13
+ Rails.root.join("config", "masheri.yml")
14
+ end
15
+
16
+ class Engine < ::Rails::Engine
17
+ initializer "load_masheri_config" do
18
+ Masheri.load_rails_config!
19
+ end
20
+ end
21
+ end
data/masheri.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "masheri"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Farley Knight"]
12
- s.date = "2013-09-04"
12
+ s.date = "2013-09-05"
13
13
  s.email = "farleyknight@gmail.com"
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -28,11 +28,12 @@ Gem::Specification.new do |s|
28
28
  "lib/masheri/api_object_base.rb",
29
29
  "lib/masheri/client.rb",
30
30
  "lib/masheri/exceptions.rb",
31
+ "lib/masheri/init.rb",
31
32
  "lib/masheri/key.rb",
32
33
  "lib/masheri/member.rb",
33
34
  "lib/masheri/query.rb",
35
+ "lib/masheri/rails.rb",
34
36
  "lib/masheri/role.rb",
35
- "lib/masheri/version.rb",
36
37
  "masheri.gemspec",
37
38
  "tasks/mashery.thor"
38
39
  ]
@@ -46,20 +47,20 @@ Gem::Specification.new do |s|
46
47
  s.specification_version = 3
47
48
 
48
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<activesupport>, ["= 3.0.0"])
49
51
  s.add_runtime_dependency(%q<thor>, [">= 0"])
50
52
  s.add_runtime_dependency(%q<httparty>, [">= 0"])
51
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
52
53
  s.add_runtime_dependency(%q<json>, [">= 0"])
53
54
  else
55
+ s.add_dependency(%q<activesupport>, ["= 3.0.0"])
54
56
  s.add_dependency(%q<thor>, [">= 0"])
55
57
  s.add_dependency(%q<httparty>, [">= 0"])
56
- s.add_dependency(%q<activesupport>, [">= 0"])
57
58
  s.add_dependency(%q<json>, [">= 0"])
58
59
  end
59
60
  else
61
+ s.add_dependency(%q<activesupport>, ["= 3.0.0"])
60
62
  s.add_dependency(%q<thor>, [">= 0"])
61
63
  s.add_dependency(%q<httparty>, [">= 0"])
62
- s.add_dependency(%q<activesupport>, [">= 0"])
63
64
  s.add_dependency(%q<json>, [">= 0"])
64
65
  end
65
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masheri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,26 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-04 00:00:00.000000000 Z
12
+ date: 2013-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: thor
15
+ name: activesupport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 3.0.0
30
30
  - !ruby/object:Gem::Dependency
31
- name: httparty
31
+ name: thor
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -44,7 +44,7 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: activesupport
47
+ name: httparty
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -95,11 +95,12 @@ files:
95
95
  - lib/masheri/api_object_base.rb
96
96
  - lib/masheri/client.rb
97
97
  - lib/masheri/exceptions.rb
98
+ - lib/masheri/init.rb
98
99
  - lib/masheri/key.rb
99
100
  - lib/masheri/member.rb
100
101
  - lib/masheri/query.rb
102
+ - lib/masheri/rails.rb
101
103
  - lib/masheri/role.rb
102
- - lib/masheri/version.rb
103
104
  - masheri.gemspec
104
105
  - tasks/mashery.thor
105
106
  homepage: http://github.com/farleyknight/masheri
@@ -115,6 +116,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
116
  - - ! '>='
116
117
  - !ruby/object:Gem::Version
117
118
  version: '0'
119
+ segments:
120
+ - 0
121
+ hash: 3621785355693449407
118
122
  required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  none: false
120
124
  requirements:
@@ -1,9 +0,0 @@
1
- module Masheri
2
- module Version
3
- MAJOR = 0
4
- MINOR = 1
5
- PATCH = 1
6
-
7
- STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
- end
9
- end