Class: Majestic::Api::Client
- Inherits:
-
Object
- Object
- Majestic::Api::Client
- Includes:
- Logger
- Defined in:
- lib/majestic/api/client.rb
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
-
#execute_command(name, params: {}, options: {}) ⇒ Object
This method will execute the specified command as an api request.
-
#execute_open_app_request(command_name, access_token, params: {}, options: {}) ⇒ Object
This will execute the specified command as an OpenApp request.
-
#execute_request(params: {}, options: {}) ⇒ Object
‘parameters’ a hash containing the command parameters.
- #get_index_item_info(urls:, params: {}, options: {}) ⇒ Object
-
#initialize(configuration: ::Majestic::Api.configuration) ⇒ Client
constructor
A new instance of Client.
- #set_api_url(format = :json) ⇒ Object
- #set_connection(headers: {}) ⇒ Object
- #verbose ⇒ Object
Methods included from Logger
Constructor Details
#initialize(configuration: ::Majestic::Api.configuration) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 |
# File 'lib/majestic/api/client.rb', line 8 def initialize(configuration: ::Majestic::Api.configuration) self.configuration = configuration set_api_url set_connection end |
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
5 6 7 |
# File 'lib/majestic/api/client.rb', line 5 def api_url @api_url end |
#configuration ⇒ Object
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/majestic/api/client.rb', line 5 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
5 6 7 |
# File 'lib/majestic/api/client.rb', line 5 def connection @connection end |
Instance Method Details
#execute_command(name, params: {}, options: {}) ⇒ Object
This method will execute the specified command as an api request. ‘name’ is the name of the command you wish to execute, e.g. GetIndexItemInfo ‘parameters’ a hash containing the command parameters. ‘timeout’ specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.
60 61 62 63 64 65 66 67 |
# File 'lib/majestic/api/client.rb', line 60 def execute_command(name, params: {}, options: {}) params.merge!({ app_api_key: self.configuration.api_key, cmd: name }) self.execute_request(params: params, options: ) end |
#execute_open_app_request(command_name, access_token, params: {}, options: {}) ⇒ Object
This will execute the specified command as an OpenApp request. ‘command_name’ is the name of the command you wish to execute, e.g. GetIndexItemInfo ‘parameters’ a hash containing the command parameters. ‘access_token’ the token provided by the user to access their resources. ‘timeout’ specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds.
74 75 76 77 78 79 80 81 82 |
# File 'lib/majestic/api/client.rb', line 74 def execute_open_app_request(command_name, access_token, params: {}, options: {}) params.merge!({ accesstoken: access_token, cmd: command_name, privatekey: self.configuration.api_key }) self.execute_request(params: params, options: ) end |
#execute_request(params: {}, options: {}) ⇒ Object
‘parameters’ a hash containing the command parameters. ‘options’ a hash containing command/call options (timeout, proxy settings etc)
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/majestic/api/client.rb', line 86 def execute_request(params: {}, options: {}) response = nil log(:info, "[Majestic::Api::Client] - Sending API Request to Majestic SEO. Parameters: #{params.inspect}. Options: #{.inspect}") response = self.connection.get do |request| request.params = params if params && !params.empty? request. = if && !.empty? end return response end |
#get_index_item_info(urls:, params: {}, options: {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/majestic/api/client.rb', line 41 def get_index_item_info(urls:, params: {}, options: {}) request_parameters = {} request_parameters[:datasource] = params.fetch(:data_source, "historic") request_parameters[:items] = urls.size urls.each_with_index do |url, index| request_parameters["item#{index}"] = url end response = self.execute_command("GetIndexItemInfo", params: request_parameters, options: ) response = Majestic::Api::ItemInfoResponse.new(response) return response end |
#set_api_url(format = :json) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/majestic/api/client.rb', line 15 def set_api_url(format = :json) self.api_url = case self.configuration.environment.to_sym when :sandbox then "https://developer.majestic.com/api/#{format}" when :production then "https://api.majestic.com/api/#{format}" else "https://developer.majestic.com/api/#{format}" end end |
#set_connection(headers: {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/majestic/api/client.rb', line 24 def set_connection(headers: {}) headers = { "User-Agent" => self.configuration.user_agent, "Content-Type" => "application/json" }.merge(headers) self.connection = Faraday.new(url: self.api_url, ssl: {verify: false}) do |builder| builder.headers = headers builder.request :url_encoded builder.request :retry builder.response :json builder.response :logger if self.configuration.verbose builder.adapter :net_http end end |
#verbose ⇒ Object
99 100 101 |
# File 'lib/majestic/api/client.rb', line 99 def verbose self.configuration.verbose end |