searchmetrics_client 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95e1db4ed6494627ea79e3375c5d677a7ee83b75
4
- data.tar.gz: 075c549b78119777464bd8ede5d80b8f21e3403b
3
+ metadata.gz: 9b4e23854b017344179dec2d9b4b50352722dba5
4
+ data.tar.gz: b94fc66dff6c8e53fd7d2322011177dcf2df43f8
5
5
  SHA512:
6
- metadata.gz: e48c1ac5a902e4d4480df38340464ce622848a07a3c37839de40f72b58218dbad6d6676eabf7d1aebd327db901976412c6c0e3d6b6ea61e3151b719cbf71063c
7
- data.tar.gz: c6fe7c04efc8d60e83f9dc20f414c3c86c5c4cdaa8091f5ec9a215e22d91442ecf61faf4e64721e1e730845d7b4d1ea80f1a897181846adb551fe809034b3aaa
6
+ metadata.gz: 5b76222f390257b3b92bc1adf45fb402a865c6e5ced5e19a27bc6c87c9dca926598225480be4a766f97ba5350b685ecdcf061418388c6dd7a8c6dbf9297f6000
7
+ data.tar.gz: 4fb68b98114838f0f72b07dedb0caf84dcc064ef627c21ce5299aafdfb27b76c97fa6d70a24d66e748e3391c04e435f006d8d60019c0f3252914c3542992d703
data/.gitignore CHANGED
@@ -7,3 +7,6 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+
11
+ # project, testing specific
12
+ spec/searchmetrics_config.yml
@@ -1,2 +1,5 @@
1
1
  inherit_gem:
2
2
  td_critic: rubocop.yml
3
+
4
+ RSpec/FilePath:
5
+ Enabled: False
@@ -14,6 +14,8 @@ matrix:
14
14
  allow_failures:
15
15
  - rvm: ruby-head
16
16
  fast_finish: true
17
+ before_script:
18
+ - cp spec/searchmetrics_config.yml.example spec/searchmetrics_config.yml
17
19
  script:
18
20
  - bundle exec rake
19
21
  - bundle exec rubocop
@@ -0,0 +1,11 @@
1
+
2
+ 0.2.0 / 2016-02-24
3
+ ==================
4
+
5
+ * Use API v3 per default
6
+ * Drop oauth v1 support
7
+
8
+ 0.1.0 / 2015-11-24
9
+ ==================
10
+
11
+ * Initial version released
data/README.md CHANGED
@@ -31,7 +31,8 @@ SearchmetricsClient.configure do |config|
31
31
  end
32
32
  ```
33
33
 
34
- Additionally you can specify `api_version` and `api_url`, which defaults to `v1` and `http://api.searchmetrics.com` regardless.
34
+ Additionally you can specify `api_version` and `api_url`, which defaults to
35
+ `v3` and `http://api.searchmetrics.com` regardless.
35
36
 
36
37
  ## Usage
37
38
 
@@ -57,11 +58,22 @@ response = SearchmetricsClient::Request.send_request_from_hash(
57
58
  params: { countrycode: 'DE', keyword: 'ruby' }
58
59
  )
59
60
  ```
61
+
60
62
  Returned value can be accessed by `#value` method:
61
63
 
62
64
  ```ruby
63
65
  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"}]
66
+ ```
67
+
68
+ Response will look like:
69
+
70
+ ```ruby
71
+ [{
72
+ :cpc=>"2.37", :competition=>"3", :search_volume=>"12366",
73
+ :trend=>"81:100:100:81:81:81:81:66:81:81:81:81",
74
+ :ad_budget=>"29307.419999999998", :integration=>"images",
75
+ :advertiser_count=>"0", :keyword=>"ruby", :year_month=>"201510"
76
+ }]
65
77
  ```
66
78
 
67
79
  `#endpoint` returns requested type:
@@ -73,7 +85,7 @@ response.endpoint # => "ResearchKeywordsGetListKeywordinfo"
73
85
  `#url` returns requested url:
74
86
 
75
87
  ```ruby
76
- response.url # => "http://api.searchmetrics.com/v1/ResearchKeywordsGetListKeywordinfo.json?countrycode=DE&keyword=ruby"
88
+ response.url # => "http://api.searchmetrics.com/v3/ResearchKeywordsGetListKeywordinfo.json?countrycode=DE&keyword=ruby"
77
89
  ```
78
90
 
79
91
  Access to raw response is available by methods `#header` and `#body`.
@@ -81,15 +93,36 @@ Access to raw response is available by methods `#header` and `#body`.
81
93
 
82
94
  ## Development
