searchmetrics_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95e1db4ed6494627ea79e3375c5d677a7ee83b75
4
+ data.tar.gz: 075c549b78119777464bd8ede5d80b8f21e3403b
5
+ SHA512:
6
+ metadata.gz: e48c1ac5a902e4d4480df38340464ce622848a07a3c37839de40f72b58218dbad6d6676eabf7d1aebd327db901976412c6c0e3d6b6ea61e3151b719cbf71063c
7
+ data.tar.gz: c6fe7c04efc8d60e83f9dc20f414c3c86c5c4cdaa8091f5ec9a215e22d91442ecf61faf4e64721e1e730845d7b4d1ea80f1a897181846adb551fe809034b3aaa
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ inherit_gem:
2
+ td_critic: rubocop.yml
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ sudo: false
2
+ bundler_args: --jobs=3 --retry=3
3
+ cache:
4
+ - bundler
5
+ before_install:
6
+ - gem update --remote bundler
7
+ language: ruby
8
+ rvm:
9
+ - 2.1
10
+ - 2.2
11
+ - jruby
12
+ - ruby-head
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ fast_finish: true
17
+ script:
18
+ - bundle exec rake
19
+ - bundle exec rubocop
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in searchmetrics_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 trommsdorff + drüner gmbh
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # SearchmetricsClient
2
+
3
+ [![Build Status](https://travis-ci.org/td-berlin/searchmetrics_client.svg)](https://travis-ci.org/td-berlin/searchmetrics_client)
4
+
5
+ A thin abstraction over the [searchmetrics API](http://api.searchmetrics.com).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'searchmetrics_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install searchmetrics_client
22
+
23
+ ## Configuration
24
+
25
+ To start using gem you have to configure API keys as bellow:
26
+
27
+ ```ruby
28
+ SearchmetricsClient.configure do |config|
29
+ config.api_key = 'Your api key'
30
+ config.api_secret = 'Your api secret'
31
+ end
32
+ ```
33
+
34
+ Additionally you can specify `api_version` and `api_url`, which defaults to `v1` and `http://api.searchmetrics.com` regardless.
35
+
36
+ ## Usage
37
+
38
+ To get metric from Searchmetrics server use:
39
+
40
+ ```ruby
41
+ SearchmetricsClient::Request.send_request_from_hash(
42
+ endpoint: 'ResearchKeywordsGetListKeywordinfo',
43
+ params: { countrycode: 'PL',
44
+ keyword: 'ruby' }
45
+ )
46
+ ```
47
+
48
+ Key `endpoint` is required.
49
+
50
+ `SearchmetricsClient::Response` object will be returned.
51
+
52
+ Exemplary call:
53
+
54
+ ```ruby
55
+ response = SearchmetricsClient::Request.send_request_from_hash(
56
+ endpoint: 'ResearchKeywordsGetListKeywordinfo',
57
+ params: { countrycode: 'DE', keyword: 'ruby' }
58
+ )
59
+ ```
60
+ Returned value can be accessed by `#value` method:
61
+
62
+ ```ruby
63
+ response.value
64
+ # => [{:cpc=>"2.37", :competition=>"3", :search_volume=>"12366", :trend=>"81:100:100:81:81:81:81:66:81:81:81:81", :ad_budget=>"29307.419999999998", :integration=>"images", :advertiser_count=>"0", :keyword=>"ruby", :year_month=>"201510"}]
65
+ ```
66
+
67
+ `#endpoint` returns requested type:
68
+
69
+ ```ruby
70
+ response.endpoint # => "ResearchKeywordsGetListKeywordinfo"
71
+ ```
72
+
73
+ `#url` returns requested url:
74
+
75
+ ```ruby
76
+ response.url # => "http://api.searchmetrics.com/v1/ResearchKeywordsGetListKeywordinfo.json?countrycode=DE&keyword=ruby"
77
+ ```
78
+
79
+ Access to raw response is available by methods `#header` and `#body`.
80
+
81
+
82
+ ## Development
83
+
84
+ 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.
85
+
86
+ 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).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/searchmetrics_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
91
+
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'searchmetrics_client'
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ require 'oauth'
2
+ require 'singleton'
3
+ require 'forwardable'
4
+
5
+ module SearchmetricsClient
6
+ class Client
7
+ include Singleton
8
+ extend Forwardable
9
+
10
+ def_delegators :access_token, :get, :post
11
+
12
+ private
13
+
14
+ def access_token
15
+ @access_token ||= generate_access_token
16
+ end
17
+
18
+ def generate_access_token
19
+ consumer = OAuth::Consumer.new(SearchmetricsClient.configuration.api_key,
20
+ SearchmetricsClient.configuration.api_secret,
21
+ site: SearchmetricsClient.configuration.api_url)
22
+ OAuth::AccessToken.from_hash(consumer, oauth_token: '', oauth_token_secret: '')
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ module SearchmetricsClient
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ self.configuration ||= Configuration.new
8
+ yield(configuration)
9
+ end
10
+
11
+ class Configuration
12
+ attr_accessor :api_key, :api_secret, :api_version, :api_url
13
+
14
+ def initialize
15
+ @api_version = 'v1'
16
+ @api_url = 'http://api.searchmetrics.com'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module SearchmetricsClient
2
+ module Errors
3
+ class ApiCredentialsNotProvided < StandardError
4
+ def message
5
+ 'You have to configure SearchmetricsClient first.'
6
+ end
7
+ end
8
+
9
+ class ApiRequestError < StandardError
10
+ def initialize(msg, response)
11
+ @response = response
12
+ super(msg)
13
+ end
14
+ end
15
+
16
+ class WrongMethodError < StandardError
17
+ def message
18
+ 'Cannot determine HTTP method from endpoint name'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,38 @@
1
+ module SearchmetricsClient
2
+ class Query
3
+ attr_accessor :query, :endpoint, :params
4
+
5
+ def self.from_hash(hash)
6
+ endpoint = hash.fetch(:endpoint)
7
+ params = hash.fetch(:params, {})
8
+ new(endpoint, params)
9
+ end
10
+
11
+ def initialize(endpoint, params = {})
12
+ @endpoint = endpoint
13
+ @params = params
14
+ @query = build_query
15
+ end
16
+
17
+ def to_h
18
+ hash = { endpoint: endpoint }
19
+ hash[:params] = params unless params.empty?
20
+ hash
21
+ end
22
+
23
+ private
24
+
25
+ def build_query
26
+ params_string = build_params unless params.empty?
27
+ "#{endpoint}.json#{params_string}"
28
+ end
29
+
30
+ def build_params
31
+ params_string = params.map do |k, v|
32
+ v = v.map(&:to_s).join(',') if v.is_a? Array
33
+ "#{k}=#{v}"
34
+ end.join('&')
35
+ "?#{params_string}"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ require 'forwardable'
2
+ require 'multi_json'
3
+
4
+ module SearchmetricsClient
5
+ class Request
6
+ attr_reader :url, :query, :response
7
+
8
+ extend Forwardable
9
+ def_delegator :query, :endpoint
10
+
11
+ def self.send_request(query)
12
+ new(query).send_request
13
+ end
14
+
15
+ def self.send_request_from_hash(hash)
16
+ query = SearchmetricsClient::Query.from_hash(hash)
17
+ SearchmetricsClient::Request.send_request(query)
18
+ end
19
+
20
+ def initialize(query)
21
+ @query = query
22
+ @url = build_url
23
+ end
24
+
25
+ def send_request
26
+ check_credentials
27
+ result = Client.instance.public_send(method, url)
28
+ check_errors(result)
29
+ @response = SearchmetricsClient::Response.new(self, result)
30
+ end
31
+
32
+ private
33
+
34
+ def method
35
+ return :get if endpoint =~ /Get/
36
+ return :post if endpoint =~ /Post/
37
+ fail SearchmetricsClient::Errors::WrongMethodError
38
+ end
39
+
40
+ def build_url
41
+ [
42
+ "#{SearchmetricsClient.configuration.api_url}/",
43
+ "#{SearchmetricsClient.configuration.api_version}/",
44
+ query.query
45
+ ].join
46
+ end
47
+
48
+ def check_credentials
49
+ fail SearchmetricsClient::Errors::ApiCredentialsNotProvided unless credentials_present?
50
+ end
51
+
52
+ def credentials_present?
53
+ SearchmetricsClient.configuration.api_key && SearchmetricsClient.configuration.api_secret
54
+ end
55
+
56
+ def check_errors(result)
57
+ return if result.code == '200'
58
+ message = MultiJson.load(result.body, symbolize_keys: true)[:error_message]
59
+ fail SearchmetricsClient::Errors::ApiRequestError.new(message, result)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ require 'forwardable'
2
+ require 'multi_json'
3
+
4
+ module SearchmetricsClient
5
+ class Response
6
+ attr_reader :request
7
+
8
+ extend Forwardable
9
+ def_delegators :request, :endpoint, :query, :url
10
+ def_delegators :@raw_response, :body, :header
11
+
12
+ def initialize(request, raw_response)
13
+ @request = request
14
+ @raw_response = raw_response
15
+ end
16
+
17
+ def value
18
+ @value ||= MultiJson.load(body, symbolize_keys: true)
19
+ end
20
+
21
+ def status
22
+ @raw_response.code.to_i
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module SearchmetricsClient
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'searchmetrics_client/version'
2
+
3
+ require 'searchmetrics_client/client'
4
+ require 'searchmetrics_client/configuration'
5
+ require 'searchmetrics_client/errors'
6
+ require 'searchmetrics_client/query'
7
+ require 'searchmetrics_client/request'
8
+ require 'searchmetrics_client/response'
9
+
10
+ module SearchmetricsClient
11
+ end
@@ -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 'searchmetrics_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'searchmetrics_client'
8
+ spec.version = SearchmetricsClient::VERSION
9
+ spec.authors = ['TD Developers']
10
+ spec.email = ['developers@td-berlin.com']
11
+
12
+ spec.summary = 'A thin abstraction over the searchmetrics API'
13
+ spec.homepage = 'https://github.com/td-berlin/searchmetrics_client'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'oauth', '~> 0.4.7'
24
+ spec.add_dependency 'multi_json', '~> 1.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.10'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'vcr', '~> 3.0'
30
+ spec.add_development_dependency 'webmock', '~> 1.22'
31
+ spec.add_development_dependency 'td_critic', '~> 0.1'
32
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: searchmetrics_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TD Developers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.22'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.22'
111
+ - !ruby/object:Gem::Dependency
112
+ name: td_critic
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.1'
125
+ description:
126
+ email:
127
+ - developers@td-berlin.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/searchmetrics_client.rb
144
+ - lib/searchmetrics_client/client.rb
145
+ - lib/searchmetrics_client/configuration.rb
146
+ - lib/searchmetrics_client/errors.rb
147
+ - lib/searchmetrics_client/query.rb
148
+ - lib/searchmetrics_client/request.rb
149
+ - lib/searchmetrics_client/response.rb
150
+ - lib/searchmetrics_client/version.rb
151
+ - searchmetrics_client.gemspec
152
+ homepage: https://github.com/td-berlin/searchmetrics_client
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.4.5.1
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: A thin abstraction over the searchmetrics API
176
+ test_files: []