adn 0.0.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,12 +2,8 @@
2
2
  *.rbc
3
3
  .bundle
4
4
  .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
5
  coverage
10
- doc/
6
+ InstalledFiles
11
7
  lib/bundler/man
12
8
  pkg
13
9
  rdoc
@@ -15,3 +11,8 @@ spec/reports
15
11
  test/tmp
16
12
  test/version_tmp
17
13
  tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ bundler_args: --without documentation
3
+ rvm:
4
+ - 1.9.3
5
+ branches:
6
+ only:
7
+ - master
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in adn.gemspec
1
+ source :rubygems
4
2
  gemspec
3
+
4
+ group :test do
5
+ gem 'rake'
6
+ gem 'minitest', '~> 3.3'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ adn (0.3.2)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ minitest (3.3.0)
10
+ rake (0.9.2.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ adn!
17
+ minitest (~> 3.3)
18
+ rake
data/README.md CHANGED
@@ -1,29 +1,66 @@
1
- # Adn
1
+ ADN
2
+ ===
2
3
 
3
- TODO: Write a gem description
4
+ [![Build Status](https://secure.travis-ci.org/kishyr/ADNRuby.png)](http://travis-ci.org/kishyr/ADNRuby)
4
5
 
5
- ## Installation
6
+ A Ruby library for App.net by [@kishyr](https://alpha.app.net/kishyr).
6
7
 
7
- Add this line to your application's Gemfile:
8
+ This library follows the current [App.net API Spec](https://github.com/appdotnet/api-spec) (as at 26 August 2012).
8
9
 
9
- gem 'adn'
10
+ ### Contributors
10
11
 
11
- And then execute:
12
+ ADN is brought to you by these very handsome gentlemen:
12
13
 
13
- $ bundle
14
+ * Kishyr Ramdial ([@kishyr](https://alpha.app.net/kishyr))
15
+ * Dave Goodchild ([@buddhamagnet](https://alpha.app.net/buddhamagnet))
16
+ * Peter Hellberg ([@peterhellberg](https://alpha.app.net/peterhellberg))
14
17
 
15
- Or install it yourself as:
18
+ ### Installation
16
19
 
17
- $ gem install adn
20
+ Simply add ADN to your Gemfile:
18
21
 
19
- ## Usage
22
+ `gem "adn"`
20
23
 
21
- TODO: Write usage instructions here
24
+ Or you can install ADN directly from rubygems.org:
22
25
 
23
- ## Contributing
26
+ `gem install adn`
24
27
 
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
28
+ ### Usage
29
+ For API calls that accept parameters described in the App.net API Spec, simply pass them as a dictionary to the method.
30
+
31
+ ```ruby
32
+ require 'adn'
33
+
34
+ ADN.token = "your App.net OAuth-token here."
35
+
36
+ # TODO: Actually describe the usage of the library
37
+ # (Should be refactored first)
38
+ ```
39
+
40
+ Complete documentation will be available soon, but in the meantime you can browse all API methods in `lib/adnruby.rb`.
41
+
42
+ ### Changelog
43
+
44
+ * **Version 0.3.1** (28 August 2012)
45
+ * Fixed naming conflict
46
+ * **Version 0.3** (28 August 2012)
47
+ * Now includes a Post class that describes each post along with a bunch of new methods of accessing a post's replies and the post's original post.
48
+ * Users methods that return user details (follow, unfollow, following, followers, mute, unmute, mute_list) will now return either a User object back, or an array of User objects.
49
+ * Similarly, User methods that return post details (posts, mentions, stream) will return a Post object, or an array of Post objects.
50
+ * To accomplish all this, I've had to change the module structure which will break existing code if you've relied on the modules to access any data. Basically, all modules begin with ADN::API:: now (eg. ADN::API::Post, ADN::API::User).
51
+ * **Version 0.2** (27 August 2012)
52
+ * Changed all existing classes to modules and introduced the User class for easily accessing user details
53
+ * **Version 0.1** (26 August 2012)
54
+ * Really basic functionality for all existing App.new API methods as per the spec.
55
+
56
+ ---
57
+
58
+ ### License
59
+
60
+ **ADN** is licensed under the MIT License and is Copyright (c) 2012 Kishyr Ramdial.
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,2 +1,24 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ require 'find'
3
+ require 'rake/testtask'
4
+ require 'bundler/gem_tasks'
5
+
6
+ task :default => :spec
7
+
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.test_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc "Run cane to check quality metrics"
13
+ task :quality do
14
+ Bundler.with_clean_env do
15
+ exec 'cane'
16
+ end
17
+ end
18
+
19
+ desc "Run tests in a loop using kicker (>= 3.0.0pre1)"
20
+ task :kicker do
21
+ Bundler.with_clean_env do
22
+ exec 'kicker --no-notification -r ruby -e "clear && rake" spec lib'
23
+ end
24
+ end
data/adn.gemspec CHANGED
@@ -1,17 +1,17 @@
1
- # -*- encoding: utf-8 -*-
1
+ # encoding: UTF-8
2
+
2
3
  require File.expand_path('../lib/adn/version', __FILE__)
3
4
 
4
- Gem::Specification.new do |gem|
5
- gem.authors = ["Justin Balthrop"]
6
- gem.email = ["git@justinbalthrop.com"]
7
- gem.description = %q{App.net command line}
8
- gem.summary = %q{Command line client for App.net}
9
- gem.homepage = "http://github.com/ninjudd/adn"
5
+ Gem::Specification.new do |s|
6
+ s.name = 'adn'
7
+ s.version = ADN::VERSION
8
+ s.authors = ["Kishyr Ramdial", "Dave Goodchild", "Peter Hellberg"]
9
+ s.email = ["kishyr@gmail.com", "buddhamagnet@gmail.com", "peter@c7.se"]
10
+ s.description = "A simple and easy to use library to interact with App.net's API"
11
+ s.summary = "A Ruby library for App.net"
12
+ s.homepage = "https://github.com/kishyr/ADNRuby"
13
+ s.files = `git ls-files`.split($\)
14
+ s.test_files = s.files.grep(%r{^(spec)/})
10
15
 
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 = "adn"
15
- gem.require_paths = ["lib"]
16
- gem.version = Adn::VERSION
16
+ s.required_ruby_version = '>= 1.9.3'
17
17
  end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module Filter
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/blob/master/resources/filters.md
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module Post
6
+ def self.create(params)
7
+ ADN::API.post("#{ADN::API_ENDPOINT_POSTS}", params)
8
+ end
9
+
10
+ def self.retrieve(post_id)
11
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/#{post_id}")
12
+ end
13
+
14
+ def self.by_id(post_id)
15
+ self.retrieve(post_id)
16
+ end
17
+
18
+ def self.delete(post_id)
19
+ ADN::API.delete("#{ADN::API_ENDPOINT_POSTS}/#{post_id}")
20
+ end
21
+
22
+ def self.replies(post_id, params = nil)
23
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/#{post_id}/replies", params)
24
+ end
25
+
26
+ def self.by_user(user_id, params = nil)
27
+ ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}/posts", params)
28
+ end
29
+
30
+ def self.mentioning_user(user_id, params = nil)
31
+ ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}/mentions", params)
32
+ end
33
+
34
+ def self.stream(params = nil)
35
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/stream", params)
36
+ end
37
+
38
+ def self.global_stream(params = nil)
39
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/stream/global", params)
40
+ end
41
+
42
+ def self.by_hashtag(hashtag, params = nil)
43
+ ADN::API.get("#{ADN::API_ENDPOINT_POSTS}/tag/#{hashtag}", params)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'delegate'
4
+
5
+ module ADN
6
+ module API
7
+ class Response < SimpleDelegator
8
+ def has_error?
9
+ has_key?("error")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module Stream
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/blob/master/resources/streams.md
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module Subscription
6
+ # Not Yet Implemented
7
+ # https://github.com/appdotnet/api-spec/
8
+ # blob/master/resources/subscriptions.md
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module Token
6
+ def self.current
7
+ result = ADN::API.get(ADN::API_ENDPOINT_TOKEN)
8
+ result["data"] unless result.has_error?
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module API
5
+ module User
6
+ def self.retrieve(user_id)
7
+ ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}")
8
+ end
9
+
10
+ def self.by_id(user_id)
11
+ self.retrieve(user_id)
12
+ end
13
+
14
+ def self.following(user_id)
15
+ ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}/following")
16
+ end
17
+
18
+ def self.followers(user_id)
19
+ ADN::API.get("#{ADN::API_ENDPOINT_USERS}/#{user_id}/followers")
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/adn/api.rb ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ %w{response filter post stream subscription token user}.each do |f|
4
+ require_relative "api/#{f}"
5
+ end
6
+
7
+ module ADN
8
+ module API
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ def perform(request)
13
+ request.add_field("Authorization", "Bearer #{ADN.token}")
14
+ response = JSON.parse ADN::HTTP.request(request).body
15
+
16
+ Response.new(response).tap { |r|
17
+ raise ADN::API::Error, r['error'] if r.has_error?
18
+ }
19
+ end
20
+
21
+ def get(url, params = nil)
22
+ url = params.nil? ? url : [url, URL.encode_www_form(params)].join("?")
23
+ request = Net::HTTP::Get.new(url)
24
+ perform(request)
25
+ end
26
+
27
+ def post(url, params = nil)
28
+ request = Net::HTTP::Post.new(url)
29
+ request.set_form_data(params) if params
30
+ perform(request)
31
+ end
32
+
33
+ def put(url, params = nil)
34
+ request = Net::HTTP::Put.new(url)
35
+ request.set_form_data(params) if params
36
+ perform(request)
37
+ end
38
+
39
+ def delete(url)
40
+ request = Net::HTTP::Delete.new(url)
41
+ perform(request)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'net/https'
4
+
5
+ module ADN
6
+ API_HOST = "alpha-api.app.net"
7
+ API_ENDPOINT = "/stream/0"
8
+ API_ENDPOINT_POSTS = "#{API_ENDPOINT}/posts"
9
+ API_ENDPOINT_USERS = "#{API_ENDPOINT}/users"
10
+ API_ENDPOINT_TOKEN = "#{API_ENDPOINT}/token"
11
+ HTTP = Net::HTTP.new(API_HOST, 443)
12
+
13
+ HTTP.use_ssl = true
14
+ end
data/lib/adn/post.rb ADDED
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ class Post
5
+ attr_accessor :post_id, :created_at, :entities,
6
+ :html, :id, :num_replies, :reply_to,
7
+ :source, :text, :thread_id, :user
8
+
9
+ def self.send_post(params)
10
+ result = ADN::API::Post.create(params)
11
+ Post.new(result["data"]) unless result.has_error?
12
+ end
13
+
14
+ def initialize(raw_post)
15
+ if raw_post.respond_to?(:each_pair)
16
+ set_values(raw_post)
17
+ post_id = id
18
+ else
19
+ post_id = raw_post
20
+ post_details = details
21
+ if post_details.has_key? "data"
22
+ set_values(post_details["data"])
23
+ end
24
+ end
25
+ end
26
+
27
+ def details
28
+ if id
29
+ Hash[self.instance_variables.map { |i| [i.to_s.slice(1..-1), self.instance_variable_get(i)]}]
30
+ else
31
+ ADN::API::Post.by_id(post_id)
32
+ end
33
+ end
34
+
35
+ def created_at
36
+ DateTime.parse(@created_at)
37
+ end
38
+
39
+ def user
40
+ ADN::User.new(@user)
41
+ end
42
+
43
+ def reply_to_post
44
+ result = ADN::API::Post.by_id(reply_to)
45
+ ADN.create_instance(result["data"], Post) unless result.has_error?
46
+ end
47
+
48
+ def replies(params = nil)
49
+ result = ADN::API::Post.replies(id, params)
50
+ ADN.create_collection(result["data"], Post) unless result.has_error?
51
+ end
52
+
53
+ def delete
54
+ result = ADN::API::Post.delete(id)
55
+ ADN.create_instance(result["data"], Post) unless result.has_error?
56
+ end
57
+
58
+ def set_values(values)
59
+ values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
60
+ end
61
+ end
62
+ end
data/lib/adn/user.rb ADDED
@@ -0,0 +1,109 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ class User
5
+ attr_accessor :user_id
6
+ attr_accessor :avatar_image, :counts, :cover_image,
7
+ :created_at, :description, :follows_you,
8
+ :id, :is_follower, :is_following, :is_muted,
9
+ :locale, :name, :timezone, :type, :username,
10
+ :you_follow, :you_muted
11
+
12
+ def initialize(user)
13
+ if user.respond_to?(:each_pair)
14
+ set_values(user)
15
+ self.user_id = id
16
+ else
17
+ self.user_id = user
18
+ user_details = details
19
+ if details.has_key? "data"
20
+ set_values(user_details["data"])
21
+ end
22
+ end
23
+ end
24
+
25
+ def details
26
+ if id
27
+ Hash[self.instance_variables.map { |i| [i.to_s.slice(1..-1), self.instance_variable_get(i)]}]
28
+ else
29
+ ADN::API::User.retrieve(user_id)
30
+ end
31
+ end
32
+
33
+ def created_at
34
+ DateTime.parse(@created_at)
35
+ end
36
+
37
+ # Followers/Users
38
+
39
+ def get_user(user)
40
+ user.is_a?(ADN::User) ? user.id : user
41
+ end
42
+
43
+ def follow(user)
44
+ user_id = get_user(user)
45
+ result = ADN.post("/stream/0/users/#{user_id}/follow")
46
+ ADN.create_instance(result["data"], User) unless result.has_error?
47
+ end
48
+
49
+ def unfollow(user)
50
+ user_id = get_user(user)
51
+ result = ADN.delete("/stream/0/users/#{user_id}/follow")
52
+ ADN.create_instance(result["data"], User) unless result.has_error?
53
+ end
54
+
55
+ def followers
56
+ result = ADN::API::User.followers(user_id)
57
+ ADN.create_collection(result["data"], User) unless result.has_error?
58
+ end
59
+
60
+ def following
61
+ result = ADN::API::User.following(user_id)
62
+ ADN.create_collection(result["data"], User) unless result.has_error?
63
+ end
64
+
65
+ # Mute
66
+
67
+ def mute(user)
68
+ user_id = get_user(user)
69
+ result = ADN.post("#{ADN::API_ENDPOINT_USERS}/#{user_id}/mute")
70
+ ADN.create_instance(result["data"], User) unless result.has_error?
71
+ end
72
+
73
+ def unmute(user)
74
+ user_id = get_user(user)
75
+ result = ADN.delete("#{ADN::API_ENDPOINT_USERS}/#{user_id}/mute")
76
+ ADN.create_instance(result["data"], User) unless result.has_error?
77
+ end
78
+
79
+ def mute_list
80
+ result = ADN.get("#{ADN::API_ENDPOINT_USERS}/me/muted")
81
+ ADN.create_collection(result["data"], User) unless result.has_error?
82
+ end
83
+
84
+ # Posts
85
+
86
+ def posts(params = nil)
87
+ result = ADN::API::Post.by_user(user_id, params)
88
+ ADN.create_collection(result["data"], Post) unless result.has_error?
89
+ end
90
+
91
+ def stream(params = nil)
92
+ result = ADN::API::Post.stream(params)
93
+ ADN.create_collection(result["data"], Post) unless result.has_error?
94
+ end
95
+
96
+ def mentions(params = nil)
97
+ result = ADN::API::Post.mentioning_user(user_id, params)
98
+ ADN.create_collection(result["data"], Post) unless result.has_error?
99
+ end
100
+
101
+ def set_values(values)
102
+ values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
103
+ end
104
+
105
+ def has_error?
106
+ self.id.nil?
107
+ end
108
+ end
109
+ end
data/lib/adn/version.rb CHANGED
@@ -1,3 +1,9 @@
1
- module Adn
2
- VERSION = "0.0.1"
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ MAJOR = 0
5
+ MINOR = 3
6
+ TINY = 2
7
+ VERSION = [MAJOR, MINOR, TINY].join('.')
3
8
  end
9
+
data/lib/adn.rb CHANGED
@@ -1,5 +1,54 @@
1
- require "adn/version"
2
1
 
3
- module Adn
4
- # Your code goes here...
2
+ #
3
+ # ADN - A simple and easy to use App.net Ruby library
4
+ #
5
+ # Copyright (c) 2012 Kishyr Ramdial
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ require 'uri'
27
+ require 'json'
28
+ require 'date'
29
+
30
+ %w{constants api post user version}.each { |f| require_relative "adn/#{f}" }
31
+
32
+ module ADN
33
+ class Error < StandardError; end
34
+
35
+ def self.token=(token)
36
+ @token = token
37
+ end
38
+
39
+ def self.token
40
+ @token
41
+ end
42
+
43
+ def self.has_error?(hash)
44
+ hash.has_key?("error")
45
+ end
46
+
47
+ def self.create_instance(data, type)
48
+ type.new(data)
49
+ end
50
+
51
+ def self.create_collection(data, type)
52
+ data.collect { |t| type.new(t) }
53
+ end
5
54
  end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADN::API::Filter do
6
+ subject { ADN::API::Filter }
7
+
8
+ # Not Implemented Yet
9
+ end