83
95
 
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.
96
+ After checking out the repo, run `bin/setup` to install dependencies.
97
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for an
98
+ interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To
101
+ release a new version, update the version number in `version.rb`, and then run
102
+ `bundle exec rake release`, which will create a git tag for the version, push
103
+ git commits and tags, and push the `.gem` file to
104
+ [rubygems.org](https://rubygems.org).
85
105
 
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).
106
+ ## Testing
107
+
108
+ First you need to have API key and secret and put them on
109
+ `spec/searchmetrics_config.yml`. You can find an example in
110
+ `spec/searchmetrics_config.yml.example`.
111
+
112
+
113
+ ```
114
+ bundle exec rspec
115
+ ```
87
116
 
88
117
  ## Contributing
89
118
 
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.
119
+ Bug reports and pull requests are welcome on GitHub at
120
+ https://github.com/[USERNAME]/searchmetrics_client. This project is intended to
121
+ be a safe, welcoming space for collaboration, and contributors are expected to
122
+ adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
91
123
 
92
124
 
93
125
  ## License
94
126
 
95
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
127
+ The gem is available as open source under the terms of the [MIT
128
+ License](http://opensource.org/licenses/MIT).
@@ -8,4 +8,6 @@ require 'searchmetrics_client/request'
8
8
  require 'searchmetrics_client/response'
9
9
 
10
10
  module SearchmetricsClient
11
+ API_DEFAULT_VERSION = 'v3'.freeze
12
+ API_BASE_URL = 'http://api.searchmetrics.com'.freeze
11
13
  end
@@ -1,25 +1,36 @@
1
- require 'oauth'
1
+ require 'oauth2'
2
2
  require 'singleton'
3
3
  require 'forwardable'
4
4
 
5
- module SearchmetricsClient
6
- class Client
5
+ module SearchmetricsClient # :nodoc:
6
+ class Client # :nodoc:
7
7
  include Singleton
8
8
  extend Forwardable
9
9
 
10
10
  def_delegators :access_token, :get, :post
11
11
 
12
+ def access_token
13
+ @access_token ||= access_token_api_v3
14
+ end
15
+
12
16
  private
13
17
 
14
- def access_token
15
- @access_token ||= generate_access_token
18
+ def api_url
19
+ File.join(SearchmetricsClient.configuration.api_base_url,
20
+ SearchmetricsClient.configuration.api_version)
16
21
  end
17
22
 
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
+ # http://api.searchmetrics.com/v3/documentation/start
24
+ def access_token_api_v3
25
+ token_url = File.join(api_url, 'token')
26
+ client = OAuth2::Client.new(
27
+ SearchmetricsClient.configuration.api_key,
28
+ SearchmetricsClient.configuration.api_secret,
29
+ site: SearchmetricsClient.configuration.api_base_url,
30
+ token_url: token_url,
31
+ token_method: :post
32
+ )
33
+ client.client_credentials.get_token
23
34
  end
24
35
  end
25
36
  end
@@ -1,4 +1,4 @@
1
- module SearchmetricsClient
1
+ module SearchmetricsClient # :nodoc:
2
2
  class << self
3
3
  attr_accessor :configuration
4
4
  end
@@ -8,12 +8,12 @@ module SearchmetricsClient
8
8
  yield(configuration)
9
9
  end
10
10
 
11
- class Configuration
12
- attr_accessor :api_key, :api_secret, :api_version, :api_url
11
+ class Configuration # :nodoc:
12
+ attr_accessor :api_key, :api_secret, :api_version, :api_base_url
13
13
 
14
14
  def initialize
15
- @api_version = 'v1'
16
- @api_url = 'http://api.searchmetrics.com'
15
+ @api_version = SearchmetricsClient::API_DEFAULT_VERSION
16
+ @api_base_url = SearchmetricsClient::API_BASE_URL
17
17
  end
18
18
  end
19
19
  end
@@ -1,19 +1,19 @@
1
1
  module SearchmetricsClient
2
2
  module Errors
3
- class ApiCredentialsNotProvided < StandardError
3
+ class ApiCredentialsNotProvided < StandardError # :nodoc:
4
4
  def message
5
5
  'You have to configure SearchmetricsClient first.'
6
6
  end
7
7
  end
8
8
 
9
- class ApiRequestError < StandardError
9
+ class ApiRequestError < StandardError # :nodoc:
10
10
  def initialize(msg, response)
11
11
  @response = response
12
12
  super(msg)
13
13
  end
14
14
  end
15
15
 
16
- class WrongMethodError < StandardError
16
+ class WrongMethodError < StandardError # :nodoc:
17
17
  def message
18
18
  'Cannot determine HTTP method from endpoint name'
19
19
  end
@@ -1,5 +1,5 @@
1
1
  module SearchmetricsClient
2
- class Query
2
+ class Query # :nodoc:
3
3
  attr_accessor :query, :endpoint, :params
4
4
 
5
5
  def self.from_hash(hash)
@@ -2,7 +2,7 @@ require 'forwardable'
2
2
  require 'multi_json'
3
3
 
4
4
  module SearchmetricsClient
5
- class Request
5
+ class Request # :nodoc:
6
6
  attr_reader :url, :query, :response
7
7
 
8
8
  extend Forwardable
@@ -25,8 +25,9 @@ module SearchmetricsClient
25
25
  def send_request
26
26
  check_credentials
27
27
  result = Client.instance.public_send(method, url)
28
- check_errors(result)
29
28
  @response = SearchmetricsClient::Response.new(self, result)
29
+ rescue OAuth2::Error => e
30
+ check_errors(e.response)
30
31
  end
31
32
 
32
33
  private
@@ -34,29 +35,36 @@ module SearchmetricsClient
34
35
  def method
35
36
  return :get if endpoint =~ /Get/
36
37
  return :post if endpoint =~ /Post/
37
- fail SearchmetricsClient::Errors::WrongMethodError
38
+ raise SearchmetricsClient::Errors::WrongMethodError
38
39
  end
39
40
 
40
41
  def build_url
41
- [
42
- "#{SearchmetricsClient.configuration.api_url}/",
43
- "#{SearchmetricsClient.configuration.api_version}/",
44
- query.query
45
- ].join
42
+ File.join(SearchmetricsClient.configuration.api_base_url,
43
+ SearchmetricsClient.configuration.api_version,
44
+ query.query)
46
45
  end
47
46
 
48
47
  def check_credentials
49
- fail SearchmetricsClient::Errors::ApiCredentialsNotProvided unless credentials_present?
48
+ unless credentials_present?
49
+ raise SearchmetricsClient::Errors::ApiCredentialsNotProvided
50
+ end
50
51
  end
51
52
 
52
53
  def credentials_present?
53
- SearchmetricsClient.configuration.api_key && SearchmetricsClient.configuration.api_secret
54
+ SearchmetricsClient.configuration.api_key &&
55
+ SearchmetricsClient.configuration.api_secret
56
+ end
57
+
58
+ def result_success?(result)
59
+ return true if result.status == 200
60
+ false
54
61
  end
55
62
 
56
63
  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)
