Class: GitHubMarkdownAPI::APIClient Abstract
- Inherits:
-
Object
- Object
- GitHubMarkdownAPI::APIClient
- Defined in:
- lib/github_markdown_api/api_client.rb
Overview
This class is abstract.
Abstract class of API Clients
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content_type ⇒ String
HTTP ContentType.
-
#endpoints ⇒ Hash
Endpoint (Server path of API).
-
#host ⇒ String
Hostname.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#port ⇒ Fixnum
Port number.
-
#scheme ⇒ Symbol
Scheme of API.
Class Method Summary collapse
Instance Method Summary collapse
-
#default_options ⇒ Hash
Returns hash of default options.
-
#endpoint(type) ⇒ URI
Returns endpoint of API type.
-
#initialize(args = {}, sub_args = {}) ⇒ APIClient
constructor
A new instance of APIClient.
-
#render(markdown) ⇒ Object
abstract
Renders HTML from Markdown.
-
#request(raw_uri, post) ⇒ String
Requests API.
- #set_option(args) ⇒ self
Constructor Details
#initialize(args = {}, sub_args = {}) ⇒ APIClient
Returns a new instance of APIClient.
9 10 11 12 13 14 15 16 |
# File 'lib/github_markdown_api/api_client.rb', line 9 def initialize (args = {}, sub_args = {}) case args when Hash set_option args when String # pending end end |
Instance Attribute Details
#content_type ⇒ String
Returns HTTP ContentType.
34 35 36 |
# File 'lib/github_markdown_api/api_client.rb', line 34 def content_type @content_type end |
#endpoints ⇒ Hash
Returns Endpoint (Server path of API).
31 32 33 |
# File 'lib/github_markdown_api/api_client.rb', line 31 def endpoints @endpoints end |
#host ⇒ String
Returns Hostname.
25 26 27 |
# File 'lib/github_markdown_api/api_client.rb', line 25 def host @host end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
18 19 20 |
# File 'lib/github_markdown_api/api_client.rb', line 18 def last_response @last_response end |
#port ⇒ Fixnum
Returns Port number.
28 29 30 |
# File 'lib/github_markdown_api/api_client.rb', line 28 def port @port end |
#scheme ⇒ Symbol
Note:
:http of :https
Returns Scheme of API.
22 23 24 |
# File 'lib/github_markdown_api/api_client.rb', line 22 def scheme @scheme end |
Class Method Details
.render(markdown) ⇒ Object
108 109 110 |
# File 'lib/github_markdown_api/api_client.rb', line 108 def self.render (markdown) return self.new.render markdown end |
Instance Method Details
#default_options ⇒ Hash
Returns hash of default options
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/github_markdown_api/api_client.rb', line 50 def return { scheme: GitHubMarkdownAPI::DEFAULT_SCHEME, host: GitHubMarkdownAPI::DEFAULT_HOST, port: GitHubMarkdownAPI::DEFAULT_PORT, endpoints: GitHubMarkdownAPI::DEFAULT_ENDPOINTS, auth: GitHubMarkdownAPI::DEFAULT_AUTH, content_type: GitHubMarkdownAPI::DEFAULT_CONTENT_TYPE, } end |
#endpoint(type) ⇒ URI
Returns endpoint of API type
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/github_markdown_api/api_client.rb', line 93 def endpoint (type) path = @endpoints[type.to_sym] klass = case @scheme when :http; URI::HTTP when :https; URI::HTTPS end param = { host: @host, path: path, } param[:port] = @port || 443 return klass.build(param) end |
#render(markdown) ⇒ Object
This method is abstract.
Renders HTML from Markdown
86 87 88 |
# File 'lib/github_markdown_api/api_client.rb', line 86 def render (markdown) raise NotImplementedError, "#{__method__} is a abstract method." end |
#request(raw_uri, post) ⇒ String
Requests API
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/github_markdown_api/api_client.rb', line 65 def request (raw_uri, post) http = Net::HTTP.new(raw_uri.host, raw_uri.port) if @scheme == :https http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end response = http.start{ http.request post } @last_response = response case response when Net::HTTPSuccess return response.body else raise RuntimeError, response end end |
#set_option(args) ⇒ self
38 39 40 41 42 43 44 45 46 |
# File 'lib/github_markdown_api/api_client.rb', line 38 def set_option (args) option = .merge(args) @scheme = option[:scheme].intern @host = option[:host].to_s @port = option[:port] @endpoints = option[:endpoints].to_hash @content_type = option[:content_type].to_s return self end |