discourse_api 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85bfc87d0fafa517bf76b417bc63d77cc1e4981f
4
+ data.tar.gz: 220503a989f470fdb0b9edec7732e8b53eebf7ab
5
+ SHA512:
6
+ metadata.gz: 0e78225f1b54155f57a86f147f4323c96aadfb20737a294df74cd41cb3eda9cc9ed95578bbbd9c5bfdfef6563b6798fe357ba4e2d8cbd533d8a7b5988e9f09e3
7
+ data.tar.gz: 9390284acb2d3b7660dabeae0fdcc953e4f4707772977f88ee295020901430d17285550c4163efa0ef6ff10bd5804147f0601a97a8aa06f5e62cf1553d434588
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+ bin/
19
+ .ruby-gemset
20
+ .ruby-version
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: 'bundle exec rake test'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in discourse_api.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Discourse
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,55 @@
1
+ # DiscourseApi
2
+
3
+ [![Build Status](https://travis-ci.org/discourse/discourse_api.pong?branch=master)][travis]
4
+ [![Code Climate](https://codeclimate.com/github/discourse/discourse_api.png)][codeclimate]
5
+
6
+ [travis]: http://travis-ci.org/discourse/discourse_api
7
+ [codeclimate]: https://codeclimate.com/github/discourse/discourse_api
8
+
9
+ The Discourse API gem allows you to consume the Discourse API
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'discourse_api'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install discourse_api
24
+
25
+ ## Usage
26
+
27
+ Over time this project intends to have a full Discourse API. At the moment there are only a
28
+ few endpoints available:
29
+
30
+ ```ruby
31
+ client = DiscourseApi::Client.new("http://try.discourse.org")
32
+
33
+ # Topic endpoints
34
+ client.latest_topics #=> Gets a list of the latest topics
35
+ client.hot_topics #=> Gets a list of hot topics
36
+ client.new_topics #=> Gets a list of new topics
37
+ client.topics_by("sam") #=> Gets a list of topics created by user "sam"
38
+ client.topic(57) #=> Gets the topic with id 57
39
+
40
+ # Search endpoint
41
+ client.search("sandbox") #=> Gets a list of topics that match "sandbox"
42
+
43
+ # Categories endpoint
44
+ client.categories #=> Gets a list of categories
45
+
46
+ ```
47
+
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task test: :spec
7
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'discourse_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "discourse_api"
8
+ spec.version = DiscourseApi::VERSION
9
+ spec.authors = ["Sam Saffron", "John Paul Ashenfelter", "Michael Herold"]
10
+ spec.email = ["sam.saffron@gmail.com", "john@ashenfelter.com", "michael.j.herold@gmail.com"]
11
+ spec.description = %q{Discourse API}
12
+ spec.summary = %q{Allows access to the Discourse API}
13
+ spec.homepage = "http://github.com/discourse/discourse_api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday", "~> 0.8.8"
22
+ spec.add_dependency "faraday_middleware", "~> 0.9"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "guard-rspec"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "rb-inotify"
30
+ spec.add_development_dependency "simplecov"
31
+ end
@@ -0,0 +1,7 @@
1
+ # require 'discourse_api'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require File.expand_path('../../lib/discourse_api', __FILE__)
5
+
6
+ client = DiscourseApi::Client.new("http://localhost:3000", '6157bad92b3b5f8004aedc82220281c5966e5d560bb2bb6f962123474db4ac98')
7
+ puts client.latest
@@ -0,0 +1,10 @@
1
+ # require 'discourse_api'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require File.expand_path('../../lib/discourse_api', __FILE__)
5
+
6
+ client = DiscourseApi::Client.new("localhost",3000)
7
+ client.api_key = "6157bad92b3b5f8004aedc82220281c5966e5d560bb2bb6f962123474db4ac98"
8
+ client.api_username = "forumadmin"
9
+
10
+ client.topic_invite_user(topic_id: 1, email: "bob@bob.com")
@@ -0,0 +1,16 @@
1
+ # require 'discourse_api'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require File.expand_path('../../lib/discourse_api', __FILE__)
5
+
6
+ client = DiscourseApi::Client.new("localhost", 3000)
7
+ client.api_key = "YOUR_API_KEY"
8
+ client.api_username = "YOUR_USERNAME"
9
+
10
+ client.post_create(
11
+ category: "Boing Boing",
12
+ skip_validations: true,
13
+ auto_track: false,
14
+ title: "Concert Master: A new way to choose",
15
+ raw: "This is the raw markdown for my post"
16
+ )
@@ -0,0 +1,13 @@
1
+ # require 'discourse_api'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require File.expand_path('../../lib/discourse_api', __FILE__)
5
+
6
+ client = DiscourseApi::Client.new("localhost", 3000)
7
+ client.api_key = "YOUR_API_KEY"
8
+ client.api_username = "YOUR_USERNAME"
9
+
10
+ puts client.topics_latest({})
11
+ puts client.topics_hot({})
12
+ puts client.categories({})
13
+
@@ -0,0 +1,14 @@
1
+ # require 'discourse_api'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require File.expand_path('../../lib/discourse_api', __FILE__)
5
+
6
+ client = DiscourseApi::Client.new("localhost", 3000)
7
+ client.api_key = "YOUR_API_KEY"
8
+ client.api_username = "YOUR_USERNAME"
9
+
10
+ puts client.username_update(username: "Batman", new_username: "Alfred")
11
+ puts client.user_update(username: "Batman", name: "Bruce Wayne")
12
+ puts client.email_update(username: "Batman", email: "batman@example.com")
13
+ puts client.toggle_avatar(username: "Batman", use_uploaded_avatar: true)
14
+ puts client.upload_avatar(username: "DiscourseHero", file: "http://cdn.discourse.org/assets/logo.png")
@@ -0,0 +1,3 @@
1
+ require "discourse_api/client"
2
+ require "discourse_api/error"
3
+ require "discourse_api/version"
@@ -0,0 +1,10 @@
1
+ module DiscourseApi
2
+ module API
3
+ module Categories
4
+ def categories(*args)
5
+ response = get('/categories.json', args)
6
+ response[:body]['category_list']['categories']
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module DiscourseApi
2
+ module API
3
+ module Search
4
+ # Returns search results that match the specified term.
5
+ #
6
+ # @param term [String] a search term
7
+ # @param options [Hash] A customizable set of options
8
+ # @option options [String] :type_filter Returns results of the specified type.
9
+ # @return [Array] Return results as an array of Hashes.
10
+ def search(term, options={})
11
+ response = get('/search.json', options.merge(term: term))
12
+ response[:body]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ module DiscourseApi
2
+ module API
3
+ module Topics
4
+ def hot_topics(*args)
5
+ response = get('/hot.json', args)
6
+ response[:body]['topic_list']['topics']
7
+ end
8
+
9
+ def invite_user_to_topic(user_email, topic_id)
10
+ params = { email: user_email, topic_id: topic_id }
11
+ post "/t/#{topic_id}/invite.json", params
12
+ end
13
+
14
+ def latest_topics(*args)
15
+ response = get('/latest.json', args)
16
+ response[:body]['topic_list']['topics']
17
+ end
18
+
19
+ def new_topics(*args)
20
+ response = get("/new.json", args)
21
+ response[:body]['topic_list']['topics']
22
+ end
23
+
24
+ def topic(id, *args)
25
+ response = get("/t/#{id}.json", args)
26
+ response[:body]
27
+ end
28
+
29
+ def topics_by(username, *args)
30
+ response = get("/topics/created-by/#{username}.json", args)
31
+ response[:body]['topic_list']['topics']
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ module DiscourseApi
2
+ module API
3
+ module Users
4
+ def toggle_avatar(username, use_uploaded_avatar)
5
+ put("/users/#{username}/preferences/avatar/toggle", { use_uploaded_avatar: use_uploaded_avatar })
6
+ end
7
+
8
+ def user(username, *args)
9
+ response = get("/user/#{username}.json", args)
10
+ response[:body]['user']
11
+ end
12
+
13
+ def update_avatar(username, file)
14
+ put("/users/#{username}/preferences/avatar", { file: file })
15
+ end
16
+
17
+ def update_email(username, email)
18
+ put("/users/#{username}/preferences/email", { email: email })
19
+ end
20
+
21
+ def update_user(username, *args)
22
+ put("/users/#{username}", args)
23
+ end
24
+
25
+ def update_username(username, new_username)
26
+ put("/users/#{username}/preferences/username", { new_username: new_username })
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,76 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'json'
4
+ require 'discourse_api/version'
5
+ require 'discourse_api/api/categories'
6
+ require 'discourse_api/api/search'
7
+ require 'discourse_api/api/topics'
8
+ require 'discourse_api/api/users'
9
+
10
+ module DiscourseApi
11
+ class Client
12
+ attr_accessor :api_key, :api_username
13
+ attr_reader :host
14
+
15
+ include DiscourseApi::API::Categories
16
+ include DiscourseApi::API::Search
17
+ include DiscourseApi::API::Topics
18
+ include DiscourseApi::API::Users
19
+
20
+ def initialize(host, api_key=nil, api_username=nil)
21
+ @host = host
22
+ @api_key = api_key
23
+ @api_username = api_username
24
+ end
25
+
26
+ def connection_options
27
+ @connection_options ||= {
28
+ url: @host,
29
+ headers: {
30
+ accept: 'application/json',
31
+ user_agent: user_agent,
32
+ }
33
+ }
34
+ end
35
+
36
+ def delete(path, params={})
37
+ request(:delete, path, params)
38
+ end
39
+
40
+ def get(path, params={})
41
+ request(:get, path, params)
42
+ end
43
+
44
+ def post(path, params={})
45
+ request(:post, path, params)
46
+ end
47
+
48
+ def put(path, params={})
49
+ request(:put, path, params)
50
+ end
51
+
52
+ def user_agent
53
+ @user_agent ||= "DiscourseAPI Ruby Gem #{DiscourseApi::VERSION}"
54
+ end
55
+
56
+ private
57
+
58
+ def connection
59
+ @connection ||= Faraday.new connection_options do |conn|
60
+ # Convert request params to "www-form-encoded"
61
+ conn.request :url_encoded
62
+ # Parse responses as JSON
63
+ conn.response :json
64
+ # Use Faraday's default HTTP adapter
65
+ conn.adapter Faraday.default_adapter
66
+ end
67
+ end
68
+
69
+ def request(method, path, params={})
70
+ response = connection.send(method.to_sym, path, params)
71
+ response.env
72
+ rescue Faraday::Error::ClientError, JSON::ParserError
73
+ raise DiscourseApi::Error
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,14 @@
1
+ module DiscourseApi
2
+ class Error < StandardError
3
+ attr_reader :wrapped_exception
4
+
5
+ # Initializes a new Error object
6
+ #
7
+ # @param exception [Exception, String]
8
+ # @return [DiscourseApi::Error]
9
+ def initialize(exception=$!)
10
+ @wrapped_exception = exception
11
+ exception.respond_to?(:message) ? super(exception.message) : super(exception.to_s)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module DiscourseApi
2
+ VERSION = "0.1.1"
3
+ end