64
+ return if result_success?(result)
65
+ body = MultiJson.load(result.body, symbolize_keys: true)
66
+ message = body[:error_message]
67
+ raise SearchmetricsClient::Errors::ApiRequestError.new(message, result)
60
68
  end
61
69
  end
62
70
  end
@@ -1,8 +1,8 @@
1
1
  require 'forwardable'
2
2
  require 'multi_json'
3
3
 
4
- module SearchmetricsClient
5
- class Response
4
+ module SearchmetricsClient # :nodoc:
5
+ class Response # :nodoc:
6
6
  attr_reader :request
7
7
 
8
8
  extend Forwardable
@@ -19,7 +19,7 @@ module SearchmetricsClient
19
19
  end
20
20
 
21
21
  def status
22
- @raw_response.code.to_i
22
+ @raw_response.status
23
23
  end
24
24
  end
25
25
  end
@@ -1,3 +1,3 @@
1
1
  module SearchmetricsClient
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -10,6 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['developers@td-berlin.com']
11
11
 
12
12
  spec.summary = 'A thin abstraction over the searchmetrics API'
13
+ spec.description = 'Client library and thin abstraction over the' \
14
+ ' searchmetrics API'
13
15
  spec.homepage = 'https://github.com/td-berlin/searchmetrics_client'
14
16
  spec.license = 'MIT'
15
17
 
@@ -20,13 +22,13 @@ Gem::Specification.new do |spec|
20
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
23
  spec.require_paths = ['lib']
22
24
 
23
- spec.add_dependency 'oauth', '~> 0.4.7'
25
+ spec.add_dependency 'oauth2', '~> 1.0'
24
26
  spec.add_dependency 'multi_json', '~> 1.0'
25
27
 
26
28
  spec.add_development_dependency 'bundler', '~> 1.10'
27
29
  spec.add_development_dependency 'rake', '~> 10.0'
28
- spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'rspec', '~> 3.4'
29
31
  spec.add_development_dependency 'vcr', '~> 3.0'
30
32
  spec.add_development_dependency 'webmock', '~> 1.22'
31
- spec.add_development_dependency 'td_critic', '~> 0.1'
33
+ spec.add_development_dependency 'td_critic', '~> 0.2'
32
34
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchmetrics_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TD Developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: oauth
14
+ name: oauth2
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.4.7
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.4.7
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: multi_json
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '3.4'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '3.4'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: vcr
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -114,15 +114,15 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0.1'
117
+ version: '0.2'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0.1'
125
- description:
124
+ version: '0.2'
125
+ description: Client library and thin abstraction over the searchmetrics API
126
126
  email:
127
127
  - developers@td-berlin.com
128
128
  executables: []
@@ -135,6 +135,7 @@ files:
135
135
  - ".travis.yml"
136
136
  - CODE_OF_CONDUCT.md
137
137
  - Gemfile
138
+ - History.md
138
139
  - LICENSE
139
140
  - README.md
140
141
  - Rakefile