rubychina_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b516e37f225835cd24445cf2df8a8751913612f
4
+ data.tar.gz: 092c9c5472e9954a517a0f8ce61c6305a6ac15c0
5
+ SHA512:
6
+ metadata.gz: 4954b924170d408e24339c821a159bb615fdba1d69b96845ce6a46ba3257ddb20d419b6fe0153d4aa6a730ce0aa5ff1eb270a1f7d2f904a7fb7df74ae1047a93
7
+ data.tar.gz: 398be36900cdae9eae5fe3797fadba609d787fc91173b4c55ec0f42b2a702948ad9bcb31c868f4261bef3134cec5f6b0e3a965e5797ffcedcf2e097243c7bad4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 adamshen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # RubychinaApi
2
+
3
+ 为了了解如何用Ruby写Web api wrapper做的实验
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'rubychina_api'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ #### Api手册
14
+
15
+ [https://ruby-china.org/api/](https://ruby-china.org/api/)
16
+
17
+ #### 登录
18
+ ```ruby
19
+ RubychinaApi::Config.client_id = "blabla"
20
+ RubychinaApi::Config.secret = "blabla"
21
+
22
+ RubychinaApi::Client.login("username" => "name", "password" => "xxxx")
23
+ => #<RubychinaApi::Client:0x007fda3cdf98f0
24
+ @avatar_url=
25
+ "https://ruby-china-files.b0.upaiyun.com/user/large_avatar/xxxxx.jpg",
26
+ @id=xxxxx,
27
+ @meta=nil,
28
+ @name="xxxx">
29
+ ```
30
+
31
+ #### Api请求
32
+ 使用get、post方法请求数据
33
+ ```ruby
34
+ response = RubychinaApi::Operation.get("topics")
35
+ JSON.parse(response.body)["topics"].last.fetch("title")
36
+ => "Rails- 让我欢喜让我忧!"
37
+ ```
38
+
39
+ 需要二级url时,增加参数
40
+ ```ruby
41
+ response = RubychinaApi::Operation.get("topics", "24325")
42
+ JSON.parse(response.body).dig("topic","title")
43
+ => "《提问的智慧》"
44
+ ```
45
+
46
+ 需要params时,增加hash作为尾参数
47
+ ```ruby
48
+ response = RubychinaApi::Operation.get("topics", {"type": "no_reply"})
49
+ JSON.parse(response.body)["topics"].first.fetch("id")
50
+ => "给指定 url POST 包的时候,用 YAML.load_file 方法:当 YML 文件内有汉字时,报 utf-8 错"
51
+ ```
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ require "oauth2"
2
+ require "json"
3
+ require "faraday"
4
+
5
+ require_relative "rubychina_api/version"
6
+ require_relative "rubychina_api/client"
7
+ require_relative "rubychina_api/operation"
8
+
9
+ module RubychinaApi
10
+ class Config
11
+ class << self
12
+ attr_accessor :client_id, :secret, :access_token
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module RubychinaApi
2
+ class Client
3
+ attr_reader :id, :name, :avatar_url, :meta
4
+
5
+ def initialize(attributes)
6
+ @id = attributes["id"]
7
+ @name = attributes["name"]
8
+ @avatar_url = attributes["avatar_url"]
9
+ @meta = attributes["meta"]
10
+ end
11
+
12
+ def self.login(option)
13
+ client = OAuth2::Client.new(RubychinaApi::Config.client_id, RubychinaApi::Config.secret, site: 'https://ruby-china.org')
14
+ RubychinaApi::Config.access_token = client.password.get_token(option["username"], option["password"]).token
15
+ response = RubychinaApi::Operation.get("hello")
16
+ new(JSON.parse(response.body).fetch("user"))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module RubychinaApi
2
+ class Operation
3
+ API_URL = "https://ruby-china.org/api/v3/"
4
+
5
+ class << self
6
+ def method_missing(method_id, *args)
7
+ method_name = method_id.id2name
8
+ raise "invalid operation method" unless valid_method?(method_name)
9
+ raise "must get access_token first" unless RubychinaApi::Config.access_token
10
+
11
+ option = {}
12
+ option.merge!(args.pop) if args.last.is_a?(Hash)
13
+
14
+ Faraday.send method_name,
15
+ "#{API_URL}#{args.join('/')}.json?access_token=#{RubychinaApi::Config.access_token}", option
16
+
17
+ end
18
+
19
+ private
20
+ def valid_method?(method_name)
21
+ %w(get post).include?(method_name)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module RubychinaApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubychina_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubychina_api"
8
+ spec.version = RubychinaApi::VERSION
9
+ spec.authors = ["adamshen"]
10
+ spec.email = ["adam_ruby@126.com"]
11
+ spec.summary = %q{Simple wrapper for the RubyChina API}
12
+ spec.description = %q{Simple wrapper for the RubyChina API}
13
+ spec.homepage = "https://github.com/adamshen/rubychina_api"
14
+ spec.license = "MIT"
15
+ spec.files = %w(LICENSE.txt README.md Rakefile rubychina_api.gemspec)
16
+ spec.files += Dir["lib/**/*"]
17
+
18
+ spec.add_dependency "faraday"
19
+ spec.add_dependency "json"
20
+ spec.add_dependency "oauth2"
21
+ spec.add_development_dependency "vcr"
22
+ spec.add_development_dependency "webmock"
23
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubychina_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - adamshen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oauth2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple wrapper for the RubyChina API
84
+ email:
85
+ - adam_ruby@126.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/rubychina_api.rb
94
+ - lib/rubychina_api/client.rb
95
+ - lib/rubychina_api/operation.rb
96
+ - lib/rubychina_api/version.rb
97
+ - rubychina_api.gemspec
98
+ homepage: https://github.com/adamshen/rubychina_api
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.5.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Simple wrapper for the RubyChina API
122
+ test_files: []