medium_api 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +62 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/medium_api/client.rb +79 -0
- data/lib/medium_api/configuration.rb +5 -0
- data/lib/medium_api/contributor.rb +4 -0
- data/lib/medium_api/errors.rb +8 -0
- data/lib/medium_api/post.rb +4 -0
- data/lib/medium_api/publication.rb +13 -0
- data/lib/medium_api/resource_api.rb +7 -0
- data/lib/medium_api/user.rb +21 -0
- data/lib/medium_api/utils.rb +30 -0
- data/lib/medium_api/version.rb +3 -0
- data/lib/medium_api.rb +43 -0
- data/medium_api.gemspec +33 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f7a60764dc7f8c9583797801ec3e2b87a05a8e7f1563f94c3c73f504e09fb720
|
4
|
+
data.tar.gz: 01f03c2b547b3bfd03ced8923da3e99fc9a9c94e396ddd566203418167650234
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91c4a32936c404de57b439d3326e3d9d2d31c0828a1fdfdc0906a92c3e65d9fc4598d8d38ccdc493fcbaa912b13c7125a686e62b76e41b71245e4545d8e19138
|
7
|
+
data.tar.gz: 6ecb7ae01aa27fdea0c98a11cb3f3a3316e0e1cacd7ceb7cb903bdcc8d7eeb45a0f0fe79525adbd8d6ba0cb777c874b74416fef0aa92f00ea7c91a260b1a22c0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
medium_api (0.1.0)
|
5
|
+
httparty (~> 0.20.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.8.0)
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
12
|
+
coderay (1.1.3)
|
13
|
+
crack (0.4.5)
|
14
|
+
rexml
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
hashdiff (1.0.1)
|
17
|
+
httparty (0.20.0)
|
18
|
+
mime-types (~> 3.0)
|
19
|
+
multi_xml (>= 0.5.2)
|
20
|
+
method_source (1.0.0)
|
21
|
+
mime-types (3.3.1)
|
22
|
+
mime-types-data (~> 3.2015)
|
23
|
+
mime-types-data (3.2021.0901)
|
24
|
+
multi_xml (0.6.0)
|
25
|
+
pry (0.14.1)
|
26
|
+
coderay (~> 1.1)
|
27
|
+
method_source (~> 1.0)
|
28
|
+
public_suffix (4.0.6)
|
29
|
+
rake (12.3.3)
|
30
|
+
rexml (3.2.5)
|
31
|
+
rspec (3.10.0)
|
32
|
+
rspec-core (~> 3.10.0)
|
33
|
+
rspec-expectations (~> 3.10.0)
|
34
|
+
rspec-mocks (~> 3.10.0)
|
35
|
+
rspec-core (3.10.1)
|
36
|
+
rspec-support (~> 3.10.0)
|
37
|
+
rspec-expectations (3.10.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.10.0)
|
40
|
+
rspec-mocks (3.10.2)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.10.0)
|
43
|
+
rspec-support (3.10.2)
|
44
|
+
vcr (6.0.0)
|
45
|
+
webmock (3.13.0)
|
46
|
+
addressable (>= 2.3.6)
|
47
|
+
crack (>= 0.3.2)
|
48
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
49
|
+
|
50
|
+
PLATFORMS
|
51
|
+
ruby
|
52
|
+
|
53
|
+
DEPENDENCIES
|
54
|
+
medium_api!
|
55
|
+
pry
|
56
|
+
rake (~> 12.0)
|
57
|
+
rspec (~> 3.0)
|
58
|
+
vcr
|
59
|
+
webmock
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# MediumApi
|
2
|
+
|
3
|
+
A ruby api wrapper for medium.com
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'medium_api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install medium_api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
First, you need api key. To do this email yourfriends@medium.com. For more info checkout [official docs](https://github.com/Medium/medium-api-docs#22-self-issued-access-tokens)
|
24
|
+
|
25
|
+
### Configure the gem
|
26
|
+
```ruby
|
27
|
+
MediumApi.configure do |config|
|
28
|
+
config.api_key = ''
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
### Get current user
|
33
|
+
```ruby
|
34
|
+
MediumApi.me
|
35
|
+
```
|
36
|
+
|
37
|
+
### Create a post for current user
|
38
|
+
```ruby
|
39
|
+
MediumApi.me.create_post(post_attributes)
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
### Get publications for current user
|
44
|
+
```
|
45
|
+
MediumApi.me.publications
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thesmartnik/medium_api_udemy
|
58
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "medium_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module MediumApi
|
2
|
+
# A main class to make http calls to medium.com
|
3
|
+
class Client
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
base_uri 'https://api.medium.com/v1'
|
7
|
+
|
8
|
+
attr_reader :api_key
|
9
|
+
|
10
|
+
# @visibility private
|
11
|
+
CODE_TO_ERROR = {
|
12
|
+
400 => Error::BadRequestError,
|
13
|
+
401 => Error::UnauthorizedError,
|
14
|
+
403 => Error::ForbiddenError
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize(api_key:)
|
18
|
+
@api_key = api_key
|
19
|
+
|
20
|
+
self.class.headers("Authorization" => "Bearer #{api_key}")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns current user for specified api_key
|
24
|
+
# @return [Hash]
|
25
|
+
# @raise [MediumApi::Error::UnauthorizedError]
|
26
|
+
def me
|
27
|
+
with_error_handling do
|
28
|
+
self.class.get('/me')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return publications for provided user_id
|
33
|
+
# @param user_id [String, Integer]
|
34
|
+
# @return [Array<Hash>]
|
35
|
+
# @raise [MediumApi::Error::UnauthorizedError]
|
36
|
+
# @raise [MediumApi::Error::ForbiddenError]
|
37
|
+
def user_publications(user_id)
|
38
|
+
with_error_handling do
|
39
|
+
self.class.get("/users/#{user_id}/publications")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return contributors for provided publication_id
|
44
|
+
# @param publication_id [String, Integer]
|
45
|
+
# @return [Array<Hash>]
|
46
|
+
# @raise [MediumApi::Error::UnauthorizedError]
|
47
|
+
def publication_contributors(publication_id)
|
48
|
+
with_error_handling do
|
49
|
+
self.class.get("/publications/#{publication_id}/contributors")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Creates user post for provided user_id
|
54
|
+
# @param user_id [String, Integer]
|
55
|
+
# @param post_attributes [Hash]
|
56
|
+
# @return [Array<Hash>]
|
57
|
+
# @see MediumApi::Post for possible post attributes
|
58
|
+
# @raise [MediumApi::Error::UnauthorizedError]
|
59
|
+
# @raise [MediumApi::Error::ForbiddenError]
|
60
|
+
# @raise [MediumApi::Error::BadRequestError]
|
61
|
+
def create_user_post(user_id, post_attributes)
|
62
|
+
with_error_handling do
|
63
|
+
self.class.post("/users/#{user_id}/posts", body: Utils.camelcase_keys(post_attributes))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def with_error_handling
|
70
|
+
response = yield
|
71
|
+
|
72
|
+
return response['data'] if response.success?
|
73
|
+
|
74
|
+
message = response['errors'].first['message']
|
75
|
+
error_class = CODE_TO_ERROR[response.code] || MediumApi::Error
|
76
|
+
raise error_class, message
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module MediumApi
|
2
|
+
class Publication < Struct.new(:id, :description, :name, :url, :image_url, keyword_init: true)
|
3
|
+
include ResourceApi
|
4
|
+
|
5
|
+
# @return [Array<Contributor>]
|
6
|
+
def contributors
|
7
|
+
data = client.publication_contributors(id)
|
8
|
+
data.map do |contributor_attrs|
|
9
|
+
Contributor.new(Utils.underscore_keys(contributor_attrs))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MediumApi
|
2
|
+
class User < Struct.new(:id, :username, :name, :url, :image_url, keyword_init: true)
|
3
|
+
include ResourceApi
|
4
|
+
# @param post_attributes
|
5
|
+
# @return [MediumApi::Post]
|
6
|
+
def create_post(post_attributes)
|
7
|
+
data = client.create_user_post(id, post_attributes)
|
8
|
+
|
9
|
+
Post.new(Utils.underscore_keys(data))
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [MediumApi::Publication]
|
13
|
+
def publications
|
14
|
+
data = client.user_publications(id)
|
15
|
+
|
16
|
+
data.map do |publication_attributes|
|
17
|
+
Publication.new(Utils.underscore_keys(publication_attributes))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MediumApi
|
2
|
+
# @api private
|
3
|
+
module Utils
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def underscore_keys(hash)
|
7
|
+
hash.transform_keys do |key|
|
8
|
+
new_key = underscore(key.to_s)
|
9
|
+
key.is_a?(Symbol) ? new_key.to_sym : new_key
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def underscore(string)
|
14
|
+
string.gsub(/([A-Z])/, '_\1').downcase
|
15
|
+
end
|
16
|
+
|
17
|
+
def camelcase_keys(hash)
|
18
|
+
hash.transform_keys do |key|
|
19
|
+
new_key = camelcase(key.to_s)
|
20
|
+
key.is_a?(Symbol) ? new_key.to_sym : new_key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def camelcase(string)
|
25
|
+
first_word, *words = string.split('_')
|
26
|
+
|
27
|
+
[first_word, *words.map(&:capitalize)].join('')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/medium_api.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "medium_api/version"
|
2
|
+
require 'httparty'
|
3
|
+
require 'medium_api/utils'
|
4
|
+
require 'medium_api/resource_api'
|
5
|
+
require 'medium_api/errors'
|
6
|
+
require 'medium_api/client'
|
7
|
+
require 'medium_api/configuration'
|
8
|
+
require 'medium_api/user'
|
9
|
+
require 'medium_api/publication'
|
10
|
+
require 'medium_api/post'
|
11
|
+
require 'medium_api/contributor'
|
12
|
+
|
13
|
+
module MediumApi
|
14
|
+
class << self
|
15
|
+
# Used for configuring MediumApi
|
16
|
+
# @example
|
17
|
+
# MediumApi.configure do |config|
|
18
|
+
# config.api_key = ''
|
19
|
+
# end
|
20
|
+
# @yield [MediumApi::Configuration]
|
21
|
+
def configure
|
22
|
+
yield configuration
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [MediumApi::Configuration]
|
26
|
+
def configuration
|
27
|
+
@configuration ||= Configuration.new
|
28
|
+
end
|
29
|
+
|
30
|
+
# Method to get current user for provided api_key
|
31
|
+
# @return [MediumApi::User]
|
32
|
+
def me
|
33
|
+
attributes = client.me
|
34
|
+
|
35
|
+
User.new(**Utils.underscore_keys(attributes))
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [MediumApi::Client]
|
39
|
+
def client
|
40
|
+
@client ||= Client.new(api_key: configuration.api_key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/medium_api.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/medium_api/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "medium_api"
|
5
|
+
spec.version = MediumApi::VERSION
|
6
|
+
spec.authors = [" TheSmartnik"]
|
7
|
+
spec.email = ["misharinn@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Medium Api Wrapper}
|
10
|
+
spec.description = %q{Gem to interact with medium.com API}
|
11
|
+
spec.homepage = "https://github.com/TheSmartnik/medium_api"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
16
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_dependency 'httparty', '~> 0.20.0'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'vcr'
|
32
|
+
spec.add_development_dependency 'webmock'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: medium_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- " TheSmartnik"
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.20.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.20.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: Gem to interact with medium.com API
|
84
|
+
email:
|
85
|
+
- misharinn@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/medium_api.rb
|
100
|
+
- lib/medium_api/client.rb
|
101
|
+
- lib/medium_api/configuration.rb
|
102
|
+
- lib/medium_api/contributor.rb
|
103
|
+
- lib/medium_api/errors.rb
|
104
|
+
- lib/medium_api/post.rb
|
105
|
+
- lib/medium_api/publication.rb
|
106
|
+
- lib/medium_api/resource_api.rb
|
107
|
+
- lib/medium_api/user.rb
|
108
|
+
- lib/medium_api/utils.rb
|
109
|
+
- lib/medium_api/version.rb
|
110
|
+
- medium_api.gemspec
|
111
|
+
homepage: https://github.com/TheSmartnik/medium_api
|
112
|
+
licenses: []
|
113
|
+
metadata:
|
114
|
+
homepage_uri: https://github.com/TheSmartnik/medium_api
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 2.3.0
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.1.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Medium Api Wrapper
|
134
|
+
test_files: []
|