naver-sdk 0.1.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.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "naver/sdk"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ module Naver # :nodoc:
2
+ # Common functionality across Naver API objects.
3
+ class Client
4
+ # The connection object being used to communicate with Naver.
5
+ # @return [Naver::Connection] the connection
6
+ def connection
7
+ self.class.connection
8
+ end
9
+
10
+ # Converts the response body to an ObjectifiedHash.
11
+ def self.parse(body)
12
+ if body.is_a?(Hash)
13
+ ObjectifiedHash.new(body)
14
+ elsif body.is_a?(Array)
15
+ body.collect! { |e| ObjectifiedHash.new(e) }
16
+ elsif body
17
+ true
18
+ elsif !body
19
+ false
20
+ elsif body.nil?
21
+ false
22
+ else
23
+ raise Naver::Error.new("Couldn't parse a response body")
24
+ end
25
+ end
26
+
27
+ class << self
28
+ # The connection object being used to communicate with Naver.
29
+ # @return [Naver::Connection] the connection
30
+ def connection
31
+ @@connection ||= Connection.new
32
+ end
33
+
34
+ # Assign a default connection object.
35
+ # @param conn [Naver::Connection] the connection
36
+ # @return [Naver::Connection] the connection
37
+ def connection=(conn)
38
+ @@connection = conn
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,24 @@
1
+ module Naver
2
+ module Config
3
+ OPTION_KEYS = [
4
+ :client_id,
5
+ :client_secret,
6
+ :redirect_uri,
7
+ :timeout,
8
+ :debug
9
+ ]
10
+
11
+ attr_accessor *OPTION_KEYS
12
+
13
+ def configure
14
+ yield self
15
+ self
16
+ end
17
+
18
+ def options
19
+ options = {}
20
+ OPTION_KEYS.each{ |key| options[key] = send(key) }
21
+ options
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,75 @@
1
+ module Naver
2
+ class Connection
3
+ # Base URI for the NAVER API..
4
+ DEFAULT_API_BASE_URI = "https://openapi.naver.com"
5
+
6
+ # Create a Connection object.
7
+ # @param api_base_uri [String] Base URI at which to make API calls.
8
+ def initialize(api_base_uri: DEFAULT_API_BASE_URI)
9
+ @client_id = Naver.client_id
10
+ @client_secret = Naver.client_secret
11
+ @api_base_uri = api_base_uri
12
+
13
+ headers = { user_agent: "NAVER Ruby SDK Gem #{ Naver::Sdk::VERSION}" }
14
+ @connection = Faraday.new(url: @api_base_uri, headers: headers) do |faraday|
15
+ faraday.request :multipart
16
+ faraday.request :url_encoded
17
+ faraday.response :logger if Naver.debug
18
+ faraday.adapter :net_http
19
+ end
20
+ end
21
+
22
+ # Perform a GET request.
23
+ # @param path [String] The path at which to make ther request.
24
+ # @param params [Hash] A hash of request parameters.
25
+ def get(path, params = {})
26
+ request(:get, path, params)
27
+ end
28
+
29
+ # Perform a PUT request.
30
+ # @param path [String] The path at which to make ther request.
31
+ # @param params [Hash] A hash of request parameters.
32
+ def put(path, params = {})
33
+ request(:put, path, params)
34
+ end
35
+
36
+ # Perform a POST request.
37
+ # @param path [String] The path at which to make ther request.
38
+ # @param params [Hash] A hash of request parameters.
39
+ def post(path, params = {})
40
+ request(:post, path, params)
41
+ end
42
+
43
+ # Perform a DELETE request.
44
+ # @param path [String] The path at which to make ther request.
45
+ # @param params [Hash] A hash of request parameters.
46
+ def delete(path, params = {})
47
+ request(:delete, path, params)
48
+ end
49
+
50
+ private
51
+
52
+ def request(verb, path, params = {})
53
+ raise ArgumentError.new "Invalid http verb #{verb}" if ![:get, :post, :put, :delete].include?(verb)
54
+
55
+ response = @connection.run_request(verb, path, params, public_auth_header) do |request|
56
+ request.params.update(params) if verb == :get && params
57
+ yield(request) if block_given?
58
+ end
59
+
60
+ if !(200..299).include?(response.status)
61
+ body = JSON.parse(response.body)
62
+ raise Naver::Error.new(body)
63
+ end
64
+
65
+ response
66
+ end
67
+
68
+ def public_auth_header
69
+ {
70
+ "X-Naver-Client-Id" => @client_id,
71
+ "X-Naver-Client-Secret" => @client_secret
72
+ }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module Naver # :nodoc:
2
+ # Raised when there is an error communicating with Naver.
3
+ class Error < StandardError; end
4
+ end
data/lib/naver/map.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Naver # :nodoc:
2
+ # Naver Map API
3
+ class Map < Client
4
+ class << self
5
+ # 주소 -> 좌표 변환
6
+ # @param params [Hash] Params for the search
7
+ def geocode(params = {})
8
+ parse(JSON.parse(connection.get("/v1/map/geocode", params).body)["result"]["items"])
9
+ end
10
+
11
+ # 좌표 -> 주소 변환
12
+ # @param params [Hash] Params for the search
13
+ def reverse_geocode(params = {})
14
+ params = { query: "#{params[:lat]}, #{params[:lng]}" }
15
+ parse(JSON.parse(connection.get("/v1/map/reversegeocode", params).body)["result"]["items"])
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,68 @@
1
+ module Naver
2
+ class Oauth
3
+ # Base URI for NAVER OAuth.
4
+ # DEFAULT_OAUTH_BASE_URI = "https://nid.naver.com/oauth2.0"
5
+ DEFAULT_OAUTH_BASE_URI = "https://nid.naver.com"
6
+
7
+ # Create a Oauth object.
8
+ # @param oauth_base_uri [String] Base URI at which to make API calls.
9
+ def initialize(oauth_base_uri: DEFAULT_OAUTH_BASE_URI, redirect_uri: Naver.redirect_uri)
10
+ @client_id = Naver.client_id
11
+ @client_secret = Naver.client_secret
12
+ @oauth_base_uri = oauth_base_uri
13
+ @redirect_uri = redirect_uri
14
+
15
+ headers = { user_agent: "NAVER Ruby SDK Gem #{ Naver::Sdk::VERSION}" }
16
+ @oauth = OAuth2::Client.new(@client_id, @client_secret, site: @oauth_base_uri, authorize_url: "/oauth2.0/authorize", token_url: "/oauth2.0/token", headers: headers) do |http|
17
+ http.request :multipart
18
+ http.request :url_encoded
19
+ http.response :logger if Naver.debug
20
+ http.adapter :net_http
21
+ end
22
+ end
23
+
24
+ # Get OAuth URL for user authentication and authorization.
25
+ # @param requested_scopes [Array] An array of permission scopes being requested.
26
+ # @return [String] The authorization URL.
27
+ def authorization_url(requested_scopes: ["public"])
28
+ @oauth.auth_code.authorize_url(
29
+ redirect_uri: @redirect_uri,
30
+ scope: requested_scopes.join("")
31
+ )
32
+ end
33
+
34
+ # Generate an access token given an auth code received from NAVER.
35
+ # This is used internally to authenticate and authorize future user actions.
36
+ # @param auth_code [String] The OAuth authentication code from NAVER.
37
+ def authorize!(auth_code)
38
+ @oauth_token = @oauth.auth_code.get_token(auth_code, redirect_uri: @redirect_uri)
39
+ # TODO check if it succeeded
40
+ end
41
+
42
+ # Extract hash with OAuth token attributes. Extracted token attributes can
43
+ # be used with create_and_assign_token to prevent the need for
44
+ # reauthorization.
45
+ # @return [Hash, nil] @oauth_token converted to a hash.
46
+ def extract_token
47
+ @oauth_token.to_hash if @oauth_token
48
+ end
49
+
50
+ # Create and assign new access token from extracted token. To be used with
51
+ # extract_token to reauthorize app without api call.
52
+ # @param token_extract [Hash] OAuth token hash from #extract_token.
53
+ # @return [OAuth2::AccessToken, nil] New access token object.
54
+ def create_and_assign_token(token_extract)
55
+ unless token_extract.nil?
56
+ @oauth_token = OAuth2::AccessToken.from_hash(@oauth, token_extract)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def refresh_token!
63
+ return if !@oauth_token.expired?
64
+ @oauth_token = @oauth_token.refresh_token
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,35 @@
1
+ module Naver
2
+ # Converts hashes to the objects.
3
+ class ObjectifiedHash
4
+ # Creates a new ObjectifiedHash object.
5
+ def initialize(hash)
6
+ @hash = hash
7
+ @data = hash.inject({}) do |data, (key, value)|
8
+ value = ObjectifiedHash.new(value) if value.is_a?(Hash)
9
+ value = value.collect { |e| ObjectifiedHash.new(e) } if value.is_a?(Array)
10
+ data[key.to_s] = value
11
+ data
12
+ end
13
+ end
14
+
15
+ # @return [Hash] The original hash.
16
+ def to_hash
17
+ @hash
18
+ end
19
+ alias_method :to_h, :to_hash
20
+
21
+ # @return [String] Formatted string with the class name, object id and original hash.
22
+ def inspect
23
+ "#<#{self.class}:#{object_id} {hash: #{@hash.inspect}}"
24
+ end
25
+
26
+ # Delegate to ObjectifiedHash.
27
+ def method_missing(key)
28
+ @data.key?(key.to_s) ? @data[key.to_s] : nil
29
+ end
30
+
31
+ def respond_to_missing?(method_name, include_private = false)
32
+ @hash.keys.map(&:to_sym).include?(method_name.to_sym) || super
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ module Naver # :nodoc:
2
+ # Naver Papago API
3
+ class Papago < Client
4
+ class << self
5
+ # 기계번역(Beta)
6
+ # @param params [Hash] Params for the translate
7
+ def translate(params = {})
8
+ parse(JSON.parse(connection.post("/v1/language/translate", params).body)["message"]["result"])
9
+ end
10
+
11
+ # 한글인명 - 로마자 변환
12
+ # @param params [Hash] Params for the translate
13
+ def romanization(params = {})
14
+ parse(JSON.parse(connection.get("/v1/krdict/romanization", params).body)["aResult"][0])
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Naver # :nodoc:
2
+ # 네이버 회원 프로필 조회
3
+ class Profile < Client
4
+ class << self
5
+ # 회원 프로필
6
+ def me
7
+ parse(JSON.parse(connection.get("/v1/nid/me").body)["response"])
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Naver
2
+ module Sdk
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/naver/sdk.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "oauth2"
2
+ require "faraday"
3
+
4
+ require "naver/sdk/version"
5
+ require "naver/config"
6
+ require "naver/connection"
7
+ require "naver/client"
8
+ require "naver/error"
9
+ require "naver/oauth"
10
+ require "naver/objectified_hash"
11
+
12
+ require "naver/map"
13
+ require "naver/papago"
14
+ require "naver/profile"
15
+ require "naver/search"
16
+ require "naver/shortenurl"
17
+
18
+ module Naver
19
+ extend Config
20
+
21
+ module Sdk
22
+ end
23
+ end
@@ -0,0 +1,90 @@
1
+ module Naver # :nodoc:
2
+ # Naver Search API
3
+ class Search < Client
4
+ class << self
5
+ # Search > Blog (검색 > 블로그)
6
+ # @param params [Hash] Params for the search
7
+ def blog(params = {})
8
+ parse(JSON.parse(connection.get("/v1/search/blog", params).body))
9
+ end
10
+
11
+ # Search > News (검색 > 뉴스)
12
+ # @param params [Hash] Params for the search
13
+ def news(params = {})
14
+ parse(JSON.parse(connection.get("/v1/search/news", params).body))
15
+ end
16
+
17
+ # Search > Book (검색 > 책)
18
+ # @param params [Hash] Params for the search
19
+ def book(params = {})
20
+ parse(JSON.parse(connection.get("/v1/search/book", params).body))
21
+ end
22
+
23
+ # Search > Adult (검색 > 성인 검색어 판별)
24
+ # @param params [Hash] Params for the search
25
+ def adult(params = {})
26
+ parse(JSON.parse(connection.get("/v1/search/adult", params).body))
27
+ end
28
+
29
+ # Search > Encyc (검색 > 백과 사전)
30
+ # @param params [Hash] Params for the search
31
+ def encyc(params = {})
32
+ parse(JSON.parse(connection.get("/v1/search/encyc", params).body))
33
+ end
34
+
35
+ # Search > Movie (검색 > 영화)
36
+ # @param params [Hash] Params for the search
37
+ def movie(params = {})
38
+ parse(JSON.parse(connection.get("/v1/search/movie", params).body))
39
+ end
40
+
41
+ # Search > Cafearticle (검색 > 카페글)
42
+ # @param params [Hash] Params for the search
43
+ def cafearticle(params = {})
44
+ parse(JSON.parse(connection.get("/v1/search/cafearticle", params).body))
45
+ end
46
+
47
+ # Search > Kin (검색 > 지식 iN)
48
+ # @param params [Hash] Params for the search
49
+ def kin(params = {})
50
+ parse(JSON.parse(connection.get("/v1/search/kin", params).body))
51
+ end
52
+
53
+ # Search > Local (검색 > 지역)
54
+ # @param params [Hash] Params for the search
55
+ def local(params = {})
56
+ parse(JSON.parse(connection.get("/v1/search/local", params).body))
57
+ end
58
+
59
+ # Search > Errata (검색 > 오타변환)
60
+ # @param params [Hash] Params for the search
61
+ def errata(params = {})
62
+ parse(JSON.parse(connection.get("/v1/search/errata", params).body))
63
+ end
64
+
65
+ # Search > Webkr (검색 > 웹문서)
66
+ # @param params [Hash] Params for the search
67
+ def webkr(params = {})
68
+ parse(JSON.parse(connection.get("/v1/search/webkr", params).body))
69
+ end
70
+
71
+ # Search > Image (검색 > 이미지)
72
+ # @param params [Hash] Params for the search
73
+ def image(params = {})
74
+ parse(JSON.parse(connection.get("/v1/search/image", params).body))
75
+ end
76
+
77
+ # Search > Shop (검색 > 쇼핑)
78
+ # @param params [Hash] Params for the search
79
+ def shop(params = {})
80
+ parse(JSON.parse(connection.get("/v1/search/shop", params).body))
81
+ end
82
+
83
+ # Search > Doc (검색 > 전문자료)
84
+ # @param params [Hash] Params for the search
85
+ def doc(params = {})
86
+ parse(JSON.parse(connection.get("/v1/search/doc", params).body))
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ module Naver # :nodoc:
2
+ # Naver Shortenurl API
3
+ class Shortenurl < Client
4
+ class << self
5
+ # 단축URL
6
+ # @param params [Hash] Params for the origin url
7
+ def url(params = {})
8
+ parse(JSON.parse(connection.post("/v1/util/shorturl", params).body)["result"])
9
+ end
10
+ end
11
+ end
12
+ end
data/naver-sdk.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "naver/sdk/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "naver-sdk"
8
+ spec.version = Naver::Sdk::VERSION
9
+ spec.authors = ["Surim Kim"]
10
+ spec.email = ["kimsuelim@gmail.com"]
11
+
12
+ spec.summary = "NAVER SDK for Ruby"
13
+ spec.description = "The unofficial NAVER SDK for Ruby. Provides both resource oriented interfaces and API clients for NAVER services."
14
+ spec.homepage = "https://github.com/kimsuelim/naver-sdk-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "faraday", [">= 0.8", "< 1.0"]
25
+ spec.add_dependency "oauth2", "~> 1.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "rubocop", "~> 0.49.0"
31
+ spec.add_development_dependency "coveralls", '~> 0.8.21'
32
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naver-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Surim Kim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-24 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.8'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: oauth2
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.15'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.15'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.49.0
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.49.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: coveralls
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.8.21
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.8.21
117
+ description: The unofficial NAVER SDK for Ruby. Provides both resource oriented interfaces
118
+ and API clients for NAVER services.
119
+ email:
120
+ - kimsuelim@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".travis.yml"
127
+ - CODE_OF_CONDUCT.md
128
+ - Gemfile
129
+ - LICENSE.txt
130
+ - README.md
131
+ - Rakefile
132
+ - bin/console
133
+ - bin/setup
134
+ - lib/naver/client.rb
135
+ - lib/naver/config.rb
136
+ - lib/naver/connection.rb
137
+ - lib/naver/error.rb
138
+ - lib/naver/map.rb
139
+ - lib/naver/oauth.rb
140
+ - lib/naver/objectified_hash.rb
141
+ - lib/naver/papago.rb
142
+ - lib/naver/profile.rb
143
+ - lib/naver/sdk.rb
144
+ - lib/naver/sdk/version.rb
145
+ - lib/naver/search.rb
146
+ - lib/naver/shortenurl.rb
147
+ - naver-sdk.gemspec
148
+ homepage: https://github.com/kimsuelim/naver-sdk-ruby
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.6.11
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: NAVER SDK for Ruby
172
+ test_files: []