rubaiji 0.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/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # Editor shit files
20
+ *.swp # vim
21
+ *.swo # vim
22
+ *~ # gedit
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubaiji.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
9
+ gem 'rest-client'
10
+ gem 'json'
11
+
12
+ if RUBY_PLATFORM.downcase.include?("darwin")
13
+ gem 'rb-fsevent'
14
+ gem 'growl' # also install growlnotify from the Extras/growlnotify/growlnotify.pkg in Growl disk image
15
+ end
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 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
+
9
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
10
+
11
+ # Lib
12
+ watch(%r{^lib/rubaiji/(.+)\.rb$}) {"spec"}#{ |m| "spec/#{m[1]}_spec.rb" }
13
+ end
14
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Teodor Pripoae
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,29 @@
1
+ # Rubaiji
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubaiji'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubaiji
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,44 @@
1
+ module Rubaiji
2
+ class Connection
3
+ attr_reader :api_id, :api_key, :format
4
+ API_URL = "http://localhost:8000"
5
+ REPORT_INDEX_URL = "/api/v2/report/"
6
+
7
+ def initialize(params)
8
+ params.each do |k, v|
9
+ instance_variable_set("@#{k}", v) unless v.nil?
10
+ end
11
+ end
12
+
13
+ def all_reports
14
+ res = query REPORT_INDEX_URL
15
+ report_set = Rubaiji::ReportSet.new(res["objects"])
16
+ report_set.add_meta(res["meta"])
17
+ report_set
18
+ end
19
+
20
+ def report(resource_uri)
21
+ res = query resource_uri
22
+ report = Rubaiji::Report.new(res)
23
+ report
24
+ end
25
+
26
+ private
27
+ def get_url(url)
28
+ URI.escape "#{API_URL}#{url}?format=json&username=#{api_id}&api_key=#{api_key}"
29
+ end
30
+
31
+ def query(url)
32
+ RestClient.get(get_url(url), {:accept => :json}) { |response, request, result, &block|
33
+ case response.code
34
+ when 200
35
+ JSON.parse response
36
+ when 401
37
+ raise Rubaiji::AuthenticationError
38
+ when 404
39
+ raise Rubaiji::ApiNotFound
40
+ end
41
+ }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ module Rubaiji
2
+ class RubaijiError < StandardError; end
3
+ class AuthenticationError < RubaijiError; end
4
+ end
@@ -0,0 +1,41 @@
1
+ module Rubaiji
2
+ class Report
3
+ attr_reader :id, :results_no, :date, :resource_uri
4
+ attr_accessor :results
5
+
6
+ def initialize(params)
7
+ params.each do |k, v|
8
+ instance_variable_set("@#{k}", v) unless v.nil? || k == "results"
9
+ end
10
+
11
+ unless params["results"].nil?
12
+ self.results = []
13
+ params["results"].each do |res|
14
+ self.results << Rubaiji::Result.new(res)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ class ReportSet < Array
21
+ attr_reader :limit, :next_token, :offset, :previous, :total_count
22
+
23
+ def initialize(array)
24
+ array.each do |e|
25
+ self << Rubaiji::Report.new(
26
+ :resource_uri => e["resource_uri"],
27
+ :date => DateTime.parse(e["date"]),
28
+ :results_no => e["results_no"]
29
+ )
30
+ end
31
+ end
32
+
33
+ def add_meta(meta)
34
+ limit = meta["limit"]
35
+ next_token = meta["next"]
36
+ offset = meta["offset"]
37
+ previous = meta["previous"]
38
+ total_count = meta["total_count"]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ module Rubaiji
2
+ class Result
3
+ attr_reader :id, :link, :rank, :old_rank, :summary,
4
+ :status, :images, :smart, :keywords
5
+ attr_accessor :videos
6
+
7
+ def initialize(params)
8
+ params.each do |k, v|
9
+ instance_variable_set("@#{k}", v) unless v.nil? or k == "videos"
10
+ end
11
+
12
+ _res = JSON.parse("\{\"videos\": #{params["videos"]}\}")
13
+ self.videos = []
14
+ _res["videos"].each do |video|
15
+ self.videos << Rubaiji::Video.new(video)
16
+ end
17
+ end
18
+
19
+ def old?
20
+ status == "0"
21
+ end
22
+
23
+ def new?
24
+ status == "1"
25
+ end
26
+
27
+ def smart?
28
+ smart == true
29
+ end
30
+
31
+ def has_videos?
32
+ videos.size > 0
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Rubaiji
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ module Rubaiji
2
+ class Video
3
+ attr_accessor :embed
4
+
5
+ def initialize(params)
6
+ params.each do |k, v|
7
+ instance_variable_set("@#{k}", v) unless v.nil?
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/rubaiji.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+ require 'uri'
4
+ require 'date'
5
+
6
+ require "rubaiji/connection"
7
+ require 'rubaiji/errors'
8
+ require 'rubaiji/report'
9
+ require 'rubaiji/result'
10
+ require "rubaiji/version"
11
+ require 'rubaiji/video'
12
+
13
+ module Rubaiji
14
+ module Config
15
+ API_ID = ''
16
+ API_KEY = ''
17
+ end
18
+ end
data/rubaiji.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rubaiji/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Teodor Pripoae"]
6
+ gem.email = ["toni@netbaiji.com"]
7
+ gem.description = %q{Ruby bindings for NetBaiji API}
8
+ gem.summary = %q{Ruby bindings for NetBaiji API}
9
+ gem.homepage = "http://netbaiji.com/api/documentation"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rubaiji"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rubaiji::VERSION
17
+ gem.add_development_dependency "rspec", "2.11"
18
+ gem.add_development_dependency "growl"
19
+ gem.add_development_dependency "autotest"
20
+ gem.add_development_dependency "autotest-growl"
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubaiji'
2
+
3
+ describe Rubaiji::Connection do
4
+ it "should return a connection class" do
5
+ c = Rubaiji::Connection.new(:api_id => "test_api_id", :api_key => "test")
6
+ c.class.should eql(Rubaiji::Connection)
7
+ end
8
+
9
+ it "should have api_key and api_id variables" do
10
+ c = Rubaiji::Connection.new(:api_id => "test_api_id", :api_key => "test")
11
+ c.api_id.should eql("test_api_id")
12
+ c.api_key.should eql("test")
13
+ end
14
+
15
+ it "should return authentication error" do
16
+ api_id = "root"
17
+ api_key = "some invalid api_key"
18
+ c = Rubaiji::Connection.new(:api_id => api_id, :api_key => api_key)
19
+
20
+ expect {c.all_reports}.to raise_error(Rubaiji::AuthenticationError)
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ require 'rubaiji'
2
+
3
+ describe Rubaiji::ReportSet do
4
+
5
+ it "should return valid report set" do
6
+ api_id = "root"
7
+ api_key = "8dc3e35e989b51e849592d6fef6afe4bd07aaae7"
8
+ c = Rubaiji::Connection.new(:api_id => api_id, :api_key => api_key)
9
+
10
+ reports = c.all_reports
11
+ reports.class.should eql(Rubaiji::ReportSet)
12
+ end
13
+
14
+ it "should return report instances and have correct fields" do
15
+ api_id = "root"
16
+ api_key = "8dc3e35e989b51e849592d6fef6afe4bd07aaae7"
17
+ c = Rubaiji::Connection.new(:api_id => api_id, :api_key => api_key)
18
+
19
+ reports = c.all_reports
20
+
21
+ reports.each do |report|
22
+ report.should be_an_instance_of Rubaiji::Report
23
+ report.resource_uri.should_not be_nil
24
+ report.results_no.should be_an_instance_of Fixnum
25
+ report.date.should be_an_instance_of DateTime
26
+ end
27
+ end
28
+
29
+ it "should return report instance and have results" do
30
+ api_id = "root"
31
+ api_key = "8dc3e35e989b51e849592d6fef6afe4bd07aaae7"
32
+ c = Rubaiji::Connection.new(:api_id => api_id, :api_key => api_key)
33
+
34
+ report = c.report '/api/v2/report/1270/'
35
+ report.class.should eql(Rubaiji::Report)
36
+
37
+ report.results.each do |result|
38
+ result.class.should eql(Rubaiji::Result)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ RSpec.configure do |config|
2
+ config.formatter = 'Growl::RSpec::Formatter'
3
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubaiji
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Teodor Pripoae
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
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: '2.11'
30
+ - !ruby/object:Gem::Dependency
31
+ name: growl
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: autotest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: autotest-growl
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby bindings for NetBaiji API
79
+ email:
80
+ - toni@netbaiji.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - Guardfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/rubaiji.rb
92
+ - lib/rubaiji/connection.rb
93
+ - lib/rubaiji/errors.rb
94
+ - lib/rubaiji/report.rb
95
+ - lib/rubaiji/result.rb
96
+ - lib/rubaiji/version.rb
97
+ - lib/rubaiji/video.rb
98
+ - rubaiji.gemspec
99
+ - spec/connection_spec.rb
100
+ - spec/report_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://netbaiji.com/api/documentation
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Ruby bindings for NetBaiji API
126
+ test_files:
127
+ - spec/connection_spec.rb
128
+ - spec/report_spec.rb
129
+ - spec/spec_helper.rb